Codeforces Round #341 (Div. 2)题解

A. Wet Shark and Odd and Even

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个正数,每个数最多加1次,求最大的偶数和
思路
先求出n个数的和,如果和是奇数,就减去最小的那个奇数,然后输出;如果和是偶数就直接输出

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#define N 100010
#define ll long long
using namespace std;
int a[N], n;
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt", "r", stdin);
#endif
    int i, t = 1000000001;
    ll sum = 0;;
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
        sum += a[i];
        if (a[i]&1) t = min(t, a[i]);
    }
    if (sum&1)  sum -= t;
    cout << sum;
    return 0;
}

B. Wet Shark and Bishops

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.

题意
有n个bishops(我也不知道bishops是什么鬼)在一个1000*1000的网格上,如果两个bishops在同一个对角线上,他们之间就会相互攻击,现在给出这n个bishops的位置,问有多少对bishops 相互攻击?
思路
只有x1+y1==x2+y2或x1-y1==x2-y2时,(x1, y1)和(x2, y2)在一条对角线上,所以我们只需要用两个数组a和b,来统计x+y=i的坐标有多少,x-y+1000=i(x-y有可能是负的,所以需要加1000) 的坐标有多少每个对角线上相互攻击的数目为a[i]*(a[i]-1)/2
具体代码如下:

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#define N 2020
#define ll long long
using namespace std;
int a[N], b[N], n;
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt", "r", stdin);
#endif
    int i, j, k, tt, x, y;
    ll ans = 0l, t;
    scanf("%d", &n);
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    for (i = 0; i < n; i++)
    {
        scanf("%d%d", &x, &y);
        a[x+y]++;
        b[x-y+1000]++;
    }
    for (i = 0; i < N; i++)
        ans += a[i]*(a[i]-1)/2 + b[i]*(b[i]-1)/2;
    printf("%I64d", ans);
    return 0;
}

C. Wet Shark and Flowers

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 ·si 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 = 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.
  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 s1 will 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个sharks 种花,每个sharks 种的话价值si, si在li和ri 直接随机取值,如果si*si+1可以被p整除,第i个和第i+1个shark将会得到1000 dollars,求最后所得钱 的期望是多少
思路
先求出si可以被p整除的概率a[i], 最后的结果就是所有的a[i]+a[i+1]-a[i]*a[i+1]*2000 的和(减去的是重复的部分)

#include <bits/stdc++.h>
#define N 100010
using namespace std;
double a[N];
int main()
{
    int n, i, j, l, r, p;
    scanf("%d%d", &n, &p);
    for (i =  0; i < n; i++)
    {
        scanf("%d%d", &l, &r);
        a[i] = (double)(r/p - (l-1)/p)/(r-l+1);
    }
    a[n] = a[0];
    double ans = 0;
    for (i = 0; i < n; i++)
        ans += (a[i] + a[i+1] - a[i] * a[i+1])*2000;
    printf("%.10lf", ans);
    return 0;
}

D. Rat Kwesh and Cheese

Wet Shark asked Rat Kwesh to generate three positive real numbers x, y and z, from 0.1 to 200.0, inclusive. Wet Krash wants to impress Wet Shark, so all generated numbers will have exactly one digit after the decimal point.

Wet Shark knows Rat Kwesh will want a lot of cheese. So he will give the Rat an opportunity to earn a lot of cheese. He will hand the three numbers x, y and z to Rat Kwesh, and Rat Kwesh will pick one of the these twelve options:
1. a1 = xyz;
2. a2 = xzy;
3. a3 = (xy)z;
4. a4 = (xz)y;
5. a5 = yxz;
6. a6 = yzx;
7. a7 = (yx)z;
8. a8 = (yz)x;
9. a9 = zxy;
10. a10= zyx;
11. a11 = (zx)y;
12. a12 = (zy)x.

Let m be the maximum of all the ai, and c be the smallest index (from 1 to 12) such that ac = m. Rat’s goal is to find that c, and he asks you to help him. Rat Kwesh wants to see how much cheese he gets, so he you will have to print the expression corresponding to that ac.

Input

The only line of the input contains three space-separated real numbers x, y and z (0.1 ≤ x, y, z ≤ 200.0). Each of x, y and z is given with exactly one digit after the decimal point.

Output

Find the maximum value of expression among xyz, xzy, (xy)z, (xz)y, yxz, yzx, (yx)z, (yz)x, zxy, zyx, (zx)y, (zy)x and print the corresponding expression. If there are many maximums, print the one that comes first in the list.

