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.
The only line contains a string of length n (1 ≤ n ≤ 100). It's guaranteed that the string only contains uppercase English letters.
Print a single integer — the number of subsequences "QAQ" in the string.
QAQAQYSYIOIWIN
4
QAQQQZZYNOIWIN
3
题解:
常见的题,记录'Q'出现的前缀,然后对于每个'A',答案加上前面'Q'的数量乘上后面'Q'的数量
1 #include2 #include 3 #include 4 #include 5 char s[1005]; 6 int a[1005],ans=0; 7 int main() 8 { 9 scanf("%s",s+1);10 int len=strlen(s+1);11 for(int i=1;i<=len;i++)12 {13 if(s[i]=='Q')14 a[i]=a[i-1]+1;15 else a[i]=a[i-1];16 }17 for(int i=1;i<=len;i++)18 {19 if(s[i]=='A')20 ans+=a[i-1]*(a[len]-a[i-1]);21 }22 printf("%d\n",ans);23 return 0;24 }
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.
The only line contains three integers n, m and k (1 ≤ n, m ≤ 1018, k is either 1 or -1).
Print a single number denoting the answer modulo 1000000007.
1 1 -1
1
1 3 1
1
3 3 -1
16
题解:
看到数据感觉是log的级别,快速幂?或者数学解?然后推了一堆没用的。最后写了个暴力打个表发现规律。
k=1:ans=2^(n-1)*(m-1)
k=-1:当(n+m)&1==1时为0,其他ans=2^(n-1)*(m-1)
记住(n-1)*(m-1)会爆long long,因此先算前面的然后再次快速幂。
1 #include2 #include 3 #include 4 #include 5 using namespace std; 6 const long long mod=(long long )(1e9+7); 7 inline long long pow(long long x,long long y) 8 { 9 long long ret=1;10 while(y)11 {12 if(y&1) ret=ret*x%mod;13 x=x*x%mod;14 y>>=1;15 }16 return ret;17 }18 int main()19 {20 long long n,m,k;21 scanf("%lld%lld%lld",&n,&m,&k);22 if(k==1) printf("%lld\n",pow(pow(2,n-1),m-1)%mod);23 else24 {25 if((n+m)%2==1) printf("0\n");26 else printf("%lld\n",pow(pow(2,n-1),m-1)%mod);27 }28 return 0;29 }
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. gcdhere means the .
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.
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.
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.
4 2 4 6 12
3 4 6 12
2 2 3
-1
题解:
显然最小的数x一定是所有数的最大公因数,否则无解。然后在每两个数之间插入一个x可以发现每个区间的gcd就是x,然后i==j的区间的gcd就是本身。所以是合法解。1 #include2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 int n,a[10005]; 8 int main() 9 {10 scanf("%d",&n);11 for(int i=1;i<=n;++i)12 {13 scanf("%d",a+i);14 if(a[i]%a[1]!=0)15 {16 printf("-1");17 return 0;18 }19 }20 printf("%d\n",2*n);21 for(int i=1;i<=n;++i)22 printf("%d %d ",a[i],a[1]);23 return 0;24 }