看在[l,r]区间内是否存在[x,y]区间内某一个数的k倍。枚举即可...小心爆掉ll。
#include<bits/stdc++.h>
using namespace std;
int main(){
long long l,r,x,y,k;
while(cin>>l>>r>>x>>y>>k){
bool flag=0;
for(long long i=x;i<=y;i++){
if(i*k<=r&&i*k>=l)
{
flag=1;
break;
}
if(i*k>=r)break;
}
if(flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
Gleb ordered pizza home. When the courier delivered the pizza, he was very upset, because several pieces of sausage lay on the crust, and he does not really like the crust.
The pizza is a circle of radius r and center at the origin. Pizza consists of the main part — circle of radius r - d with center at the origin, and crust around the main part of the width d. Pieces of sausage are also circles. The radius of the i -th piece of the sausage is ri, and the center is given as a pair (xi, yi).
Gleb asks you to help determine the number of pieces of sausage caught on the crust. A piece of sausage got on the crust, if it completely lies on the crust.
First string contains two integer numbers r and d (0 ≤ d < r ≤ 500) — the radius of pizza and the width of crust.
Next line contains one integer number n — the number of pieces of sausage (1 ≤ n ≤ 105).
Each of next n lines contains three integer numbers xi, yi and ri ( - 500 ≤ xi, yi ≤ 500, 0 ≤ ri ≤ 500), where xi and yi are coordinates of the center of i-th peace of sausage, ri — radius of i-th peace of sausage.
Output the number of pieces of sausage that lay on the crust.
8 4 7 7 8 1 -7 3 2 0 2 1 0 -2 2 -3 -3 1 0 6 2 5 3 1
2
10 8 4 0 0 9 0 0 10 1 0 1 1 0 2
0
Below is a picture explaining the first example. Circles of green color denote pieces of sausage lying on the crust.
<center>依旧暴力水题,直接算两个距离即可。
#include<bits/stdc++.h>
using namespace std;
struct Node{
int x,y,r;
}sau[100005];
int main(){
int r,d,n;
while(cin>>r>>d){
cin>>n;
int sum=0;
for(int i=0;i<n;i++){
cin>>sau[i].x>>sau[i].y>>sau[i].r;
}
for(int i=0;i<n;i++){
if(sqrt(sau[i].x*sau[i].x+sau[i].y*sau[i].y)-sau[i].r>=(r-d)&&sqrt(sau[i].x*sau[i].x+sau[i].y*sau[i].y)+sau[i].r<=r)
sum++;
}
cout<<sum<<endl;
}
return 0;
}
由于一个数的因子个数为松散的n√,而且一条路径上的gcd会很快的将到1。所以如果我们从根节点往下dfs,记录所有可以通过把1个或0个点权值变成0得到的gcd。对于每个点,答案就是转移到当前点的所有gcd的最大值。
时间复杂度为O(V∗d),其中V为顶点数,d为因子数。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iostream>
#define M(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
const int MAXN=200007;
typedef long long LL;
struct Edge
{
int to, ne;
}e[MAXN<<1];
int head[MAXN], v[MAXN], edgenum;
inline void addedge(int u, int v)
{
edgenum++;e[edgenum].to=v, e[edgenum].ne=head[u], head[u]=edgenum;
edgenum++;e[edgenum].to=u, e[edgenum].ne=head[v], head[v]=edgenum;
}
vector<int> dp[MAXN];
int fgcd[MAXN];
int gcd(int a, int b) { return b==0 ? a : gcd(b, a%b); }
void dfs(int u, int fa)
{
if(fa==-1)
{
fgcd[u]=v[u];
dp[u].push_back(0);
dp[u].push_back(v[u]);
}
else
{
fgcd[u]=gcd(fgcd[fa], v[u]);
dp[u].push_back(fgcd[fa]);
for(int i=0;i<dp[fa].size();i++)
dp[u].push_back(gcd(dp[fa][i], v[u]));
sort(dp[u].begin(), dp[u].end());
dp[u].erase(unique(dp[u].begin(), dp[u].end()), dp[u].end());
}
for(int i=head[u]; ~i;i=e[i].ne)
if(e[i].to!=fa)
dfs(e[i].to, u);
}
int main()
{
int n;
while(cin>>n)
{
for(int i=1;i<=n;i++)
scanf("%d", &v[i]);
M(head, -1);edgenum=0;M(dp, 0);
for(int i=1;i<n;i++)
{
int u, v;cin>>u>>v;
addedge(u, v);
}
for(int i=1;i<=n;i++) dp[i].clear();
dfs(1, -1);
for(int i=1;i<=n;i++)
printf("%d%c", dp[i][dp[i].size()-1], i==n ? '\n' : ' ');
}
}
求mex可以用trie来搞。把整个序列的trie建出来后,假如要全局异或一个x,若x的从高到低第i位为1,则trie的第i层的所有节点都要翻转左右儿子。知道了这点后,我们就可以通过打标记实现翻转来快速异或一个值,而不需要实际进行异或。因为所有数都在300000内,那么01字典树开20层就够。标记什么的类似线段树,边查询边pushdown,如果这一层对应的位是1,那么交换左右儿子。每一个节点存一个是否儿子满了的标志,查询时如果左儿子没满,mex值肯定在左子树;要是满了,就进入右子树。