codeforce #925 (div3) 题解

devtools/2024/9/24 21:10:01/

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() {IOSint 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};// calint 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() {IOSint 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() {IOSint t;cin>>t;while(t--) {int n,k;cin>>n>>k;// initmemset(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 高二排列组合问题

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

请添加图片描述
大概就是这么个拼接的方法。shape 1和shape 2的个数相差不能超过1,超过就拼不出来;shape 3和shape 4就是造成不同拼接方式的关键,穿插在shape 1和shape 2的间隙,要注意shape 3是可以自拼接,并不是每个间隙只能塞一个

#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() {IOSfact[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 0int 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 oneint 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;
}

http://www.ppmy.cn/devtools/7270.html

相关文章

基于R语言实现的负二项回归模型【理解与实现】-理解负二项回归模型和泊松回归模型之间的区别

前言 我们可以在R语言中使用MASS包中的glm.nb函数来拟合负二项模型&#xff0c;以及使用glm函数来拟合泊松模型。以下是一个详细的过程&#xff0c;包括模拟数据的生成、模型的拟合、结果的比较和解释。 需要的包 if (!require("MASS")) install.packages("M…

Python 物联网入门指南(四)

原文&#xff1a;zh.annas-archive.org/md5/4fe4273add75ed738e70f3d05e428b06 译者&#xff1a;飞龙 协议&#xff1a;CC BY-NC-SA 4.0 第九章&#xff1a;构建光学字符识别的神经网络模块 本章介绍以下主题&#xff1a; 使用光学字符识别&#xff08;OCR&#xff09;系统 使…

SAP打印输出设置

SAP打印输入有很多方式&#xff0c;适合不同的应用场景。 一.打印输出总体概览图 二.前台打印 这个是比较常见的&#xff0c;前端打印的出现减轻了管理员的工作量&#xff0c;用户可以选择自己电脑上的打印机输出&#xff0c;不需要所有打印机都在SAP平台中进行配置&#xff0…

【前端】修改iframe里面的pdf的样式

iframe是HTML中的一种元素&#xff0c;用于在网页中嵌入其他网页或文档。通过使用iframe&#xff0c;你可以在一个网页中显示另一个网页的内容。然而&#xff0c;由于安全性和隐私方面的考虑&#xff0c;通过CSS样式直接修改iframe中的内容是不被允许的。 但是&#xff0c;你可…

MyBatis Dynamic SQL基本使用

MyBatis Dynamic SQL基本使用 一、概念二、特性Hamcrest是什么 三、MyBatis Dynamic SQL 快速入门3.1 环境准备3.2 定义表和列3.3 创建 MyBatis3 映射器3.4 使用 MyBatis3 执行 SQL 四、数据库对象表示4.1 表或视图表示4.2 表别名4.3 列表示 五、Where 子句支持5.1 简单的 wher…

洛谷 P3702 [SDOI2017] 序列计数 题解代码 动态规划

[SDOI2017] 序列计数 题目描述 Alice 想要得到一个长度为 n n n 的序列&#xff0c;序列中的数都是不超过 m m m 的正整数&#xff0c;而且这 n n n 个数的和是 p p p 的倍数。 Alice 还希望&#xff0c;这 n n n 个数中&#xff0c;至少有一个数是质数。 Alice 想知道…

7. Spring Boot 创建与使用

经过前面的六篇文章&#xff0c;Spring Framework的知识终于大致讲完了&#xff0c;但是Spring AOP还没提到&#xff0c;个人认为Spring AOP更适合放在Spring MVC之后再讲解&#xff0c;而讲解Spring MVC前先学习Spring Boot的目的也是为了在学习Spring MVC的时候直接使用Sprin…

百度文心一言与谷歌Gemini的对比

版权声明 本文原创作者&#xff1a;谷哥的小弟作者博客地址&#xff1a;http://blog.csdn.net/lfdfhl 本文从多角度将百度文心一言与谷歌Gemini进行对比。因为不同评测基准的侧重点和难度可能有所不同&#xff0c;所以本文涉及到的评测结果仅供参考。Gemini和文心一言都是非常…