0


Codeforces Round 976 (Div. 2) and Divide By Zero 9.0(A~E)

A - Find Minimum Operations

思路

  1. n
  2. n
  3. n进行
  4. m
  5. m
  6. m进制分解,所有位上相加就是答案(参考
  7. m
  8. =
  9. 2
  10. m=2
  11. m=2时)

代码

  1. // Problem: A. Find Minimum Operations// Contest: Codeforces - Codeforces Round 976 (Div. 2) and Divide By Zero 9.0// URL: https://codeforces.com/contest/2020/problem/0// Memory Limit: 256 MB// Time Limit: 1000 ms// // Powered by CP Editor (https://cpeditor.org)#include<bits/stdc++.h>usingnamespace std;#defineLLlonglong#definepbpush_back#definexfirst#defineysecond #defineendl'\n'const LL maxn =4e05+7;const LL N =5e05+10;const LL mod =1e09+7;constint inf =0x3f3f3f3f;const LL llinf =5e18;typedef pair<int,int>pl;
  2. priority_queue<LL , vector<LL>, greater<LL>>mi;//小根堆
  3. priority_queue<LL> ma;//大根堆intcalc(int n ,int m){int cnt =0;if(m ==1){return n;}while(n){
  4. cnt += n % m;
  5. n /= m;}return cnt;}voidsolve(){int n , m;
  6. cin >> n >> m;
  7. cout <<calc(n , m)<< endl;}intmain(){
  8. ios::sync_with_stdio(false);
  9. cin.tie(0);
  10. cout.tie(0);
  11. cout.precision(10);int t=1;
  12. cin>>t;while(t--){solve();}return0;}

B - Brightness Begins

思路

经典关灯,第

  1. x
  2. x
  3. x位置的灯会被
  4. x
  5. x
  6. x的所有因数操作,因此当且仅当
  7. x
  8. x
  9. x为完全平方数时会被关闭,考虑二分答案,找到
  10. m
  11. i
  12. d
  13. mid
  14. mid之前有多少个完全平方数就能算出最终有多少个灯是关着的,可以直接用
  15. s
  16. q
  17. r
  18. t
  19. sqrt
  20. sqrt函数近似的找到最接近
  21. m
  22. i
  23. d
  24. mid
  25. mid的完全平方数,然后左右探测一下就能找到有多少个完全平方数小于等于
  26. m
  27. i
  28. d
  29. mid
  30. mid.

代码

  1. // Problem: B. Brightness Begins// Contest: Codeforces - Codeforces Round 976 (Div. 2) and Divide By Zero 9.0// URL: https://codeforces.com/contest/2020/problem/B// Memory Limit: 256 MB// Time Limit: 1000 ms// // Powered by CP Editor (https://cpeditor.org)#include<bits/stdc++.h>usingnamespace std;#defineLLlonglong#definepbpush_back#definexfirst#defineysecond #defineendl'\n'#defineintlonglongconst LL maxn =4e05+7;const LL N =5e05+10;const LL mod =1e09+7;constint inf =0x3f3f3f3f;const LL llinf =5e18;typedef pair<int,int>pl;
  2. priority_queue<LL , vector<LL>, greater<LL>>mi;//小根堆
  3. priority_queue<LL> ma;//大根堆voidsolve(){int n;
  4. cin >> n;int l = n , r =2e18;while(l < r){int mid =(l + r)>>1;//[1 - mid]中有几个完全平方数int t =sqrt(mid);while(t * t > mid){
  5. t--;}while((t +1)*(t +1)<= mid){
  6. t++;}if(mid - t >= n){
  7. r = mid;}else{
  8. l = mid +1;}}
  9. cout << l << endl;}signedmain(){
  10. ios::sync_with_stdio(false);
  11. cin.tie(0);
  12. cout.tie(0);
  13. cout.precision(10);int t=1;
  14. cin>>t;while(t--){solve();}return0;}

C - Bitwise Balancing

思路

拆位,对每一位进行分析,

  1. [
  2. b
  3. ,
  4. c
  5. ,
  6. d
  7. ]
  8. [b,c,d]
  9. [b,c,d]在某一位上的组合只有
  10. 8
  11. 8
  12. 8种,对每种分类讨论
  13. a
  14. a
  15. a的取值

