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

Posted qscqesze

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #447 (Div. 2) 题解 ABCDE相关的知识,希望对你有一定的参考价值。

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.

Examples

input
QAQAQYSYIOIWIN
output
4
input
QAQQQZZYNOIWIN
output
3

Note

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

题意

问你有多少个QAQ子序列

题解

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.

Examples

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.

题意

给你nm的矩阵,问你有多少种填数的方案,使得每一行和每一列的乘积都是k

题解

唯一的hack点,无解的情况。
当n,m不同为奇偶的时候,k=-1时无解。

若存在答案,n为奇数,m为偶数,k=-1。
每一行的乘积为-1,整个矩形的乘积就是(-1)^n=-1。
每一列的乘积为-1,整个矩阵的乘积为(-1)^m=1
冲突,故无解。。

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.

Examples

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.

题意

有一个序列,然后有一个询问q(l,r) = gcd(a[l],a[l+1],...,a[r-1],a[r]),然后把所有可能的q都插入到一个set里面。

现在给你这个set,让你找到一个可能的原序列。

题解

构造题,输出s[1],s[1],s[2],s[1],s[3],s[1].....,s[n-1],s[1],s[n],s[1]就好

每次输出s[i],s[1],这样就使得连续的长度大于1的gcd都为s[1],然后长度为1的都是自己。

最后再check一下是否有解。

代码

#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.

Examples

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.

题意

有n个城市,i和2i存在一条边,i和2i+1也连一条边。
给你每条边的长度。
现在给你m个询问,每个询问给你x和h,表示起点为x,然后让你求sigma(max(h-dist[i],0))

题解

每个点都是往后连的,我们定义函数query(x,y)表示起点为x,与他相连且只考虑比他大的点,能获得的快乐一共是多少。

然后我们再不停的/2去算这个log就好了。

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

代码

#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.

Examples

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.

题意

给你n个点m条边的有向图,每条边都会有蘑菇,一开始第i个边有a[i]个蘑菇,第一次经过能获得a[i]个,第二次能获得a[i]-1个,第三次能获得a[i]-1-2个,直到a[i]非负。

现在给你起点,问你最多能获得多少个蘑菇。

题解

一个强连通里面,一定能把所有边的蘑菇都采完。

然后缩点之后就是一个dag了,跑个dp就好了。

中间算一个边能取多少个蘑菇的时候,用二分就好了。

好像有点卡常,把map改成unordered_map才过。。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
vector<int> G[maxn],V[maxn],E[maxn],V2[maxn];
int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt,num[maxn],in[maxn],n,m;
long long value[maxn],dp[maxn];
vector<int>p[maxn],q[maxn];
unordered_map<int,int>H;
stack<int> S;
inline int read()
{
    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){
    pre[u]=lowlink[u]=++dfs_clock;
    S.push(u);
    for(int i=0;i<G[u].size();i++)
    {
        int v = G[u][i];
        if(!pre[v])
        {
            dfs(v);
            lowlink[u]=min(lowlink[u],lowlink[v]);
        }
        else if(!sccno[v])
        {
            lowlink[u]=min(lowlink[u],pre[v]);
        }
    }
    if(lowlink[u]==pre[u])
    {
        scc_cnt++;
        do
        {
            u = S.top();S.pop();
            sccno[u]=scc_cnt;
            num[scc_cnt]++;
        }while(pre[u]!=lowlink[u]);
    }
}
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;
        a=read(),b=read(),c=read();
        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;
}

以上是关于Codeforces Round #447 (Div. 2) 题解 ABCDE的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #447 (Div. 2)

Codeforces Round #447 (Div. 2) A

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

Codeforces Round #447 (Div. 2)

Codeforces Round #447 (Div. 2) AQAQ

Codeforces Round #447 Div. 2