A.很水的题目,3个for循环就可以了

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. using namespace std;
  5. char str[];
  6. int main()
  7. {
  8. cin>>str;
  9. int ans = ;
  10. int L = strlen(str);
  11. for(int i = ; i < L; i++)
  12. for(int j = i+; j < L; j++)
  13. for(int k = j+; k < L; k++)
  14. if(str[i] == 'Q' && str[j] == 'A' && str[k] == 'Q') ans++;
  15. cout<<ans<<endl;
  16. return ;
  17. }

B.如果存在解,那么答案就是2^(x-1)(y-1),然后快速幂就可以了。

实际上就是判断(x-1)*(y-1)都填1有没有解,如果有的话,其实你变换任意一个矩阵内元素的值都有对应的唯一一种情况成立。

注意费马小定理和long long的溢出问题。

  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4. typedef long long LL;
  5. const LL MOD = 1e9 + ;
  6. LL mypow(LL a, LL b){
  7. LL ans = ; for(; b; b>>=, (a*=a)%=MOD) if(b&) (ans*=a)%=MOD;return ans;
  8. }
  9. LL x, y, k;
  10. int main()
  11. {
  12. cin>>x>>y>>k;
  13. if( ((x+y)&) && k == -){
  14. cout<<<<endl;
  15. return ;
  16. }
  17. cout<<mypow(, ((x-)%(MOD-)) *((y-)%(MOD-)) %(MOD-))<<endl;
  18. return ;
  19. }

C.首先必定有一个元素是所有元素的gcd,否则就是无解。

然后这个gcd也必定是最小的,令它为g,那么我们只需要把g插入到原序列中,就可以保证两两之间的gcd被限制到g,就满足了要求。

这个构造还是挺巧妙的。

  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4. const int maxn = ;
  5. int a[maxn], n;
  6. int H[];
  7. int gcd(int x, int y) { return x % y == ? y : gcd(y, x%y); }
  8. int main()
  9. {
  10. cin>>n;
  11. for(int i = ; i <= n; i++) scanf("%d", &a[i]);
  12. int ans = a[];
  13. for(int i = ; i <= n; i++) ans = gcd(ans, a[i]);
  14. if(ans != a[]){
  15. cout<<"-1"<<endl;
  16. return ;
  17. }
  18. cout<<*n-<<endl;
  19. cout<<a[]<<" ";
  20. for(int i = ; i <= n; i++){
  21. cout<<a[]<<" "<<a[i]<<" ";
  22. }
  23. }

D.树是二叉树,就很好做了

每个结点保存子树中到它的距离集合

答案就是子树内满足要求的点的个数n*h和它们的距离和的差,就是n*h - sum

查询这个用二分查找就可以了

注意到非子树内也有满足要求的点,

解决这个问题只需要沿着祖先往上爬就可以了,沿途统计答案。