代码

  1. // Problem: C. Bitwise Balancing// Contest: Codeforces - Codeforces Round 976 (Div. 2) and Divide By Zero 9.0// URL: https://codeforces.com/contest/2020/problem/C// Memory Limit: 256 MB// Time Limit: 2000 ms// // Powered by CP Editor (https://cpeditor.org)#include<bits/stdc++.h>usingnamespace std;#defineLLlonglong#definepbpush_back#definexfirst#defineysecond #defineendl'\n'#defineintlonglongconst LL maxn =4e05+7;const LL N =5e05+10;const LL mod =1e09+7;constint inf =0x3f3f3f3f;const LL llinf =5e18;typedef pair<int,int>pl;
  2. priority_queue<LL , vector<LL>, greater<LL>>mi;//小根堆
  3. priority_queue<LL> ma;//大根堆voidsolve(){//如果d的这一位是0,int b , c , d;
  4. cin >> b >> c >> d;int a =0;for(int i =63; i >=0; i --){if(d >> i &1LL){if(c >> i &1LL){if(b >> i &1LL){continue;}else{
  5. cout <<-1<< endl;return;}}else{
  6. a |=(1LL<< i);}}else{if(b >> i &1LL){if(c >> i &1LL){
  7. a |=(1LL<< i);}else{
  8. cout <<-1<< endl;return;}}else{continue;}}}
  9. cout << a <<endl;}signedmain(){
  10. ios::sync_with_stdio(false);
  11. cin.tie(0);
  12. cout.tie(0);
  13. cout.precision(10);int t=1;
  14. cin>>t;while(t--){solve();}return0;}

D - Connect the Dots

思路

注意到

  1. d
  2. d
  3. d很小,因此如果我们只需要知道某一位置上能否跟前面
  4. d
  5. d
  6. d个数相连,就能把最终的图连出来。

用数组

  1. c
  2. n
  3. t
  4. [
  5. i
  6. ]
  7. [
  8. j
  9. ]
  10. cnt[i][j]
  11. cnt[i][j]来表示
  12. a
  13. i
  14. a_i
  15. ai​能否跟
  16. a
  17. i
  18. j
  19. a_{i - j}
  20. aij​相连,用差分来维护
  21. c
  22. n
  23. t
  24. cnt
  25. cnt数组即可。

代码

  1. // Problem: D. Connect the Dots// Contest: Codeforces - Codeforces Round 976 (Div. 2) and Divide By Zero 9.0// URL: https://codeforces.com/contest/2020/problem/D// Memory Limit: 512 MB// Time Limit: 2000 ms// // Powered by CP Editor (https://cpeditor.org)#include<bits/stdc++.h>usingnamespace std;#defineLLlonglong#definepbpush_back#definexfirst#defineysecond #defineendl'\n'const LL maxn =4e05+7;const LL N =5e05+10;const LL mod =1e09+7;constint inf =0x3f3f3f3f;const LL llinf =5e18;typedef pair<int,int>pl;
  2. priority_queue<LL , vector<LL>, greater<LL>>mi;//小根堆
  3. priority_queue<LL> ma;//大根堆structDSU{
  4. std::vector<int> f, siz;DSU(){}DSU(int n){init(n);}voidinit(int n){
  5. f.resize(n);
  6. std::iota(f.begin(), f.end(),0);
  7. siz.assign(n,1);}intfind(int x){while(x != f[x]){
  8. x = f[x]= f[f[x]];}return x;}boolsame(int x,int y){returnfind(x)==find(y);}boolmerge(int x,int y){
  9. x =find(x);
  10. y =find(y);if(x == y){returnfalse;}
  11. siz[x]+= siz[y];
  12. f[y]= x;returntrue;}intsize(int x){return siz[find(x)];}};voidsolve(){//注意到D很小int n , m;
  13. cin >> n >> m;int vis[n +100][20];int cnt[n +100][20];memset(vis ,0,sizeof vis);memset(cnt ,0,sizeof cnt);
  14. DSU dsu(n +5);for(int i =0; i < m ; i ++){int a , d , k;
  15. cin >> a >> d >> k;int l = a , r = a + k * d;
  16. cnt[l + d][d]++;
  17. cnt[r + d][d]--;}for(int i =1; i <= n ; i ++){int tot =0;for(int j =1; j <=min(10, i); j ++){
  18. cnt[i][j]+= cnt[i - j][j];}}for(int i =2; i <= n ; i ++){for(int j =1; j <=min(i -1,10); j ++){if(cnt[i][j]){
  19. dsu.merge(i , i - j);}}}int ans =0;for(int i =1; i <= n ; i ++){
  20. ans +=(dsu.f[i]== i);}
  21. cout << ans << endl;}intmain(){
  22. ios::sync_with_stdio(false);
  23. cin.tie(0);
  24. cout.tie(0);
  25. cout.precision(10);int t=1;
  26. cin>>t;while(t--){solve();}return0;}

