Codeforces Round #341 (Div. 2) 总结

A. Wet Shark and Odd and Even
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Today, Wet Shark is given n integers. Using any of these integers no more than once, Wet Shark wants to get maximum possible even (divisible by 2) sum. Please, calculate this value for Wet Shark.

Note, that if Wet Shark uses no integers from the n integers, the sum is an even integer 0.

Input

The first line of the input contains one integer, n (1 ≤ n ≤ 100 000). The next line contains n space separated integers given to Wet Shark. Each of these integers is in range from 1 to 109, inclusive.

Output

Print the maximum possible even sum that can be obtained if we use some of the given integers.

Sample test(s)
input
3
1 2 3
output
6
input
5
999999999 999999999 999999999 999999999 999999999
output
3999999996
Note

In the first sample, we can simply take all three integers for a total sum of 6.

In the second sample Wet Shark should take any four out of five integers 999 999 999.

题意:求所给n个数中,远一些数,组成的最大偶数是多少。

解:来一个for循环,把全部数累加起来,再算算这n个数中有多少个是奇数。最后,要是奇数的个数是奇数个,则sum-=min(odd).否则,sum=sum;

<span style="font-size:14px;">#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL __int64
const LL maxm=1e5+10;
LL a[maxm];
int main()
{
    LL n;
    while(scanf("%I64d",&n)!=EOF)
    {
        LL s1=0;
        LL sum=0;
        LL Mi=1e9+10;
        for(LL i=0;i<n;i++)
        {
            scanf("%I64d",&a[i]);
            if(a[i]&1)
            {
                s1++;
                Mi=min(Mi,a[i]);
            }
            sum+=a[i];
        }
        if(s1&1)
        {
            sum-=Mi;
        }
        printf("%I64d\n",sum);
    }
    return 0;
}</span>

B. Wet Shark and Bishops
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 test(s)
input
5
1 1
1 5
3 3
5 1
5 5
output
6
input
3
1 1
2 3
3 5
output
0
Note

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.

题意:有一个1000*1000的网格,给出n个象的点,对角线上象之间两两都可以攻击,问一共有多少攻击组合

解:只要是对角线就满足x+y=k(k=0,1,2....)或者x-y=k(k=0,1,2....))  所以只要统计出每一组对角线上有多少个点然后套公式就可以出来了

<span style="font-size:14px;">#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm=1e6+10;
int x[maxm];
int y[maxm];
int dp1[maxm];
int dp2[maxm];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        memset(dp1,0,sizeof(dp1));
        memset(dp2,0,sizeof(dp2));
        int sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&x[i],&y[i]);
            dp1[x[i]+y[i]]++;
            dp2[x[i]-y[i]+1000]++;
        }
        for(int i=0;i<=2000;i++)
        {
            sum+=((dp1[i]-1)*(dp1[i]-1)-((dp1[i]-1)*(dp1[i]-2))/2);
            sum+=((dp2[i]-1)*(dp2[i]-1)-((dp2[i]-1)*(dp2[i]-2))/2);
        }
        printf("%d\n",sum);
    }
    return 0;
}</span>

C. Wet Shark and Flowers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 test(s)
input
3 2
1 2
420 421
420420 420421
output
4500.0
input
3 5
1 4
2 3
11 14
output
0.0
Note

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. (1, 420, 420420): note that s0·s1 = 420s1·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.
  2. (1, 420, 420421): now, the product s2·s0 is not divisible by 2. Therefore, sharks s0 and s2 will receive 1000 dollars, while shark s1will receive 2000. The total is 4000.
  3. (1, 421, 420420): total is 4000
  4. (1, 421, 420421): total is 0.
  5. (2, 420, 420420): total is 6000.
  6. (2, 420, 420421): total is 6000.
  7. (2, 421, 420420): total is 6000.
  8. (2, 421, 420421): total is 4000.

The expected value is .

In the second sample, no combination of quantities will garner the sharks any money.

题意:给你n个区间和一个数k,求当前区间取一个数*下一个区间取一个数能够整除k的期望。。


解:经典概率问题,一个区间内能整除k的概率为p[i]=(r/k-(l-1)/k)/(r-l+1),所以说满足条件的概率为

P=p[i]*p[i+1]+p[i]*(1-p[i+1])+(1-p[i])*p[i+1],化简后就是P=p[i]+p[i+1]-p[i]*p[i+1]然后累加P*2000即可,注意可成环,即第n个区间的下一个为第一个。。

<span style="font-size:14px;">#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm=1e5+10;
double p[maxm];
int main()
{
    int n,k;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            int l,r;
            scanf("%d%d",&l,&r);
            int x=(r/k-(l-1)/k);
            p[i]=(double)x/(r-l+1);
        }
        p[n]=p[0];
        double ans=0;
        for(int i=0;i<n;i++)
        {
            ans+=(p[i]+p[i+1]-p[i]*p[i+1])*2000.0;
        }
        printf("%lf\n",ans);
    }
    return 0;

}</span>


你可能感兴趣的:(Codeforces Round #341 (Div. 2) 总结)