Codeforces Round #430 (Div. 2) A B C D

来源:互联网 发布:dt大数据 编辑:程序博客网 时间:2024/05/16 09:18

A. Kirill And The Game
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Kirill plays a new computer game. He came to the potion store where he can buy any potion. Each potion is characterized by two integers — amount of experience and cost. The efficiency of a potion is the ratio of the amount of experience to the cost. Efficiency may be a non-integer number.

For each two integer numbers a and b such that l ≤ a ≤ r and x ≤ b ≤ y there is a potion with experience a and cost b in the store (that is, there are (r - l + 1)·(y - x + 1) potions).

Kirill wants to buy a potion which has efficiency k. Will he be able to do this?

Input

First string contains five integer numbers lrxyk (1 ≤ l ≤ r ≤ 1071 ≤ x ≤ y ≤ 1071 ≤ k ≤ 107).

Output

Print "YES" without quotes if a potion with efficiency exactly k can be bought in the store and "NO" without quotes otherwise.

You can output each of the letters in any register.

Examples
input
1 10 1 10 1
output
YES
input
1 5 6 10 1
output
NO

A题,比赛的时候挂了。

不能直接根据数据的区间关系判断,因为整数是不连续的。


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;const int maxn=100005,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L);  int main() {ll l,r,x,y,k,i;cin >> l >> r >> x >> y >> k;for (i=x;i<=y;i++) {if (i*k>=l&&i*k<=r) {cout << "YES";return 0;}}cout << "NO";return 0;}

B. Gleb And Pizza
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 (xiyi).

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.

Input

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 xiyi and ri ( - 500 ≤ xi, yi ≤ 5000 ≤ 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

Output the number of pieces of sausage that lay on the crust.

Examples
input
8 477 8 1-7 3 20 2 10 -2 2-3 -3 10 6 25 3 1
output
2
input
10 840 0 90 0 101 0 11 0 2
output
0
Note

Below is a picture explaining the first example. Circles of green color denote pieces of sausage lying on the crust.


水。


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;const int maxn=105,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L);  int sqr(int x) {return x*x;}int main() {int d,R,n,x,y,r,i,ans=0;cin >> R >> d >> n;int p=d;d=R;R=R-p;for (i=1;i<=n;i++) {scanf("%d%d%d",&x,&y,&r);if (sqr(x)+sqr(y)>=sqr(R+r)&&sqr(x)+sqr(y)<=sqr(d-r)) ans++;}printf("%d\n",ans);return 0;}

C. Ilya And The Tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found a very interesting tree rooted at vertex 1. There is an integer number written on each vertex of the tree; the number written on vertex i is equal to ai.

Ilya believes that the beauty of the vertex x is the greatest common divisor of all numbers written on the vertices on the path from the root to x, including this vertex itself. In addition, Ilya can change the number in one arbitrary vertex to 0 or leave all vertices unchanged. Now for each vertex Ilya wants to know the maximum possible beauty it can have.

For each vertex the answer must be considered independently.

The beauty of the root equals to number written on it.

Input

First line contains one integer number n — the number of vertices in tree (1 ≤ n ≤ 2·105).

Next line contains n integer numbers ai (1 ≤ i ≤ n1 ≤ ai ≤ 2·105).

Each of next n - 1 lines contains two integer numbers x and y (1 ≤ x, y ≤ nx ≠ y), which means that there is an edge (x, y) in the tree.

Output

Output n numbers separated by spaces, where i-th number equals to maximum possible beauty of vertex i.

Examples
input
26 21 2
output
6 6 
input
36 2 31 21 3
output
6 6 6 
input
110
output
10 



一棵树,每个点有一个点权。设1号点为根,问对于每一个点,从树上单独删去一个点的点权或者不删,从根到该点的gcd最大是多少。


观察发现,一共只有两种情况:

1.把根删去,每个点的答案就是从根到该点路径上除去根所有点的gcd,可以dfs得到。不删的结果肯定没有删根更优。

2.删除根之外另一个点。