这里使用了upper_bound,还是很好用的。

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <vector>
  5. #include <algorithm>
  6. using namespace std;
  7. const int maxn = 1e6 + ;
  8. long long v[maxn][];
  9. vector<long long> D[maxn], Sum[maxn];
  10. long long getsum(int k, int i, int j){
  11. if(i > j) return ;
  12. return i- < ? Sum[k][j] : Sum[k][j] - Sum[k][i-];
  13. }
  14. int main()
  15. {
  16. int n, m, x, a, h;
  17. cin>>n>>m;
  18. for(int i = ; i < n; i++) {
  19. scanf("%d", &x);
  20. v[(i+)/][(i+)%] = x;
  21. }
  22. for(int i = n; i >= ; i--){
  23. D[i].push_back();
  24. if(i* <= n){
  25. for(auto x : D[i*])
  26. D[i].push_back(x+v[i][]);
  27. }
  28. if(i*+ <= n){
  29. for(auto x : D[i*+])
  30. D[i].push_back(x+v[i][]);
  31. }
  32. sort(D[i].begin(), D[i].end());
  33. Sum[i].push_back(D[i][]);
  34. for(int j = ; j < D[i].size(); j++) Sum[i].push_back(D[i][j]+Sum[i][j-]);
  35. }
  36. while(m--){
  37. scanf("%d %d", &a, &h);
  38. long long d = , n = , decans = ;
  39. int pos1 = upper_bound(D[a].begin(), D[a].end(), h) - D[a].begin();
  40. n += pos1;
  41. decans += getsum(a, , pos1-);
  42. int i = a, j;
  43. while(i != ){
  44. j = i^;
  45. d += v[i/][i&];
  46. if(d < h) {
  47. n++;
  48. decans += d;
  49. }
  50. i /= ;
  51. pos1 = upper_bound(D[j].begin(), D[j].end(), h-v[i][j&]-d) - D[j].begin();
  52. n += pos1;
  53. decans += getsum(j, , pos1-)+pos1*(d+v[i][j&]);
  54. }
  55. long long ans = (long long)n*h - decans;
  56. printf("%lld\n", ans);
  57. }
  58. }

E.就是tarjan缩点+动态规划。

缩点之后可以用dfs直接更新dp。

注意要先求最大值,再加环构成的影响。

求一个边的循环贡献,这里是先排了个序,然后按顺序扫一遍求出来的。

当然也可以二分找。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <stack>
  4. #include <vector>
  5. #include <queue>
  6. #include <algorithm>
  7. #define mp make_pair
  8. #define fi first
  9. #define se second
  10. using namespace std;
  11. const int maxn = 1e6 + ;
  12. stack<int> S;
  13. vector< pair<int, int> > G2[maxn];
  14. vector<int> G[maxn];
  15. long long dp[maxn], v[maxn];
  16. int dfn[maxn], low[maxn], ins[maxn], bl[maxn], C = , Z = ;
  17. int vis[maxn];
  18. void tj(int x)
  19. {
  20. dfn[x]=low[x]=++C; ins[x]=; S.push(x);
  21. for(auto b : G[x])
  22. {
  23. if(!dfn[b]) tj(b),low[x]=min(low[x],low[b]);
  24. else if(ins[b]) low[x]=min(low[x],dfn[b]);
  25. }
  26. if(dfn[x]!=low[x]) return;
  27. ++Z;
  28. while(!S.empty())
  29. {
  30. int g=S.top(); S.pop();
  31. ins[g]=; bl[g]=Z;
  32. if(g==x) break;
  33. }
  34. }
  35. struct Edge{
  36. int from, to, cost;
  37. Edge(int from, int to, int cost):from(from), to(to), cost(cost) {}
  38. bool operator <(const Edge &B) const{
  39. return cost < B.cost;
  40. }
  41. };
  42. vector<Edge> edges;
  43. int n, m, x, y, z, s;
  44. long long ans = ;
  45. void dfs(int x){
  46. if(vis[x]) return;
  47. vis[x] = ; dp[x] = ;
  48. for(auto e : G2[x]){
  49. dfs(e.fi);
  50. dp[x] = max(dp[x], dp[e.fi] + e.se);
  51. }
  52. dp[x] += v[x];
  53. ans = max(ans, dp[x]);
  54. }
  55. int main()
  56. {
  57. cin>>n>>m;
  58. for(int i = ; i <= m; i++){
  59. scanf("%d %d %d", &x, &y, &z);
  60. G[x].push_back(y);
  61. edges.push_back(Edge(x, y, z));
  62. }
  63. sort(edges.begin(), edges.end());
  64. cin>>s;
  65. tj(s);
  66. int k = ;
  67. for(auto &e : edges){
  68. while(e.cost >= (k+)*(k+)/) k++;
  69. long long temp = (long long)e.cost*(k+) - (long long)k*(k+)*(k+)/;
  70. if(bl[e.from] == bl[e.to])
  71. v[bl[e.from]] += temp;
  72. else G2[bl[e.from]].push_back(mp(bl[e.to], e.cost));
  73. }
  74. dfs(bl[s]);
  75. cout<<ans<<endl;
  76. return ;
  77. }

