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

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char str[];
int main()
{
cin>>str;
int ans = ;
int L = strlen(str);
for(int i = ; i < L; i++)
for(int j = i+; j < L; j++)
for(int k = j+; k < L; k++)
if(str[i] == 'Q' && str[j] == 'A' && str[k] == 'Q') ans++;
cout<<ans<<endl;
return ;
}

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

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

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

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const LL MOD = 1e9 + ;
LL mypow(LL a, LL b){
LL ans = ; for(; b; b>>=, (a*=a)%=MOD) if(b&) (ans*=a)%=MOD;return ans;
}
LL x, y, k;
int main()
{
cin>>x>>y>>k;
if( ((x+y)&) && k == -){
cout<<<<endl;
return ;
}
cout<<mypow(, ((x-)%(MOD-)) *((y-)%(MOD-)) %(MOD-))<<endl;
return ;
}

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

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

这个构造还是挺巧妙的。

#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = ;
int a[maxn], n;
int H[];
int gcd(int x, int y) { return x % y == ? y : gcd(y, x%y); }
int main()
{
cin>>n;
for(int i = ; i <= n; i++) scanf("%d", &a[i]);
int ans = a[];
for(int i = ; i <= n; i++) ans = gcd(ans, a[i]);
if(ans != a[]){
cout<<"-1"<<endl;
return ;
}
cout<<*n-<<endl;
cout<<a[]<<" ";
for(int i = ; i <= n; i++){
cout<<a[]<<" "<<a[i]<<" ";
}
}

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

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

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

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

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

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

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

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 1e6 + ;
long long v[maxn][];
vector<long long> D[maxn], Sum[maxn];
long long getsum(int k, int i, int j){
if(i > j) return ;
return i- < ? Sum[k][j] : Sum[k][j] - Sum[k][i-];
}
int main()
{
int n, m, x, a, h;
cin>>n>>m;
for(int i = ; i < n; i++) {
scanf("%d", &x);
v[(i+)/][(i+)%] = x;
}
for(int i = n; i >= ; i--){
D[i].push_back();
if(i* <= n){
for(auto x : D[i*])
D[i].push_back(x+v[i][]);
}
if(i*+ <= n){
for(auto x : D[i*+])
D[i].push_back(x+v[i][]);
}
sort(D[i].begin(), D[i].end());
Sum[i].push_back(D[i][]);
for(int j = ; j < D[i].size(); j++) Sum[i].push_back(D[i][j]+Sum[i][j-]);
}
while(m--){
scanf("%d %d", &a, &h);
long long d = , n = , decans = ;
int pos1 = upper_bound(D[a].begin(), D[a].end(), h) - D[a].begin();
n += pos1;
decans += getsum(a, , pos1-);
int i = a, j;
while(i != ){
j = i^;
d += v[i/][i&];
if(d < h) {
n++;
decans += d;
}
i /= ;
pos1 = upper_bound(D[j].begin(), D[j].end(), h-v[i][j&]-d) - D[j].begin();
n += pos1;
decans += getsum(j, , pos1-)+pos1*(d+v[i][j&]);
}
long long ans = (long long)n*h - decans;
printf("%lld\n", ans);
}
}

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

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

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

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

当然也可以二分找。