由于求所有点的gcd必须经过根,所有答案只可能是根的一个因子。这时只要再次dfs,看根到每个点的路径上,这些因子是否至少出现了树的深度减1这么多次,如果这样,说明删掉路径上一个点之后这个因子可能是答案。


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;const int maxn=200005,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L);  int num;int head[maxn],ans[maxn],a[maxn],b[maxn],t[maxn];bool visit[maxn];struct Edge {int from,to,pre;};Edge edge[maxn*2];void addedge(int from,int to) {edge[num]=(Edge){from,to,head[from]};head[from]=num++;edge[num]=(Edge){to,from,head[to]};head[to]=num++;}int gcd(int x,int y) {int z;if (x<y) swap(x,y);while (y) {z=y;y=x%y;x=z;}return x;}void dfs(int now,int fa) {visit[now]=1;if (fa==1||fa==0) ans[now]=a[now]; else     if (now!=1) ans[now]=gcd(ans[fa],a[now]);for (int i=head[now];i!=-1;i=edge[i].pre) {int to=edge[i].to;if (!visit[to]) dfs(to,now);}}void dfs2(int now,int depth) {visit[now]=1;for (int i=1;i<=num;i++) {if (a[now]%b[i]==0) t[i]++;if (t[i]>=depth) ans[now]=max(ans[now],b[i]);}for (int i=head[now];i!=-1;i=edge[i].pre) {int to=edge[i].to;if (!visit[to]) dfs2(to,depth+1);}for (int i=1;i<=num;i++) {if (a[now]%b[i]==0) t[i]--;}}int main() {int n,i,x,y;num=0;memset(head,-1,sizeof(head));scanf("%d",&n);for (i=1;i<=n;i++) {scanf("%d",&a[i]);}for (i=1;i<n;i++) {scanf("%d%d",&x,&y);addedge(x,y);}mem0(visit);dfs(1,0);num=0;for (i=1;i<=sqrt(a[1]);i++) {if (a[1]%i==0) {b[++num]=i;if ((a[1]/i)*(a[1]/i)!=a[1]) b[++num]=a[1]/i;}}sort(b+1,b+num+1);mem0(visit);visit[1]=1;mem0(t);for (i=head[1];i!=-1;i=edge[i].pre) {if (!visit[edge[i].to]) {dfs2(edge[i].to,0);}}for (i=1;i<=n;i++) printf("%d ",ans[i]);return 0;}

D. Vitya and Strange Lesson
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Today at the lesson Vitya learned a very interesting function — mexMex of a sequence of numbers is the minimum non-negative number that is not present in the sequence as element. For example, mex([4, 33, 0, 1, 1, 5]) = 2 and mex([1, 2, 3]) = 0.

Vitya quickly understood all tasks of the teacher, but can you do the same?

You are given an array consisting of n non-negative integers, and m queries. Each query is characterized by one number x and consists of the following consecutive steps:

  • Perform the bitwise addition operation modulo 2 (xor) of each array element with the number x.
  • Find mex of the resulting array.

Note that after each query the array changes.

Input

First line contains two integer numbers n and m (1 ≤ n, m ≤ 3·105) — number of elements in array and number of queries.

Next line contains n integer numbers ai (0 ≤ ai ≤ 3·105) — elements of then array.

Each of next m lines contains query — one integer number x (0 ≤ x ≤ 3·105).

Output

For each query print the answer on a separate line.

Examples
input
2 21 313
output
10
input
4 30 1 5 6124
output
200
input
5 40 1 5 6 71145
output
2202

一个序列,每次把所有数异或一个数,保留结果,求最小的非负的序列中没有出现的数字。


这题需要利用异或的一个性质:(a^b)^c=a^(b^c)。

这样,每次查询我们可以认为是原序列异或上之后所有数字的异或和。


题目需要求最小的没有出现的数字,有点不好求。不过,这题中每个数字的范围并不是很大,不超过2^20=524288.

既然是没有出现,每次的序列固定,我们可以反其道而行之,把所有没有出现的数字统计出来,然后只需要求这些数中选择一个和当前要异或的数字异或运算的最小值。

求这个值,是字典树的典型应用。详见:POJ 3764 The Xor-longest length


建树的时候需要注意,虽然数字最大只有300000,但是需要把2^20以下的数字全部考虑进去,因为字典树是按照深度建立的,只有这样才能使得所有二进制为20位的未出现数字全部考虑完全。


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;const int maxn=300005,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L);  bool f[524288];int num;struct Tree{int l,r;};Tree tree[maxn*20];void insert(int now,int val,int depth) {if (depth==21) return;if ((val>>(20-depth))%2==1) {if (tree[now].l==-1) {tree[now].l=++num;tree[num].l=tree[num].r=-1;}insert(tree[now].l,val,depth+1);} else {if (tree[now].r==-1) {tree[now].r=++num;tree[num].l=tree[num].r=-1;}insert(tree[now].r,val,depth+1);}}int findxor(int now,int val,int depth) {if (depth==21) return 0;if ((val>>(20-depth))%2==0) {if (tree[now].r!=-1) return findxor(tree[now].r,val,depth+1);else return findxor(tree[now].l,val,depth+1)+(1<<(20-depth));} else {if (tree[now].l!=-1) return findxor(tree[now].l,val,depth+1);else return findxor(tree[now].r,val,depth+1)+(1<<(20-depth));}}int main() {int n,m,i,t=0,x,ans;scanf("%d%d",&n,&m);num=0;tree[0].l=tree[0].r=-1;mem0(f);for (i=1;i<=n;i++) {scanf("%d",&x);f[x]=1;}for (i=0;i<=524288;i++) {if (!f[i]) insert(0,i,0);}for (i=1;i<=m;i++) {scanf("%d",&x);t=t^x;ans=findxor(0,t,0);printf("%d\n",ans);}return 0;}

原创粉丝点击