C - C
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
CodeForces 621B
Description
Today, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns of the grid are numbered from 1 to 1000. Rows are numbered from top to bottom, while columns are numbered from left to right.
Wet Shark thinks that two bishops attack each other if they share the same diagonal. Note, that this is the only criteria, so two bishops may attack each other (according to Wet Shark) even if there is another bishop located between them. Now Wet Shark wants to count the number of pairs of bishops that attack each other.
Input
The first line of the input contains n (1 ≤ n ≤ 200 000) — the number of bishops.
Each of next n lines contains two space separated integers xi and yi (1 ≤ xi, yi ≤ 1000) — the number of row and the number of column where i-th bishop is positioned. It’s guaranteed that no two bishops share the same position.
Output
Output one integer — the number of pairs of bishops which attack each other.
Sample Input
Input
5
1 1
1 5
3 3
5 1
5 5
Output
6
Input
3
1 1
2 3
3 5
Output
0
Hint
In the first sample following pairs of bishops attack each other: (1, 3), (1, 5), (2, 3), (2, 4), (3, 4) and (3, 5). Pairs (1, 2), (1, 4), (2, 5) and (4, 5) do not attack each other because they do not share the same diagonal.
#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<set>
using namespace std;
const int MAX=200005;
struct node
{
int x,y;
int s,c;
};
bool cmp(node a,node b)
{
if (a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
int n;
node nodes[MAX];
int x1[2005]={0};
int x2[2005]={0};
int main()
{
scanf("%d",&n);
for (int i=0;i<n;i++)
{
scanf("%d%d",&nodes[i].x,&nodes[i].y);
nodes[i].s=nodes[i].x+nodes[i].y;
nodes[i].c=nodes[i].x-nodes[i].y;
x1[nodes[i].s]++;
x2[nodes[i].c+1000]++;
}
sort(nodes,nodes+n,cmp);
long long ans=0;
for (int i=0;i<2005;i++)
{
if (x1[i]!=1)
ans+=x1[i]*(x1[i]-1)/2;
if (x2[i]!=1)
ans+=x2[i]*(x2[i]-1)/2;
}
cout<<ans<<endl;
return 0;
}
D - D
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
CodeForces 621C
Description
There are n sharks who grow flowers for Wet Shark. They are all sitting around the table, such that sharks i and i + 1 are neighbours for all i from 1 to n - 1. Sharks n and 1 are neighbours too.
Each shark will grow some number of flowers si. For i-th shark value si is random integer equiprobably chosen in range from li to ri. Wet Shark has it’s favourite prime number p, and he really likes it! If for any pair of neighbouring sharks i and j the product si·sj is divisible by p, then Wet Shark becomes happy and gives 1000 dollars to each of these sharks.
At the end of the day sharks sum all the money Wet Shark granted to them. Find the expectation of this value.
Input
The first line of the input contains two space-separated integers n and p (3 ≤ n ≤ 100 000, 2 ≤ p ≤ 109) — the number of sharks and Wet Shark’s favourite prime number. It is guaranteed that p is prime.
The i-th of the following n lines contains information about i-th shark — two space-separated integers li and ri (1 ≤ li ≤ ri ≤ 109), the range of flowers shark i can produce. Remember that si is chosen equiprobably among all integers from li to ri, inclusive.
Output
Print a single real number — the expected number of dollars that the sharks receive in total. You answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let’s assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .
Sample Input
Input
3 2
1 2
420 421
420420 420421
Output
4500.0
Input
3 5
1 4
2 3
11 14
Output
0.0
Hint
A prime number is a positive integer number that is divisible only by 1 and itself. 1 is not considered to be prime.
Consider the first sample. First shark grows some number of flowers from 1 to 2, second sharks grows from 420 to 421 flowers and third from 420420 to 420421. There are eight cases for the quantities of flowers (s0, s1, s2) each shark grows:
(1, 420, 420420): note that s0·s1 = 420, s1·s2 = 176576400, and s2·s0 = 420420. For each pair, 1000 dollars will be awarded to each shark. Therefore, each shark will be awarded 2000 dollars, for a total of 6000 dollars.
(1, 420, 420421): now, the product s2·s0 is not divisible by 2. Therefore, sharks s0 and s2 will receive 1000 dollars, while shark s1 will receive 2000. The total is 4000.
(1, 421, 420420): total is 4000
(1, 421, 420421): total is 0.
(2, 420, 420420): total is 6000.
(2, 420, 420421): total is 6000.
(2, 421, 420420): total is 6000.
(2, 421, 420421): total is 4000.
The expected value is .
In the second sample, no combination of quantities will garner the sharks any money.
D: 一个区间内能整除k的概率为p[i]=(r/k-(l-1)/k)/(r-l+1)
#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
using namespace std;
const int MAXN=100010;
double p[MAXN];
int main()
{
int l,r,n,k,i;
while(scanf("%d%d",&n,&k)!=EOF)
{
for(i=0; i<n; i++)
{
scanf("%d%d",&l,&r);
int pp=r/k-(l-1)/k;
p[i]=(double)pp/(r-l+1);
}
p[n]=p[0];
double ans=0;
for(i=0; i<n; i++)
{
ans+=(p[i]+p[i+1]-p[i]*p[i+1])*2000.0;
}
printf("%lf\n",ans);
}
return 0;
}
E - E
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
CodeForces 501C
Description
Let’s define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0).
Next day Misha couldn’t remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha.
Input
The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph.
The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 1, 0 ≤ si < 216), separated by a space.
Output
In the first line print number m, the number of edges of the graph.
Next print m lines, each containing two distinct numbers, a and b (0 ≤ a ≤ n - 1, 0 ≤ b ≤ n - 1), corresponding to edge (a, b).
Edges can be printed in any order; vertices of the edge can also be printed in any order.
Sample Input
Input
3
2 3
1 0
1 0
Output
2
1 0
2 0
Input
2
1 1
1 0
Output
1
0 1
Hint
The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as “^”, and in Pascal — as “xor”.
E:
#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
using namespace std;
const int N = (1<<16);
int n,degree[N], s[N], vis[N];
vector<pair<int,int> > v;
queue<int> q;
int main()
{
scanf("%d", &n);
for(int i=0 ; i<n ; i++)
{
scanf("%d%d", °ree[i], &s[i]);
if(degree[i]==1) q.push(i);
}
while(!q.empty())
{
int u = q.front();
q.pop();
if(degree[u]==0) continue;
degree[u]--;
if(!vis[s[u]])
{
degree[s[u]]--, s[s[u]]^=u;
if(degree[s[u]]==1) q.push(s[u]), vis[s[u]]=1;
}
else degree[s[u]]--;
v.push_back(make_pair(u, s[u]));
}
printf("%d\n", v.size());
for(int i=0 ; i<(int)v.size() ; i++)
printf("%d %d\n", v[i].first, v[i].second);
return 0;
}