xyz should be outputted as x^y^z (without brackets), and (xy)z should be outputted as (x^y)^z (quotes for clarity).

Sample test(s)

Input
1.1 3.4 2.5
Output
z^y^x
Input
2.0 2.0 2.0
Output
x^y^z
Input
1.9 1.8 1.7
Output
(x^y)^z

题意
求a1到a12里面的最大并且下标最小的数,并输出那个表达式
如果是xyz 就输出 x^y^z
如果是 (xy)z就输出(x^y)^z

思路
如果直接计算xyz,它的最大值是20040000这个数是任何类型的数都放不下的。由于只是比较大小,并不需要具体的值,我们只要对xyz 取两次对数就行
log(log(xyz))=log(yz*log(x))=z*log(y)+log(log(x))
log(log((xy)z))=log(z)+log(y)+log(log(x))
这样我们就可以比较他们之间的大小了
不过还有一个地方需要注意,如果x<1, 则log(x)<0,那么log(log(x))就是nan了,这没有意义。如果x<1, xa<1 (a>0);如果x>1, xa>1 (a>0)。所以,当x, y, z中有一个,或有两个数小于1(假设x<1),此时的最大值都不可能是 xyz,  xzy, (xy)z,(xz)y。我们直接将a[1], a[2], a[3], a[4]忽略就行了
但如果x, y, z, 都小于1,加入x=0.2, 0.2yz=(1/5)yz=1/(5yz)
这就转化为求5yz。把所有的数都转化为这样是数,然后我们只需要在里面求最小数(它的倒数是最大值)

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#define N 1010
using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt", "r", stdin);
#endif
    double x, y, z, xx, yy, zz, a[15];
    int i;
    bool flag = false, f[15];
    cin >> x >> y >> z;
    for (i = 0; i < 13; i++)    f[i] = true;
    if (x < 1 && y < 1 && z < 1)
    {
        flag = true;
        xx = 1.0/x;
        yy = 1.0/y;
        zz = 1.0/z;
        a[1] = z*log(y)+log(log(xx));
        a[2] = y*log(z)+log(log(xx));
        a[3] = log(z)+log(y)+log(log(xx));
        a[4] = log(y)+log(z)+log(log(xx));
        a[5] = z*log(x)+log(log(yy));
        a[6] = x*log(z)+log(log(yy));
        a[7] = log(log(yy))+log(x)+log(z);
        a[8] = log(log(yy))+log(x)+log(z);
        a[9] = y*log(x)+log(log(zz));
        a[10] = x*log(y)+log(log(zz));;
        a[11] = log(x)+log(y)+log(log(zz));
        a[12] = log(y)+log(x)+log(log(zz));
    }
    else
    {
        if (x < 1)
        {
            f[1] = f[2] = f[3] = f[4] = false;
        }
        else
        {
            a[1] = z*log(y)+log(log(x));
            a[2] = y*log(z)+log(log(x));
            a[3] = log(z)+log(y)+log(log(x));
            a[4] = log(y)+log(z)+log(log(x));
        }
        if (y < 1)
        {
            f[5] = f[6] = f[7] = f[8] = false;
        }
        else
        {
            a[5] = z*log(x)+log(log(y));
            a[6] = x*log(z)+log(log(y));
            a[7] = log(log(y))+log(x)+log(z);
            a[8] = log(log(y))+log(x)+log(z);
        }
        if (z < 1)
        {
            f[9] = f[10] = f[11] = f[12] = false;
        }
        else
        {
            a[9] = y*log(x)+log(log(z));
            a[10] = x*log(y)+log(log(z));;
            a[11] = log(x)+log(y)+log(log(z));
            a[12] = log(y)+log(x)+log(log(z));
        }
    }

    int c = 1;
    if (flag)   //全部都小于1时,找最小值 
    {
        for (i = 2; i <= 12; i++)
            if (a[c] > a[i])    c = i;
    }
    else
    {
        for (i = 1; i <= 12; i++)
            if (f[i])
            {
                c = i;
                break;
            }
        for (i++; i <= 12; i++)
            if (f[i] && a[c] < a[i])    c = i;
    }

    switch(c)
    {
        case 1: cout << "x^y^z"; break;
        case 2: cout << "x^z^y"; break;
        case 3: cout << "(x^y)^z"; break;
        case 4: cout << "(x^z)^y"; break;
        case 5: cout << "y^x^z"; break;
        case 6: cout << "y^z^x"; break;
        case 7: cout << "(y^x)^z"; break;
        case 8: cout << "(y^z)^x"; break;
        case 9: cout << "z^x^y"; break;
        case 10: cout << "z^y^x"; break;
        case 11: cout << "(z^x)^y"; break;
        case 12: cout << "(z^y)^x"; break;
    }
    return 0;
}

