BC都被hack的人生，痛苦。

# A. QAQ

"QAQ" is a word to denote an expression of crying. Imagine "Q" as eyes with tears and "A" as a mouth.

Now Diamond has given Bort a string consisting of only uppercase English letters of length n. There is a great number of "QAQ" in the string (Diamond is so cute!).

Bort wants to know how many subsequences "QAQ" are in the string Diamond has given. Note that the letters "QAQ" don't have to be consecutive, but the order of letters should be exact.

## Input

The only line contains a string of length n (1 ≤ n ≤ 100). It's guaranteed that the string only contains uppercase English letters.

## Output

Print a single integer — the number of subsequences "QAQ" in the string.

input
QAQAQYSYIOIWIN
output
4
input
QAQQQZZYNOIWIN
output
3

## Note

In the first example there are 4 subsequences "QAQ": "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN".

no response

## 代码

#include<bits/stdc++.h>
using namespace std;
string s;
int main(){
cin>>s;
long long ans = 0,rnum = 0,lnum = 0;
for(int i=0;i<s.size();i++){
if(s[i]=='Q')rnum++;
}
for(int i=0;i<s.size();i++){
if(s[i]=='Q')lnum++,rnum--;
if(s[i]=='A'){
ans=ans+lnum*rnum;
}
}
cout<<ans<<endl;
}

# B. Ralph And His Magic Field

Ralph has a magic field which is divided into n × m blocks. That is to say, there are n rows and m columns on the field. Ralph can put an integer in each block. However, the magic field doesn't always work properly. It works only if the product of integers in each row and each column equals to k, where k is either 1 or -1.

Now Ralph wants you to figure out the number of ways to put numbers in each block in such a way that the magic field works properly. Two ways are considered different if and only if there exists at least one block where the numbers in the first way and in the second way are different. You are asked to output the answer modulo 1000000007 = 109 + 7.

Note that there is no range of the numbers to put in the blocks, but we can prove that the answer is not infinity.

## Input

The only line contains three integers n, m and k (1 ≤ n, m ≤ 1018, k is either 1 or -1).

## Output

Print a single number denoting the answer modulo 1000000007.

input
1 1 -1
output
1
input
1 3 1
output
1
input
3 3 -1
output
16

## Note

In the first example the only way is to put -1 into the only block.

In the second example the only way is to put 1 into every block.

n为偶数，m为奇数同理。

## 代码

#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
long long quickpow(long long  m,long long n,long long k)
{
long long b = 1;
while (n > 0)
{
if (n & 1)
b = (b*m)%k;
n = n >> 1 ;
m = (m*m)%k;
}
return b;
}
int main(){
long long ans = 1;
long long n,m,k;
cin>>n>>m>>k;
if((n+m)%2&&k==-1){
cout<<"0"<<endl;
return 0;
}
cout<<quickpow(quickpow(2,min(n,m)-1,mod),max(n,m)-1,mod)<<endl;
}

# C. Marco and GCD Sequence

In a dream Marco met an elderly man with a pair of black glasses. The man told him the key to immortality and then disappeared with the wind of time.

When he woke up, he only remembered that the key was a sequence of positive integers of some length n, but forgot the exact sequence. Let the elements of the sequence be a1, a2, ..., an. He remembered that he calculated gcd(ai, ai + 1, ..., aj) for every 1 ≤ i ≤ j ≤ n and put it into a set S. gcd here means the greatest common divisor.

Note that even if a number is put into the set S twice or more, it only appears once in the set.

Now Marco gives you the set S and asks you to help him figure out the initial sequence. If there are many solutions, print any of them. It is also possible that there are no sequences that produce the set S, in this case print -1.

## Input

The first line contains a single integer m (1 ≤ m ≤ 1000) — the size of the set S.

The second line contains m integers s1, s2, ..., sm (1 ≤ si ≤ 106) — the elements of the set S. It's guaranteed that the elements of the set are given in strictly increasing order, that means s1 < s2 < ... < sm.

## Output

If there is no solution, print a single line containing -1.

Otherwise, in the first line print a single integer n denoting the length of the sequence, n should not exceed 4000.

In the second line print n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the sequence.

We can show that if a solution exists, then there is a solution with n not exceeding 4000 and ai not exceeding 106.

If there are multiple solutions, print any of them.

input
4
2 4 6 12
output
3
4 6 12
input
2
2 3
output
-1

## Note

In the first example 2 = gcd(4, 6), the other elements from the set appear in the sequence, and we can show that there are no values different from 2, 4, 6 and 12 among gcd(ai, ai + 1, ..., aj) for every 1 ≤ i ≤ j ≤ n.

