codeforce #925 (div3) 题解

这篇具有很好参考价值的文章主要介绍了codeforce #925 (div3) 题解。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

D. Divisible Pairs

给出数组 a a a,如果二元组 ( i , j ) (i,j) (i,j)满足 a i + a j m o d x = = 0 & & a i − a j m o d y = = 0 a_i + a_j mod x ==0 \&\& a_i - a_j mod y == 0 ai+ajmodx==0&&aiajmody==0,则beauty。其中 i < j i<j i<j

根据题意不难得出,符合条件的二元组应满足
a i m o d    x + a j m o d    x = x a i m o d    y = a j m o d    y a_i \mod x + a_j \mod x = x \\ a_i \mod y = a_j \mod y aimodx+ajmodx=xaimody=ajmody

所以用 ( a i m o d    x , a i m o d    y ) (a_i \mod x, a_i \mod y) (aimodx,aimody)作为key,对于每个元素 a i a_i ai查找 ( x − a i m o d    x , a i m o d    y ) (x- a_i \mod x, a_i \mod y) (xaimodx,aimody)的个数

#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <time.h>
#include <set>
#include <map>
#include <queue>

#define IOS     ios::sync_with_stdio(0);cin.tie(0);
#define rep(index,start,end) for(int index = start;index < end; index ++)
#define drep(index,start,end) for(int index = start;index >= end; index --)

using namespace std;

typedef long long ll;
ll gcd(ll a, ll b){
    ll t;
    while(b){
        t = b;
        b = a % b;
        a = t;
    }
    return a;
}

const int maxn = 2e5+5;


typedef pair<int, int> key;
int n,x,y;
map<key, int> store;
int main() {
    IOS
    
    int t;
    cin>>t;
    while(t--) {
        store.clear();
        
        cin>>n>>x>>y;
        int tmp;
        ll sum = 0LL;
        rep(i,0,n) {
            cin>>tmp;
            int modx = tmp % x;
            int mody = tmp % y;
            key now ={modx, mody};
            
            // cal
            int bmody = tmp % y;
            int bmodx = (x - tmp%x) % x;
            sum += store[{bmodx, bmody}];
            
            
            if (store.find(now) != store.end()) {
                store[now] += 1;
            } else {
                store[now] = 1;
            }
        }

        cout<<sum<<endl;
    }
    return 0;
}

E. Anna and the Valentine’s Day Gift 博弈论

俩人玩游戏,一个能选一个数reverse,一个能选一个数拼接,看最后的结果能不能大于 1 0 m 10^m 10m

如果想要减少最终结果的位数,那么必须reverse之后产生前导零,例如10000,反转后变成1。那么这道题就变成了对反转后产生前导零个数的排序。注意我们的对手不傻,当我们把产生最多前导零的数字反转后,对手肯定会把产生第二多的拼接保护,防止最终结果位数减少,所以只能减去排序结果的偶数位

#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <time.h>
#include <set>
#include <map>
#include <queue>

#define IOS     ios::sync_with_stdio(0);cin.tie(0);
#define rep(index,start,end) for(int index = start;index < end; index ++)
#define drep(index,start,end) for(int index = start;index >= end; index --)

using namespace std;

typedef long long ll;
ll gcd(ll a, ll b){
    ll t;
    while(b){
        t = b;
        b = a % b;
        a = t;
    }
    return a;
}

const int maxn = 2e5+5;

struct node{
    int digit;
    int zero;
};

int n,m;
node store[maxn];
bool cmp(const node& a, const node& b) {
    return a.zero > b.zero;
}
int main() {
    IOS
    
    int t;
    cin>>t;
    while(t--) {
        int tmp;
        cin>>n>>m;
        int sum = 0;
        rep(i,0,n) {
            cin>>tmp;
            int dig = 0;
            int zero = 0;
            bool leading = true;
            while(tmp > 0) {
                dig ++;
                if (leading && !(tmp % 10)) {
                    zero ++;
                } else {
                    leading = false;
                }
                tmp /= 10;
            }
//            cout<<"dig :"<<dig<<" zero:"<<zero<<endl;
            store[i].digit = dig;
            store[i].zero = zero;
            sum += dig;
        }
        
        sort(store, store+n, cmp);
//        cout<<"test log:"<<store[0].zero<<endl;
        for(int i=0;i<n;i+=2) {
            sum -= store[i].zero;
        }
        
        if (sum < m+1) {
            cout<<"Anna"<<endl;
        } else {
            cout<<"Sasha"<<endl;
        }
    }
    
    return 0;
}

https://codeforces.com/contest/1931/problem/F 拓扑排序

给几个数组,第一位没有用,问有没有一个排列能满足这几个数组中元素的先后关系。

