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}\right)\) 即只用统计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 << "\n";
    }
    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 << "\n";
    return 0;
}

C. Ivan the Fool and the Probability Theory

  • 题意:给出一个\(n * m\)的方格 要在尽可能多的小格子中填入东西 每个小格子最多能有一个相邻的格子被填充 问一共有多少种填法
  • 思路:
    \begin{tabular}{|l|ccc|}
    \hline
    \diagbox{n}{ans}{m} & 1 & 2 & 3 & 4 & \(\dots\) & n \
    \hline
    1 & 1 & 2 & 3 & 5 & \(\dots\) & \(f\left(n - 1\right) + f\left(n - 2\right)\) \
    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 & \(f\left(m - 1\right) + f\left(m - 2\right)\) & \(\dots\) & \(\dots\) & \(\dots\) & \(\dots\) & \(2 * \left(f\left(n - 1\right) + f\left(n - 2\right) + f\left(m - 1\right) + f\left(m - 2\right) - 1\right)\) \
    \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 << "\n";
    return 0;
}

原文地址:https://www.cnblogs.com/Misuchii/p/11716610.html

时间: 2024-11-07 15:16:55

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

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

题目链接:https://codeforces.com/contest/1248/problem/C 赛后补的,已经有很详细的题解了,这里只作一个记录. 每相邻两行只可能完全相同或者完全不同,而每一行只有1010101010……和01010101010……是没有连续相邻的(有连续相邻的下一行只能完全不同,即有连续相邻的从第一行开始后面的就已经固定了),n=1时答案是递推数列,F[n] = F[n-1] + F[n-2].当第一行为10101010……或0101010101时每一行只有两种状态,此

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

题目原址:C. Ivan the Fool and the Probability Theory 题意:n×m的网格中填黑白格,最多有两个相邻,共用一条边为相邻,有几种填法. 思路:这题比赛写自闭了,一直觉得自己的思路没错,然后就残酷打脸,超级难受的那种. 从不能有3个及以上相邻可以得出,只要有两个相邻的就能确定全部的分布,那就变成了求一行的方法数量,再减去黑白相间的两种,而剩下就是黑白相间两种情况时有多少种了. 一行的求法:dp[ i ] [ 0 ]表示在第 i 个格子里放白色的放法有多少,d

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我

[Codeforces] Round #352 (Div. 2)

人生不止眼前的狗血,还有远方的狗带 A题B题一如既往的丝帛题 A题题意:询问按照12345678910111213...的顺序排列下去第n(n<=10^3)个数是多少 题解:打表,输出 1 #include<bits/stdc++.h> 2 using namespace std; 3 int dig[10],A[1005]; 4 int main(){ 5 int aa=0; 6 for(int i=1;;i++){ 7 int x=i,dd=0; 8 while(x)dig[++dd

Codeforces Round #273 (Div. 2)

Codeforces Round #273 (Div. 2) 题目链接 A:签到,仅仅要推断总和是不是5的倍数就可以,注意推断0的情况 B:最大值的情况是每一个集合先放1个,剩下都丢到一个集合去,最小值是尽量平均去分 C:假如3种球从小到大是a, b, c,那么假设(a + b) 2 <= c这个比較明显答案就是a + b了.由于c肯定要剩余了,假设(a + b)2 > c的话,就肯定能构造出最优的(a + b + c) / 3,由于肯定能够先拿a和b去消除c,而且控制a和b成2倍关系或者消除

Codeforces Round #339 (Div. 2) B. Gena&#39;s Code

B. Gena's Code It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, f

Codeforces Round #315 (Div. 1)

A. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and un