## 代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3+6;
int n;
int a[maxn];
map<int,int>H;
int gcd(int a,int b){
if(b==0)return a;
return gcd(b,a%b);
}
int main(){
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
H[a[i]]=1;
}
vector<int> ans;
for(int i=0;i<n;i++){
ans.push_back(a[i]);
ans.push_back(a[0]);
//cout<<a[i]<<" "<<a[0]<<" ";
}
for(int i=0;i<ans.size();i++){
int p=ans[i];
for(int j=i;j<ans.size();j++){
p=gcd(p,ans[j]);
if(!H[p]){
cout<<"-1"<<endl;
return 0;
}
}
}
cout<<n*2<<endl;
for(int i=0;i<ans.size();i++){
cout<<ans[i]<<" ";
}
cout<<endl;
}

# D. Ralph And His Tour in Binary Country

Ralph is in the Binary Country. The Binary Country consists of n cities and (n - 1) bidirectional roads connecting the cities. The roads are numbered from 1 to (n - 1), the i-th road connects the city labeled (here ⌊ x⌋ denotes the x rounded down to the nearest integer) and the city labeled (i + 1), and the length of the i-th road is Li.

Now Ralph gives you m queries. In each query he tells you some city Ai and an integer Hi. He wants to make some tours starting from this city. He can choose any city in the Binary Country (including Ai) as the terminal city for a tour. He gains happiness (Hi - L) during a tour, where L is the distance between the city Ai and the terminal city.

Ralph is interested in tours from Ai in which he can gain positive happiness. For each query, compute the sum of happiness gains for all such tours.

Ralph will never take the same tour twice or more (in one query), he will never pass the same city twice or more in one tour.

## Input

The first line contains two integers n and m (1 ≤ n ≤ 106, 1 ≤ m ≤ 105).

(n - 1) lines follow, each line contains one integer Li (1 ≤ Li ≤ 105), which denotes the length of the i-th road.

m lines follow, each line contains two integers Ai and Hi (1 ≤ Ai ≤ n, 0 ≤ Hi ≤ 107).

## Output

Print m lines, on the i-th line print one integer — the answer for the i-th query.

input
2 2
5
1 8
2 4
output
11
4
input
6 4
2
1
1
3
2
2 4
1 3
3 2
1 7
output
11
6
3
28

## Note

Here is the explanation for the second sample.

Ralph's first query is to start tours from city 2 and Hi equals to 4. Here are the options:

He can choose city 5 as his terminal city. Since the distance between city 5 and city 2 is 3, he can gain happiness 4 - 3 = 1.
He can choose city 4 as his terminal city and gain happiness 3.
He can choose city 1 as his terminal city and gain happiness 2.
He can choose city 3 as his terminal city and gain happiness 1.
Note that Ralph can choose city 2 as his terminal city and gain happiness 4.
Ralph won't choose city 6 as his terminal city because the distance between city 6 and city 2 is 5, which leads to negative happiness for Ralph.
So the answer for the first query is 1 + 3 + 2 + 1 + 4 = 11.

## 题解

。。。看不懂题解没关系，读读代码就好了，代码很容易理解的。

## 代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;

int n,m;
vector<long long> l[maxn],s[maxn];
int t[maxn];
long long query(int x,long long h){
if(h<=0)return 0;
int p = upper_bound(l[x].begin(),l[x].end(),h)-l[x].begin()-1;
return (p+1)*h-s[x][p];
}
int main(){
scanf("%d%d",&n,&m);
for(int i=2;i<=n;i++){
scanf("%d",&t[i]);
}
for(int i=n;i>=1;i--){
l[i].push_back(0);
vector<int>b={i*2,i*2+1};
for(int x:b){
if(x>n)continue;
for(int y:l[x]){
l[i].push_back(y+t[x]);
}
}
sort(l[i].begin(),l[i].end());
s[i].resize(l[i].size());
for(int j=1;j<l[i].size();j++){
s[i][j]=s[i][j-1]+l[i][j];
}
}
for(int i=1;i<=m;i++){
int a,h;
scanf("%d%d",&a,&h);
long long ans = query(a,h);
while(a>1){
int b = a;
h-=t[a];
a>>=1;
if(h<=0)break;
ans+=h;
int x=a*2==b?a*2+1:a*2;
if(x<=n){
ans+=query(x,h-t[x]);
}
}
cout<<ans<<endl;
}
}

# E. Ralph and Mushrooms

Ralph is going to collect mushrooms in the Mushroom Forest.

There are m directed paths connecting n trees in the Mushroom Forest. On each path grow some mushrooms. When Ralph passes a path, he collects all the mushrooms on the path. The Mushroom Forest has a magical fertile ground where mushrooms grow at a fantastic speed. New mushrooms regrow as soon as Ralph finishes mushroom collection on a path. More specifically, after Ralph passes a path the i-th time, there regrow i mushrooms less than there was before this pass. That is, if there is initially x mushrooms on a path, then Ralph will collect x mushrooms for the first time, x - 1 mushrooms the second time, x - 1 - 2 mushrooms the third time, and so on. However, the number of mushrooms can never be less than 0.

