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 l, r, x, y, k (1 ≤ l ≤ r ≤ 107, 1 ≤ x ≤ y ≤ 107, 1 ≤ 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
题意:问在[L,R]区间内存不存在一个数i(x<=i<=y)使得商为k。
代码:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#include<map>
#include<math.h>
using namespace std;
typedef pair<int,int>pa;
const int N=5e5+100;
int read()
{
int x=0;
char ch = getchar();
while('0'>ch||ch>'9')ch=getchar();
while('0'<=ch&&ch<='9')
{
x=(x<<3)+(x<<1)+ch-'0';
ch=getchar();
}
return x;
}
/***********************************************************/
int l,r,x,y;
double k;
int main()
{
cin>>l>>r>>x>>y>>k;
for(int i=x;i<=y;i++)
{
double lx=l*1.0/i;
double lr=r*1.0/i;
if(lx<=k&&lr>=k)
{
puts("YES");
return 0;
}
}
puts("NO");
}
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 (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.
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 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
Output the number of pieces of sausage that lay on the crust.
Examples
Input
8 4
7
7 8 1
-7 3 2
0 2 1
0 -2 2
-3 -3 1
0 6 2
5 3 1
Output
2
Input
10 8
4
0 0 9
0 0 10
1 0 1
1 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.
题意:问有多少小圆在在内半径r-d外半径r的圆环内。
题解:通过圆心距+-半径判断就好。
代码:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#include<map>
#include<math.h>
using namespace std;
typedef pair<int,int>pa;
const int N=5e5+100;
int read()
{
int x=0;
char ch = getchar();
while('0'>ch||ch>'9')ch=getchar();
while('0'<=ch&&ch<='9')
{
x=(x<<3)+(x<<1)+ch-'0';
ch=getchar();
}
return x;
}
/***********************************************************/
int d,r,n;
int ans;
double x,y,di;
int solve()
{
double dis=sqrt(x*x+y*y);
if(dis-di<r-d) return 0;
if(dis+di>r) return 0;
return 1;
}
int main()
{
scanf("%d%d",&r,&d);
scanf("%d",&n);
ans=0;
for(int i=1;i<=n;i++)
{
scanf("%lf%lf%lf",&x,&y,&di);
if(solve()) 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 ≤ n, 1 ≤ ai ≤ 2·105).
Each of next n - 1 lines contains two integer numbers x and y (1 ≤ x, y ≤ n, x ≠ 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
2
6 2
1 2
Output
6 6
Input
3
6 2 3
1 2
1 3
Output
6 6 6
Input
1
10
Output
10
题意:设数上某点的魅力值等于从根到这个点的gcd的最大值(允许让路径上某个点的值为0)求所有点最大魅力值。
题解:用set维护跟到点的所有gcd情况,然后判断。
代码:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#include<map>
#include<math.h>
using namespace std;
typedef pair<int,int>pa;
const int N=2e5+100;
int read()
{
int x=0;
char ch = getchar();
while('0'>ch||ch>'9')ch=getchar();
while('0'<=ch&&ch<='9')
{
x=(x<<3)+(x<<1)+ch-'0';
ch=getchar();
}
return x;
}
/***********************************************************/
int head[N];
int val[N];
int vis[N];
int cnt,n,x,y;
struct node
{
int to,next;
}edge[N<<2];
set<int>st[N];
set<int>::iterator it;
void add(int f,int to)
{
edge[cnt].to=to;
edge[cnt].next=head[f];
head[f]=cnt++;
}
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
}
void dfs(int v,int fa,int gcd)
{
vis[v]=1;
for(it=st[fa].begin();it!=st[fa].end();it++)
{
st[v].insert(__gcd(*it,val[v]));
}
st[v].insert(gcd);
gcd=__gcd(gcd,val[v]);
st[v].insert(gcd);
for(int i=head[v];i!=-1;i=edge[i].next)
{
if(!vis[edge[i].to])
dfs(edge[i].to,v,gcd);
}
}
int main()
{
init();
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&val[i]);
for(int i=1;i<n;i++)
{
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
dfs(1,0,0);
for(int i=1;i<=n;i++)
printf("%d ",*st[i].rbegin());
}