E - Expected Power

思路

观察到

  1. a
  2. a
  3. a数组取值很小,考虑
  4. d
  5. p
  6. dp
  7. dp解决问题。用
  8. d
  9. p
  10. [
  11. i
  12. ]
  13. dp[i]
  14. dp[i]来代表取到当前取到
  15. i
  16. i
  17. i的概率,那么对于每个
  18. a
  19. i
  20. a_{i}
  21. ai​,我们枚举
  22. x
  23. (
  24. 0
  25. x
  26. 1023
  27. )
  28. x(0 \leq x \leq 1023)
  29. x(0x1023) ,
  30. d
  31. p
  32. [
  33. x
  34. a
  35. i
  36. ]
  37. =
  38. d
  39. p
  40. [
  41. x
  42. ]
  43. p
  44. ,
  45. d
  46. p
  47. [
  48. x
  49. ]
  50. =
  51. d
  52. p
  53. [
  54. x
  55. ]
  56. (
  57. 1
  58. p
  59. )
  60. dp[x \oplus a_{i}] = dp[x] * p , dp[x] = dp[x] * (1 - p)
  61. dp[xai​]=dp[x]∗p,dp[x]=dp[x]∗(1p),总体复杂度为
  62. O
  63. (
  64. 1024
  65. N
  66. )
  67. O(1024*N)
  68. O(1024N)能通过此题

代码

  1. // Problem: E. Expected Power// Contest: Codeforces - Codeforces Round 976 (Div. 2) and Divide By Zero 9.0// URL: https://codeforces.com/contest/2020/problem/E// Memory Limit: 256 MB// Time Limit: 4000 ms// // Powered by CP Editor (https://cpeditor.org)#include<bits/stdc++.h>usingnamespace std;#defineLLlonglong#definepbpush_back#definexfirst#defineysecond #defineendl'\n'const LL maxn =4e05+7;const LL N =5e05+10;const LL mod =1e09+7;#defineintlonglongconstint inf =0x3f3f3f3f;const LL llinf =5e18;typedef pair<int,int>pl;
  2. priority_queue<LL , vector<LL>, greater<LL>>mi;//小根堆
  3. priority_queue<LL> ma;//大根堆
  4. LL qpow(LL a , LL b)//快速幂{
  5. LL sum=1;while(b){if(b&1){
  6. sum=sum*a%mod;}
  7. a=a*a%mod;
  8. b>>=1;}return sum;}voidsolve(){int t =qpow(10000, mod -2);int dp[1030][2];//取到i的概率memset(dp ,0,sizeof dp);
  9. dp[0][0]=1;int n;
  10. cin >> n;int a[n];for(int i =0; i < n ; i ++){
  11. cin >> a[i];}for(int i =0; i < n ; i ++){int p;
  12. cin >> p;for(int j =0; j <1024; j ++){int k = j ^ a[i];
  13. dp[k][1]+= dp[j][0]*((p * t)% mod);
  14. dp[k][1]%= mod;
  15. dp[j][1]+= dp[j][0]*(((10000- p)* t)% mod);
  16. dp[j][1]%= mod;}for(int j =0; j <1024; j++){
  17. dp[j][0]= dp[j][1];
  18. dp[j][1]=0;}}int ans =0;for(int i =1; i <1024; i ++){
  19. ans += dp[i][0]* i * i;
  20. ans %= mod;}
  21. cout << ans << endl;}signedmain(){
  22. ios::sync_with_stdio(false);
  23. cin.tie(0);
  24. cout.tie(0);
  25. cout.precision(10);int t=1;
  26. cin>>t;while(t--){solve();}return0;}
标签: 算法 数据结构 c++

本文转载自: https://blog.csdn.net/weixin_61825750/article/details/142646582
版权归原作者 今天补题了么 所有, 如有侵权,请联系我们删除。

“Codeforces Round 976 (Div. 2) and Divide By Zero 9.0(A~E)”的评论:

还没有评论