E. Wet Shark and Blocks

There are b blocks of digits. Each one consisting of the same n digits, which are given to you in the input. Wet Shark must choose exactly one digit from each block and concatenate all of those digits together to form one large integer. For example, if he chooses digit 1 from the first block and digit 2 from the second block, he gets the integer 12.

Wet Shark then takes this number modulo x. Please, tell him how many ways he can choose one digit from each block so that he gets exactly k as the final result. As this number may be too large, print it modulo 109 + 7.

Note, that the number of ways to choose some digit in the block is equal to the number of it’s occurrences. For example, there are 3 ways to choose digit 5 from block 3 5 6 7 8 9 5 1 1 1 1 5.

Input

The first line of the input contains four space-separated integers, n, b, k and x (2 ≤ n ≤ 50 000, 1 ≤ b ≤ 109, 0 ≤ k < x ≤ 100, x ≥ 2) — the number of digits in one block, the number of blocks, interesting remainder modulo x and modulo x itself.

The next line contains n space separated integers ai (1 ≤ ai ≤ 9), that give the digits contained in each block.

Output

Print the number of ways to pick exactly one digit from each blocks, such that the resulting integer equals k modulo x.

Sample test(s)

Input
12 1 5 10
3 5 6 7 8 9 5 1 1 1 1 5
Output
3
Input
3 2 1 2
6 2 2
Output
0
Input
3 2 1 2
3 1 2
Output
6
Note
In the second sample possible integers are 22, 26, 62 and 66. None of them gives the remainder 1 modulo 2.
In the third sample integers 11, 13, 21, 23, 31 and 33 have remainder 1 modulo 2. There is exactly one way to obtain each of these integers, so the total answer is 6.

题意
给n个数作为一个块,一共有b个块,从每个快中选一个数,最后组成一个数模x等于k的方法数。
思路:DP+快速矩阵幂
用dp[i][j]表示现在i位,余数是j。那么很容易知道,边界是d[0][0] = 1;那么dp[i][j] = sum(dp[i-1][a] * cnt[k]),其中a是(a*10 + k)%x == j,k是指枚举放1~9中哪一位,cnt[k]就是k在n个数中出现的次数。
但是因为b特别大,这就需要进行矩阵优化,知道了DP方程,其实矩阵的构造特别简单。 根据矩阵相乘的规则, 其实这个矩阵就是A[i][(j*10+cnt[k])%x]++,其中cnt[k]是k取1~n的每个数字。

#include <bits/stdc++.h>
#define N 102
#define ll long long
using namespace std;
const int mod=1000000007;
int n, block, r, x, cnt[15];
ll a[N][N] ,b[N][N], t[N][N];
void mul(ll aa[N][N], ll bb[N][N], ll cc[N][N])
{
 int i, j, k;
 for (i = 0; i < x; i++)
 {
 for (j = 0; j < x; j++)
 {
 t[i][j] = 0;
 for (k = 0; k < x; k++)
 t[i][j] += aa[i][k]*bb[k][j]%mod;
 }
 }
 for (i = 0; i < x; i++)
 for (j = 0; j < x; j++)
 cc[i][j] = t[i][j]%mod;
}
int main()
{
#ifndef ONLINE_JUDGE
//  freopen("1.txt", "r", stdin);
#endif
 int i, j, k;
 scanf("%d%d%d%d", &n, &block, &r, &x);
 memset(cnt, 0, sizeof(cnt));
 for (i = 0; i < n; i++)
 {
 scanf("%d", &k);
 cnt[k]++;
 }
 for (i = 0; i < x; i++)
 {
 a[i][i] = 1;
 for (k = 1; k <= 9; k++)
 {
 j = (i*10+k)%x;
 b[i][j] += cnt[k];
 }
 }
 while(block)
 {
 if (block&1) mul(a, b, a);
 mul(b, b, b);
 block >>= 1;
 }
 printf("%I64d", a[0][r]);
 return 0;
}

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