数组给出的顺序天然形成有向图。像 1 → 2 1 \rightarrow 2 12 2 → 1 2 \rightarrow 1 21这种矛盾的顺序必然是不存在序列的,也就是说给出的关系不能有环。所以简单套一个拓扑排序就可以了


#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <time.h>
#include <set>
#include <map>
#include <queue>
 
#define IOS     ios::sync_with_stdio(0);cin.tie(0);
#define rep(index,start,end) for(int index = start;index < end; index ++)
#define drep(index,start,end) for(int index = start;index >= end; index --)
 
using namespace std;
 
const int maxn = 2e5+5;
 
int store[maxn];
vector<int> adj[maxn];
int in_degrad[maxn];
queue<int> Q;
int main() {
    IOS
    
    int t;
    cin>>t;
    while(t--) {
        int n,k;
        cin>>n>>k;
        // init
        memset(in_degrad, 0, sizeof(in_degrad));
        rep(i,0,n+1) adj[i].clear();
        while(!Q.empty()) Q.pop();
        
        rep(i,0,k) {
            rep(j,0,n) cin>>store[j];
            
            rep(j,1,n-1) {
                int commonA = store[j];
                int commonB = store[j+1];
                if (find(adj[commonA].begin(), adj[commonA].end(), commonB) == adj[commonA].end()) {
                    adj[commonA].push_back(commonB);
                    in_degrad[commonB] ++;
                }
            }
        }
        
        rep(i,1,n+1) {
            if (!in_degrad[i])
                Q.push(i);
        }
        
        while (!Q.empty()) {
            int now = Q.front();
            Q.pop();
            
            int len = adj[now].size();
            rep(i,0,len) {
                int next = adj[now][i];
                if (-- in_degrad[next] == 0)
                    Q.push(next);
            }
        }
        
        bool _loop = false;
        rep(i,1,n+1)
            if (in_degrad[i]) {
//                cout<<i<<' '<<in_degrad[i]<<endl;
                _loop = true;
                break;
            }
        cout<<(_loop? "NO":"YES")<<endl;
    }
    
    return 0;
}

G. One-Dimensional Puzzle 高二排列组合问题

题干太长懒得翻译 有多少种排列方式可以把给出的所有形状拼成一个长条。

codeforce #925 (div3) 题解,codeforces,算法
大概就是这么个拼接的方法。shape 1和shape 2的个数相差不能超过1,超过就拼不出来;shape 3和shape 4就是造成不同拼接方式的关键,穿插在shape 1和shape 2的间隙,要注意shape 3是可以自拼接,并不是每个间隙只能塞一个文章来源地址https://www.toymoban.com/news/detail-853264.html

#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <time.h>
#include <set>
#include <map>
#include <queue>

#define IOS     ios::sync_with_stdio(0);cin.tie(0);
#define mem(A,B) memset(A,B,sizeof(A));
#define rep(index,start,end) for(int index = start;index < end; index ++)
#define drep(index,start,end) for(int index = start;index >= end; index --)

using namespace std;

typedef long long ll;

const int maxn = 3e6+5;
const int mod = 998244353;

ll fact[maxn];

ll pow_mod(ll x, ll p) {
    if (p == 0) {
        return 1;
    }
    if (p % 2 == 0) {
        ll y = pow_mod(x, p / 2);
        return (y * y) % mod;
    }
    return (x * pow_mod(x, p - 1)) % mod;
}
 
ll inv(ll x) {
    return pow_mod(x, mod - 2);
}
ll cnk(ll n, ll k) {
    ll res = fact[n];
    res = (res * inv(fact[k])) % mod;
    res = (res * inv(fact[n - k])) % mod;
//    cout<<"n:"<<n<<" k:"<<k<<" res:"<<res<<endl;
    return res;
}
int abs(int num) {
    return num<0 ? -num :num;
}


int store[5];
int main() {
    IOS
    
    fact[0] = fact[1] = 1;
    rep(i,2,maxn)
        fact[i] = (fact[i-1] * i) % mod;
    
    
    int t;
    cin>>t;
    while(t--) {
        rep(i,0,4) cin>>store[i];
        
        if (store[0] == 0 && store[1] == 0) {
            cout<<((store[2]!=0 && store[3] != 0)? 0:1)<<endl;
            continue;
        }
        int dfi = abs(store[1] - store[0]);
        if (dfi > 1) {
            cout<<0<<endl;
            continue;
        }
        
        ll ans = 0;
        if (dfi == 0) {
            // same and not 0
            int x3,x4;
            x3 = store[1];
            x4 = x3 + 1;
            ans += (cnk(store[2]+x3-1, store[2]) * cnk(store[3]+x4-1, store[3])) % mod;
            
            x4 = store[1];
            x3 = x4 + 1;
            ans = ans + (cnk(store[2]+x3-1, store[2]) * cnk(store[3]+x4-1, store[3])) % mod;
            ans = ans % mod;
        } else {
            // greater than one
            int x3,x4;
            x3 = x4 = max(store[0], store[1]);
            ans = (cnk(store[2]+x3-1, store[2]) * cnk(store[3]+x4-1, store[3])) % mod;
        }
        cout<<ans<<endl;
    }
    
    return 0;
}