#include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
#include <queue>
#include <algorithm>
#define mp make_pair
#define fi first
#define se second
using namespace std;
const int maxn = 1e6 + ;
stack<int> S;
vector< pair<int, int> > G2[maxn];
vector<int> G[maxn];
long long dp[maxn], v[maxn];
int dfn[maxn], low[maxn], ins[maxn], bl[maxn], C = , Z = ;
int vis[maxn];
void tj(int x)
{
dfn[x]=low[x]=++C; ins[x]=; S.push(x);
for(auto b : G[x])
{
if(!dfn[b]) tj(b),low[x]=min(low[x],low[b]);
else if(ins[b]) low[x]=min(low[x],dfn[b]);
}
if(dfn[x]!=low[x]) return;
++Z;
while(!S.empty())
{
int g=S.top(); S.pop();
ins[g]=; bl[g]=Z;
if(g==x) break;
}
}
struct Edge{
int from, to, cost;
Edge(int from, int to, int cost):from(from), to(to), cost(cost) {}
bool operator <(const Edge &B) const{
return cost < B.cost;
}
};
vector<Edge> edges;
int n, m, x, y, z, s;
long long ans = ;
void dfs(int x){
if(vis[x]) return;
vis[x] = ; dp[x] = ;
for(auto e : G2[x]){
dfs(e.fi);
dp[x] = max(dp[x], dp[e.fi] + e.se);
}
dp[x] += v[x];
ans = max(ans, dp[x]);
}
int main()
{
cin>>n>>m;
for(int i = ; i <= m; i++){
scanf("%d %d %d", &x, &y, &z);
G[x].push_back(y);
edges.push_back(Edge(x, y, z));
}
sort(edges.begin(), edges.end());
cin>>s;
tj(s);
int k = ;
for(auto &e : edges){
while(e.cost >= (k+)*(k+)/) k++;
long long temp = (long long)e.cost*(k+) - (long long)k*(k+)*(k+)/;
if(bl[e.from] == bl[e.to])
v[bl[e.from]] += temp;
else G2[bl[e.from]].push_back(mp(bl[e.to], e.cost));
}
dfs(bl[s]);
cout<<ans<<endl;
return ;
}

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. 【LG5022】[NOIP2018]旅行

    [LG5022][NOIP2018]旅行 题面 洛谷 题解 首先考虑一棵树的部分分怎么打 直接从根节点开始\(dfs\),依次选择编号最小的儿子即可 而此题是一个基环树 怎么办呢? 可以断掉环上的一条 ...

  2. 移动端推广APP防作弊机制之依我见

    本文来自网易云社区 在广告投放过程中,虚假流量常常给广告运营人员带来麻烦,影响广告投放的效果,如何预防作弊,不妨先来重现一下流量产生的场景,用户点击广告之后,一般都会落到广告主的网页,或者安装广告主的 ...

  3. 【Excel函数】如何在excle区分一列数字是否连续

    需求:区分这批卡号,哪些在一个号段 数据源: 89860616090033685544898606160900336855518986061609003368556989860616090033685 ...

  4. loadrunner之做压力测试要做的准备

    前提B/S架构 1.要有个备库和主库保存一致 到时候做压力测试的时候,要断开主库连接到备库.进行测试.以免主库出现垃圾数据.2.节点 判断单节点能承受多大的压力,如200万的用户账号,10万的在线用户 ...

  5. 初次学习asp.net core的心得

    初次学习Asp.Net Core方面的东西,虽然研究的还不是很深,今天主要是学习了一下Asp.Net Core WebAPI项目的使用,发现与Asp.Net WebAPI项目还是有很多不同.不同点包含 ...

  6. Spark之编程模型RDD

    前言:Spark编程模型两个主要抽象,一个是弹性分布式数据集RDD,它是一种特殊集合,支持多种数据源,可支持并行计算,可缓存:另一个是两种共享变量,支持并行计算的广播变量和累加器. 1.RDD介绍 S ...

  7. scrapy-redis+selenium+webdriver解决动态代理ip和user-agent的问题(全网唯一完整代码解决方案)

    问题描述:在爬取一些反爬机制做的比较好的网站时,经常会遇见一个问题就网站代码是通过js写的,这种就无法直接使用一般的爬虫工具爬取,这种情况一般有两种解决方案 第一种:把js代码转为html代码,然后再 ...

  8. 分布式数据库中间件Mycat百亿级数据存储(转)

    此文转自: https://www.jianshu.com/p/9f1347ef75dd 2013年阿里的Cobar在社区使用过程中发现存在一些比较严重的问题,如高并发下的假死,心跳连接的故障,只实现 ...

  9. [leetcode-811-Subdomain Visit Count]

    A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...

  10. 最全的NB-IoT芯片厂商、模组厂商信息

    NB-IoT作为LPWAN(低功耗广域网)的新兴技术,因为具有低功耗.低成本.广覆盖.海量节点等优势,并且在授权频段可以与2G.3G无缝连接而被运营商所青睐且接受.特别是到了2017年,据统计全球有5 ...