Codeforces Round #341 (Div. 2) ABCDE

http://www.cnblogs.com/wenruo/p/5176375.html

A. Wet Shark and Odd and Even

题意:输入n个数,选择其中任意个数,使和最大且为奇数。

题解:算出所有数的和,如果奇数的个数为奇数个,则减去最小的奇数,否则不用处理。

#include <bits/stdc++.h>
using namespace std;
#define PI acos(-1.0)
#define EXP exp(1.0)
#define ESP 1E-6
#define clr(x,c) memset(x,c,sizeof(x))
typedef long long ll;

using namespace std;
int main()
{
    int n;
    int cnt = 0; int minn = 1000000000;
    int val;
    ll ans = 0;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &val);ans += val;
        if (val % 2 == 1) {
            cnt++;
            minn = min(minn, val);
        }
    }
    if (cnt % 2 == 1) ans -= minn;
    cout << ans;
    return 0;
}

B. Wet Shark and Bishops

题意:一个1000*1000的棋盘,上面摆放n个棋子,同一对角线上的两个棋子可以相互攻击,求一共有多少对棋子可以相互攻击。

题解:有主对角线和副对角线,对于每一个对角线,设对角线上有棋子X个,则该对角线有C(X,2)对棋子可以相互攻击。

#include <bits/stdc++.h>
#define PI acos(-1.0)
#define EXP exp(1.0)
#define ESP 1E-6
#define clr(x,c) memset(x,c,sizeof(x))
typedef long long ll;

using namespace std;

int xSubY[2005];
int xAddY[2005];
ll c[1005][1005];
//c[i][j] := C(i, j)
void init()
{
	c[0][0] = 1;
    for(int i = 1; i <= 1000; i++)
    {
        c[i][0] = 1;
        c[i][i] = 1;
        for(int j = 1; j < i; j++)
            c[i][j] = (c[i-1][j-1] + c[i-1][j]);
    }
}

int main()
{
    init();
    int n;
    int x, y;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d%d", &x, &y);
        xSubY[x-y+1000]++;
        xAddY[x+y]++;
    }
    ll ans = 0;
    for (int i = 0; i <= 2000; ++i) {
        ans += c[ xSubY[i] ][2];
        ans += c[ xAddY[i] ][2];
    }
    cout << ans;
    return 0;
}

C. Wet Shark and Flowers

题意:n个鲨鱼围成一圈1~n,每个鲨鱼有一个数,鲨鱼i的数字在范围l[i]~r[i],如果相邻数字的乘积为P或P的倍数(p为素数),则两个鲨鱼各获得1000元,求一共能获得钱的期望。

题解:分别考虑相邻的每两个就可以,对于相邻两个数,只有至少一个为p的倍数,才有可能乘积是p的倍数。对于每个鲨鱼一共有r-l+1个数字,有r/p-(l-1)/p的p的倍数。具体看代码。

#include <bits/stdc++.h>
#define PI acos(-1.0)
#define EXP exp(1.0)
#define ESP 1E-6
#define clr(x,c) memset(x,c,sizeof(x))
typedef long long ll;

using namespace std;
const int N = 100005;

int l[N];
int r[N];
double ans[N];

int main()
{
    int n, p;
    scanf("%d%d", &n, &p);
    for (int i = 0; i < n; ++i) {
        scanf("%d%d", &l[i], &r[i]);
    }
    l[n] = l[0]; r[n] = r[0];
    for (int i = 0; i < n; ++i) {
        double a = r[i] - l[i] + 1;
        double b = r[i + 1] - l[i + 1] + 1;
        double pa = r[i] / p - (l[i] - 1) / p;
        double pb = r[i + 1] / p - (l[i + 1] - 1) / p;
        double tmp = (pa * b + pb * a - pa * pb) / (a * b);
        ans[i] += tmp;
        ans[i + 1] += tmp;
    }
    double res = 0;
    for (int i = 0; i <= n; ++i) res += ans[i];
    printf("%f", res * 1000);

    return 0;
}

D. Rat Kwesh and Cheese

题意:很简单,求a1 = xyz;a2 = xzy;a3 = (xy)z;a4 = (xz)y;a5 = yxz;a6 = yzx;a7 = (yx)z;a8 = (yz)x;a9 = zxy;a10 = zyx;a11 = (zx)y;a12 = (zy)x.的最大值。(0.1 ≤ x, y, z ≤ 200.0)

题解:很容易想到求对数,但是,y^z也会很大,所以想当然的求了两次对数,也就是log(log(x^y^z))变成z*log(y)+log(log(x));但是log(x)会出现负数……(后来发现好多人都是这么做的,笑)。正解是取一次对数,然后使用long double,精度足够。

#include <bits/stdc++.h>
typedef long double lld;
using namespace std;
const lld ESP = 1E-8;

lld cal(lld x, lld y, lld z)
{
    //return z * log(y) + log(log(x));
    return log(x) * pow(y, z);
}

lld ca(lld x, lld y, lld z)
{
    //return log(y * z * log(x));
    return y * z * log(x);
}

int main()
{
    const char* result[12] = {"x^y^z", "x^z^y", "(x^y)^z", "(x^z)^y", "y^x^z",
                        "y^z^x", "(y^x)^z", "(y^z)^x", "z^x^y", "z^y^x", "(z^x)^y", "(z^y)^x"};

    lld a[20];
    lld x, y, z;
    cin >> x >> y >> z;
    a[0] = cal(x, y, z);
    a[1] = cal(x, z, y);
    a[2] = ca(x, y, z);
    a[3] = ca(x, z, y);
    a[4] = cal(y, x, z);
    a[5] = cal(y, z, x);
    a[6] = ca(y, x, z);
    a[7] = ca(y, z, x);
    a[8] = cal(z, x, y);
    a[9] = cal(z, y, x);
    a[10] = ca(z, x, y);
    a[11] = ca(z, y, x);
    lld maxn = a[0];
    int res = 0;
    for (int i = 1; i < 12; ++i) {
        if (maxn < a[i] - ESP) {
            maxn = a[i];
            res = i;
        }
    }
    printf("%s", result[res]);
    return 0;
}

