Codeforces Round #594 (Div. 2)

Posted misuchii

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #594 (Div. 2)相关的知识,希望对你有一定的参考价值。

Codeforces Round #594 (Div. 2)

A. Integer Points

  • 题意:给出若干个(y = x + p)(y = -x + q) 求它们交点坐标为整数的个数

  • 思路:(y = x + p)(y = -x + q) 的交点为 (left(frac{q - p}{2}, frac{p + q}{2} ight)) 即只用统计p和q数组中的奇偶数即可

  • AC代码


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll mult_mod(ll x, ll y, ll mod){
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}

ll pow_mod(ll a, ll b, ll p){
    ll res = 1;
    while (b){
        if (b & 1)
            res = mult_mod(res, a, p);
        a = mult_mod(a, a, p);
        b >>= 1;
    }
    return res % p;
}

ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}

const int N = 1e5 + 10;

int t, n, m;
ll cnt1, cnt2, cnt3, cnt4, ans;
ll p[N], q[N];

int main(){
//    freopen("my_in.txt", "r", stdin);
//    freopen("my_out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> t;
    while (t -- ){
        ans = 0, cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0;
        cin >> n;
        for (int i = 1; i <= n; i ++ ){
            cin >> p[i];
            if (p[i] & 1)
                cnt1 ++ ;
            else
                cnt2 ++ ;
        }
        cin >> m;
        for (int i = 1; i <= m; i ++ ){
            cin >> q[i];
            if (q[i] & 1)
                cnt3 ++ ;
            else
                cnt4 ++ ;
        }
        ans = cnt1 * cnt3 + cnt2 * cnt4;
        cout << ans << "
";
    }
    return 0;
}

B. Grow The Tree

  • 题意:给出若干长度的线段 构成一个折线 要求折线的起点到终点的最长距离

  • 思路:先排序 排序后以前一半作为所求线段在(x)轴的投影 以后一半作为所求线段在(y)轴的投影 这样求出来的线段长度是最大的

  • AC代码


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll mult_mod(ll x, ll y, ll mod){
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}

ll pow_mod(ll a, ll b, ll p){
    ll res = 1;
    while (b){
        if (b & 1)
            res = mult_mod(res, a, p);
        a = mult_mod(a, a, p);
        b >>= 1;
    }
    return res % p;
}

ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}

const int N = 1e5 + 10;

int n;
int a[N];
ll x, y, ans;

int main(){
//    freopen("my_in.txt", "r", stdin);
//    freopen("my_out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i ++ )
        cin >> a[i];
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n / 2; i ++ )
        x += a[i];
    for (int i = n / 2 + 1; i <= n; i ++ )
        y += a[i];
    ans = x * x + y * y;
    cout << ans << "
";
    return 0;
}

C. Ivan the Fool and the Probability Theory

  • 题意:给出一个(n * m)的方格 要在尽可能多的小格子中填入东西 每个小格子最多能有一个相邻的格子被填充 问一共有多少种填法

  • 思路:
    egin{tabular}{|l|ccc|}
    hline
    diagbox{n}{ans}{m} & 1 & 2 & 3 & 4 & (dots) & n
    hline
    1 & 1 & 2 & 3 & 5 & (dots) & (fleft(n - 1 ight) + fleft(n - 2 ight))
    2 & 2 & 6 & 8 & 12 & (dots) & (dots)
    3 & 3 & 8 & 10 & 14 & (dots) & (dots)
    4 & 5 & 12 & 14 & 18 & (dots) & (dots)
    (dots) & (dots) & (dots) & (dots) & (dots) & (dots) & (dots)
    m & (fleft(m - 1 ight) + fleft(m - 2 ight)) & (dots) & (dots) & (dots) & (dots) & (2 * left(fleft(n - 1 ight) + fleft(n - 2 ight) + fleft(m - 1 ight) + fleft(m - 2 ight) - 1 ight))
    hline
    end{tabular}

  • AC代码


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll mult_mod(ll x, ll y, ll mod){
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}

ll pow_mod(ll a, ll b, ll p){
    ll res = 1;
    while (b){
        if (b & 1)
            res = mult_mod(res, a, p);
        a = mult_mod(a, a, p);
        b >>= 1;
    }
    return res % p;
}

ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}

const int mod = 1e9 + 7;
const int N = 1e5 + 10;

int n, m;
ll ans;
ll f[N];

void init(){
    f[1] = 1, f[2] = 2;
    for (int i = 3; i < N; i ++ )
        f[i] = (f[i - 1] + f[i - 2]) % mod;
}

int main(){
//    freopen("my_in.txt", "r", stdin);
//    freopen("my_out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    init();
    cin >> n >> m;
    ans = mult_mod(2, ((f[n] + f[m]) % mod - 1 + mod) % mod, mod);
    cout << ans << "
";
    return 0;
}

以上是关于Codeforces Round #594 (Div. 2)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #594 (Div. 2) C. Ivan the Fool and the Probability Theory

Codeforces Round #436 E. Fire(背包dp+输出路径)

[ACM]Codeforces Round #534 (Div. 2)

CF Round 594

codeforces 594

Codeforces 594D REQ 线段树