【CodeForces - 894】 A【暴力】B【思维+欧拉降幂】C【构造+思维】

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!).


———–钻石小姐姐啊 prprpr TAT

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.

Example
Input
QAQAQYSYIOIWIN
Output
4
Input
QAQQQZZYNOIWIN
Output
3
Note
In the first example there are 4 subsequences “QAQ”: “QAQAQYSYIOIWIN”, “QAQAQYSYIOIWIN”, “QAQAQYSYIOIWIN”, “QAQAQYSYIOIWIN”.

水题 暴力

#include
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 1e5;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

char s[10000];
int main(){
    CLOSE();
//  fread();
//  fwrite();
    scanf("%s",s);
    int n=strlen(s);
    int cnt=0;
    for(int i=0;iif(s[i]=='Q') {
            for(int j=i+1;jif(s[j]=='A') {
                    for(int k=j+1;kif(s[k]=='Q') cnt++;
                    }
                }
            }
        }
    } 
    printf("%d\n",cnt);
    return 0;
}

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.

Example
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.
分析 : 我们可知 ,填入的数字一定只有1或-1 。
现在问题就成了,一个n*m的矩阵,我们可以往其中填数1或-1,使每一列和每一行的乘积都为k。
对于每一行来说,不管前面填的什么数字,我们都可以用最后一个数字来使其满足题意,同理,列也是如此,但是对于最右下角的数字,为什么填一个数字就可以满足一个行和列的情况,脑补一下好了。情况数目为 pow(2 , (n-1) * (m-1) ),但是 n * m 爆LL ,所以我们可以用欧拉定理来降幂,phi (1e9+7) = 1e9+7 -1 .
所以 最后答案 为 pow(2 ,(((n-1)%phi)*((m-1)%phi)+phi)%phi )
关键是什么时候没有办法填数。 当n和m互为奇偶时候 无法填数。
证明: 假设n为奇数,m为偶数。
假如k=1时候,全部都填1,至少有一种情况。
假如k=-1时候,
对于所有行 每个行都要有奇数个 -1 . 总共就要有奇数n*奇数=偶数个-1.
同理所有列 总共就要有偶数n*奇数=奇数个-1.
互相矛盾。构不成

#include
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 1e5;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const int phi = mod - 1 ;

LL qpow(LL a,LL b,LL c){
    LL s=1,base=a%c;
    while(b){
        if(b&1) s=s*base%c;
        base=base*base%c;
        b>>=1;
    }
    return s;
}
int main(){
    CLOSE();
//  fread();
//  fwrite();
    LL n,m,k;
    cin>>n>>m>>k;
        if((n+m)&1&&k==-1) puts("0");
    else 
        printf("%lld\n",qpow(2,(((n-1)%phi)*((m-1)%phi)+phi)%phi,mod));
    return 0;
}

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.

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

分析: 我们可以知道set中最小的数字一定为 gcd(a1,a2,…..an) ,所以set中其他数字一定要是set[1]的倍数,否则就无法构成,如果可以的话,我直接就把set中的数字都输出出来,果然wa了。(没有注意一个重要的条件)之后找了一组数据 对于set 15 60 90 ,这三个数字,我们怎么构造, 60 和90 的gcd为30 (重要的题意 : set中数字为 原序列的【子序列】的gcd ),所以我们要将60和90隔开就行了,但是也不能添加新的数字(我们能够用的只有set中数字,想一下就知道)添加一个数字一定会使gcd 的值减少(或者不变),所以我们可以添加set[1],这样的话, 直接就会将gcd降到set[1],这样一定就会在set中,满足题意。

代码

#include
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 1e5;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

LL gcd(LL a,LL b) { return b==0?a:gcd(b,a%b); }

int arr[MAXN];
int main(){
    CLOSE();
//  fread();
//  fwrite();
     int n;scanf("%d",&n);int flag=1;
     for(int i=1;i<=n;i++) {
        scanf("%d",&arr[i]);
        if(i>1) {
            if(arr[i]%arr[1]) flag=0;
         }
     }
     if(!flag) puts("-1");
     else{
        printf("%d\n",2*n);
        for(int i=1;i<=n;i++) printf("%d %d ",arr[i],arr[1]);
    }
    return 0;
}

你可能感兴趣的:(思维,CF,刷题之旅)