E. Wet Shark and Blocks

题意:n个堆,每个堆有b个数字,每一个堆取一个数字,第i个堆取一个数字做第i位的数字,构成一个n位数,求构成的数字被x除余k有多少种方案。

题解:看到这题就想到了前几天做的题http://www.cnblogs.com/wenruo/p/5153790.html 不过这个简单些,然并卵,比赛时并没有写出来……dp+矩阵快速幂

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int digit[10];
const ll MOD = 1000000007;

typedef vector<ll> vec;
typedef vector<vec> mat;

mat mul(mat &A, mat &B)
{
    mat C(A.size(), vec(B[0].size()));
    for (unsigned int i = 0; i < A.size(); ++i) {
        for (unsigned int k = 0; k < B.size(); ++k) {
            for (unsigned int j = 0; j < B[0].size(); ++j) {
                C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD;
            }
        }
    }
    return C;
}

mat pow(mat A, int n)
{
    mat B(A.size(), vec(A.size()));
    for (unsigned int i = 0; i < A.size(); ++i)
        B[i][i] = 1;
    while (n > 0) {
        if (n & 1) B = mul(B, A);
        A = mul(A, A);
        n >>= 1;
    }
    return B;
}

/*
dp[i][k] i位数 余数为k
dp[i+1][(j+k*10)%x] = dp[i][k]*digit[j], 1<=j<=9
*/
ll cal(int n, int x, int k)
{
    mat f(x, vec(x));
    for (int i = 0; i < x; ++i) {           // 上一位余数
        for (int j = 1; j <= 9; ++j) {      // 首位数字
            int num = (i * 10 + j) % x;     // 当前余数
            f[num][i] += digit[j];
        }
    }

    mat v(x, vec(1));
    for (int i = 1; i <= 9; ++i)
        v[i % x][0] += digit[i];    // dp[1][i%x][i]

    f = pow(f, n - 1);
    v = mul(f, v);
    return v[k][0];
}

int main()
{
    int n, b, k, x;
    scanf("%d%d%d%d", &n, &b, &k, &x);
    int val;
    for (int i = 0; i < n; ++i) {
        scanf("%d", &val);
        digit[val]++;
    }
    cout << cal(b, x, k);

    return 0;
}

总体而言这次的比赛比较简单^_^

时间: 2024-10-25 03:58:15

Codeforces Round #341 (Div. 2) ABCDE的相关文章

Codeforces Round #261 (Div. 2)[ABCDE]

Codeforces Round #261 (Div. 2)[ABCDE] ACM 题目地址:Codeforces Round #261 (Div. 2) A - Pashmak and Garden 题意: 一个正方形,它的边平行于坐标轴,给出这个正方形的两个点,求出另外两个点. 分析: 推断下是否平行X轴或平行Y轴,各种if. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * File: A.cpp * Create Date: 2014-0

Codeforces Round #264 (Div. 2)[ABCDE]

Codeforces Round #264 (Div. 2)[ABCDE] ACM 题目地址: Codeforces Round #264 (Div. 2) 这场只出了两题TAT,C由于cin给fst了,D想到正解快敲完了却game over了... 掉rating掉的厉害QvQ... A - Caisa and Sugar[模拟] 题意: Caisa拿s美元去超市买sugar,有n种sugar,每种为xi美元yi美分,超市找钱时不会找美分,而是用sweet代替,当然能用美元找就尽量用美元找.他

Codeforces Round #260 (Div. 2) ABCDE

A题逗比了,没有看到All ai are distinct. All bi are distinct. 其实很水的.. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 #define mnx 100002 8 9 10 struct latop{ 11 int p, q; 12 bo

Codeforces Round #531 (Div. 3) ABCDE题解

Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividing 题意: 给一个数n,然后要求你把1,2.....n分为两个集合,使得两个集合里面元素的和的差的绝对值最小. 题解: 分析可以发现,当n%4==0 或者 n%3==0,答案为0:其余答案为1.之后输出一下就好了. 代码如下: #include <bits/stdc++.h> using name

Codeforces Round #200 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/344 A. Magnets time limit per test:1 second memory limit per test:256 megabytes Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets inst

Codeforces Round #105 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/148 比较简单的一场,最长的一题也才写了30行多一点 A. Insomnia cure time limit per test:2 seconds memory limit per test:256 megabytes ?One dragon. Two dragon. Three dragon?, - the princess was counting. She had trouble falling asleep, and

Codeforces Round #186 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/313 A. Ilya and Bank Account time limit per test:2 seconds memory limit per test:256 megabytes Ilya is a very clever lion, he lives in an unusual city ZooVille. In this city all the animals have their rights and obl

Codeforces Round #296 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/527 A. Playing with Paper time limit per test:2 seconds memory limit per test:256 megabytes One day Vasya was sitting on a not so interesting Maths lesson and making an origami from a rectangular a mm ?×? b mm sheet

Codeforces Round #240 (Div. 2) (ABCDE题解)

题目链接:http://codeforces.com/contest/415 A. Mashmokh and Lights time limit per test:1 second memory limit per test:256 megabytes Mashmokh works in a factory. At the end of each day he must turn off all of the lights. The lights on the factory are index