For example, let there be 9 mushrooms on a path initially. The number of mushrooms that can be collected from the path is 9, 8, 6 and 3 when Ralph passes by from first to fourth time. From the fifth time and later Ralph can't collect any mushrooms from the path (but still can pass it).

Ralph decided to start from the tree s. How many mushrooms can he collect using only described paths?

## Input

The first line contains two integers n and m (1 ≤ n ≤ 106, 0 ≤ m ≤ 106), representing the number of trees and the number of directed paths in the Mushroom Forest, respectively.

Each of the following m lines contains three integers x, y and w (1 ≤ x, y ≤ n, 0 ≤ w ≤ 108), denoting a path that leads from tree x to tree y with w mushrooms initially. There can be paths that lead from a tree to itself, and multiple paths between the same pair of trees.

The last line contains a single integer s (1 ≤ s ≤ n) — the starting position of Ralph.

## Output

Print an integer denoting the maximum number of the mushrooms Ralph can collect during his route.

input
2 2
1 2 4
2 1 4
1
output
16
input
3 3
1 2 4
2 3 3
1 3 8
1
output
8

## Note

In the first sample Ralph can pass three times on the circle and collect 4 + 4 + 3 + 3 + 1 + 1 = 16 mushrooms. After that there will be no mushrooms for Ralph to collect.

In the second sample, Ralph can go to tree 3 and collect 8 mushrooms on the path from tree 1 to tree 3.

## 代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
vector<int> G[maxn],V[maxn],E[maxn],V2[maxn];
long long value[maxn],dp[maxn];
vector<int>p[maxn],q[maxn];
unordered_map<int,int>H;
stack<int> S;
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void dfs(int u){
S.push(u);
for(int i=0;i<G[u].size();i++)
{
int v = G[u][i];
if(!pre[v])
{
dfs(v);
}
else if(!sccno[v])
{
}
}
{
scc_cnt++;
do
{
u = S.top();S.pop();
sccno[u]=scc_cnt;
num[scc_cnt]++;
}
}
void find_scc(){
dfs_clock = scc_cnt = 0;
memset(sccno,0,sizeof(sccno));
memset(pre,0,sizeof(pre));
for(int i=0;i<n;i++)
if(!pre[i])
dfs(i);
}
long long cal_sum(long long x){
x++;
return x*(x-1)*(x+1)/(6ll);
}
long long calc(long long x){
long long l = 1, r = 1e5+7,mid,ans=0;
while(l<=r){
mid = (l+r)/2;
if(x>mid*(mid+1)/2){
ans=mid;
l=mid+1;
}else{
r=mid-1;
}
}
return x+x*ans-cal_sum(ans);
}
long long solve(int x){
if(dp[x]!=-1)return dp[x];
dp[x]=0;
for(int i=0;i<E[x].size();i++){
dp[x]=max(dp[x],solve(E[x][i])+V2[x][i]);
}
dp[x]+=value[x];
return dp[x];
}
int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
int a,b,c;
G[a].push_back(b);
V[a].push_back(c);
}
find_scc();
int tot = 0;
for(int i=1;i<=n;i++){
//cout<<sccno[i]<<" ";
if(!H.count(sccno[i])){
H[sccno[i]]=tot;
tot++;
}
p[H[sccno[i]]].push_back(i);
}
int start;
scanf("%d",&start);
//cout<<endl;
for(int i=0;i<tot;i++){
for(int j=0;j<p[i].size();j++){
for(int k=0;k<G[p[i][j]].size();k++){
int u = p[i][j];
int v = G[u][k];
if(sccno[u]==sccno[v]){
value[i]+=calc(V[u][k]);
//cout<<V[p[i][j]][k]<<" "<<calc(V[p[i][j]][k])<<endl;
}else{
E[i].push_back(H[sccno[v]]);
in[H[sccno[v]]]++;
V2[i].push_back(V[u][k]);
}
}
}
}
memset(dp,-1,sizeof(dp));
solve(H[sccno[start]]);
/*
for(int i=0;i<tot;i++){
cout<<i<<"------------"<<" "<<value[i]<<" "<<dp[i]<<endl;
for(int j=0;j<E[i].size();j++){
cout<<E[i][j]<<" "<<V2[i][j]<<endl;
}
}
*/
long long ans = 0;
for(int i=0;i<tot;i++){
ans = max(ans,dp[i]);
}
cout<<ans<<endl;
}
posted @ 2017-11-20 20:35 qscqesze 阅读(...) 评论(...) 编辑 收藏