Codeforces Round #447 (Div. 2) 题解的更多相关文章

  1. Codeforces Round #447 (Div. 2) 题解 【ABCDE】

    BC都被hack的人生,痛苦. 下面是题解的表演时间: A. QAQ "QAQ" is a word to denote an expression of crying. Imag ...

  2. Codeforces Round #182 (Div. 1)题解【ABCD】

    Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...

  3. Codeforces Round #608 (Div. 2) 题解

    目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...

  4. Codeforces Round #525 (Div. 2)题解

    Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...

  5. Codeforces Round #528 (Div. 2)题解

    Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...

  6. Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F

    Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...

  7. Codeforces Round #677 (Div. 3) 题解

    Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...

  8. Codeforces Round #665 (Div. 2) 题解

    Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...

  9. Codeforces Round #160 (Div. 1) 题解【ABCD】

    Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...

随机推荐

  1. vue 与原生app的对接交互(混合开发)

    小伙伴们在用vue开发h5项目特别是移动端的项目,很多都是打包后挂载在原生APP上的,那就少不了与原生交互了,我最近就是在坐这个,踩了一些坑,拿出来给大家分享下. 0.通过url传输数据:(一般是在入 ...

  2. asp.net core webapi项目配置全局路由

    0.目录 整体架构目录:ASP.NET Core分布式项目实战-目录 一.前言 在开发项目的过程中,我新创建了一个controller,发现vs会给我们直接在controller头添加前缀,比如[Ro ...

  3. 我们一起学习WCF 第九篇聊天功能

    说到聊天,那么其实就是传输数据,把自己写的东西传给自己想发送的那么人.我总结一下传输常见的有三种方式 1:就是我们常见的数据库传输 2:就是文件(流)传输 3:就是socket传输 今天我们说的wcf ...

  4. Redis主从复制(Master/Slave) 与哨兵模式

    Redis主从复制是什么? 行话:也就是我们所说的主从复制,主机数据更新后根据配置和策略, 自动同步到备机的master/slaver机制,Master以写为主,Slave以读为主 Redis主从复制 ...

  5. python2 - 列表

    列表 a = [1,2,3,4,5,6,7] a[0:4:1]//正向索引 a[-1:-2:-1]//反向索引 列表添加 a = [1, 2] b = [3, 4] +:a + b//把a和b连接,重 ...

  6. localhost/127.0.0.1/本机IP的区别以及端口号

    端口号: http请求默认的端口是:80 PHPstudy中的端口号: Apache服务器的端口是:80 MySQL数据库的端口是:3306 PHP项目端口是:9000 禅道中的端口号: Apache ...

  7. 随身Wifi+win7 搭建http代理 域名劫持 抓包 内容篡改实验环境

    需求来源: 1.平板或手机是个封闭系统无法给wifi设置代理 2.需要利用filllder进行抓包,内容篡改等实验 拥有硬件资源:PC机器 + 小米随身wifi 方案1: NtBind Dns + N ...

  8. 转 gerrit

    开发环境 https://blog.csdn.net/u013207966/article/details/79112740 先记录下我的开发环境以及要正确安装gerrit需要用到的工具: Redha ...

  9. spring第一章

    spring第一章 一.概述 Spring是一个开源框架,它由Rod Johnson创建.它是为了解决企业应用开发的复杂性而创建的.Spring使用基本的JavaBean来完成以前只可能由EJB完成的 ...

  10. K-means + PCA + T-SNE 实现高维数据的聚类与可视化

    使用matlab完成高维数据的聚类与可视化 [idx,Centers]=kmeans(qy,) [COEFF,SCORE,latent] = pca(qy); SCORE = SCORE(:,:); ...