到了这里,关于codeforce #925 (div3) 题解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Codeforces Round 858 (Div. 2)题解

    目录 A. Walking Master(贪心) 题面翻译 思路: 代码实现  B. Mex Master(贪心) 题面翻译: 思路: 代码实现 C. Sequence Master(数学) 题面翻译: 思路: 代码实现 YunQian is standing on an infinite plane with the Cartesian coordinate system on it. In one move, she can move to the diagonally adjacent point on the

    2023年04月08日
    浏览(71)
  • Educational Codeforces Round 145 Div. 2 题解

    目录 A. Garland(签到) 题面翻译 思路: 代码 B. Points on Plane(数学) 题面翻译 思路: 代码 C. Sum on Subarray(构造) 题面翻译: 思路: 代码 D. Binary String Sorting 题面翻译 思路: 代码 You have a garland consisting of 4 colored light bulbs, the color of the i-th light bulb is si. Initially, all the l

    2023年04月09日
    浏览(30)
  • Educational Codeforces Round 147 div2题解

    目录 A. Matching(签到) 思路: 代码:  B. Sort the Subarray(签到) 思路: 代码: C. Tear It Apart(枚举) 思路: 代码: D. Black Cells(模拟) 思路:  代码一: 代码二:(模仿自\\\"AG\\\"佬) An integer template is a string consisting of digits and/or question marks. A positive (strictly greater than 0) in

    2023年04月21日
    浏览(31)
  • Codeforces Div.2 1798B Three Sevens题解

    题目: 传送门 B. Three Sevens time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Lottery \\\"Three Sevens\\\" was held for m days. On day i, ni people with the numbers ai,1,…,ai,ni,participated in the lottery. It is known that in each of the m days, only one winner was selected from the lot

    2024年02月07日
    浏览(30)
  • Educational Codeforces Round 134 (Div.2) D 题解

    D. Maximum AND 给定两组序列 (a) (b) ,长度为 (n) ,现有一新序列 (c) ,长度也为 (n) 。 其中, (c_i = a_i oplus b_i) 。 定义 (f(a,b) = c_1c_2……c_n) 。 现在你可以随意编排 (b) 序列的顺序,求 (f(a,b)) 的最大值。 以下位运算均是二进制。 由于按位与的运算结果是越来越小的

    2024年02月06日
    浏览(35)
  • Educational Codeforces Round 148 (Rated for Div. 2) 题解

    总结:5.21下午VP一场,死在了A题,给我wa崩溃了,浪费了差不多一个小时,BC还挺顺畅,虽然C题是在结束后不久交上去的。。。。 思路:其实思路很简单, “ The string s a palindrome ”, 题目已经说了所给的为回文字符串,所以直接判断一半 有几种字符 即可(开始的时候计算整

    2024年02月06日
    浏览(30)
  • Codeforces 918(div4)

    注意一下判断 是否为平方数的方法;也要记得开long long 正序写 讨论的情况比较多,所以选择倒叙看;

    2024年02月03日
    浏览(37)
  • Codeforces Round 881 (Div. 3)

    给定一个数组,给每个元素涂色。求最大的代价。 代价为每个颜色的代价和。 每个颜色的代价为涂了该颜色的元素的极差。 因为是极差,每个元素要么对答案有正的贡献,要么有负的贡献,要么无贡献。且正负贡献的个数要相同。 因为要最大值,自然就是想有正贡献的是最

    2024年02月09日
    浏览(32)
  • Codeforces Round 871 (Div. 4)

    给定n个长度为10的字符串,问其与codeforces字符串的对应下标字母不同的个数。 对于每个字符串从前往后依次和“codeforces”对应字符比较然后统计不同字母数即可 给定长度为n的数组,问连续相邻为0的最大长度 维护一个全局最大长度res,和当前连续序列的长度cnt。从前往后扫

    2024年02月03日
    浏览(44)
  • Codeforces Round 867 (Div. 3)

    从所有a[i]+i-1=t的选择种取个max即可 实际上就是取同符号乘积的最大值 找规律,发现结果与边长n的关系是:res = n * (n + 3) - (n - 2) ①当n为奇数时,除了1其他均无解 ②当n为偶数时,我们可以构造一个形如n,1,n - 2,3,...的数列 首先我们可以发现n必定出现在起始位置。如果n不在起

    2024年02月02日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包