Round #447(Div 2)

A. QAQ
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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

illustration by 猫屋 https://twitter.com/nekoyaliu

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

先从后往前遍历一遍,记录Q的个数,再从前往后遍历,

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lowbit(x) (x&(-x))
 4 #define max(x,y) (x>=y?x:y)
 5 #define min(x,y) (x<=y?x:y)
 6 #define MAX 100000000000000000
 7 #define MOD 1000000007
 8 #define pi acos(-1.0)
 9 #define ei exp(1)
10 #define PI 3.1415926535897932384626433832
11 typedef long long ll;
12 #define INF 0x3f3f3f3f
13 #define maxn 200
14 char a[maxn];
15 int dp[maxn];
16 
17 int main(){
18     ios::sync_with_stdio(0);
19     scanf("%s",&a);
20     int len=strlen(a);
21     dp[len]=0;
22     for(int i=len-1;i>=0;i--){
23         dp[i]=dp[i+1];
24         if(a[i]=='Q') dp[i]++;
25     }
26     int ans=0,sum=0;
27     for(int i=0;i<len;i++){
28         if(a[i]=='Q') ans++;
29             else if(a[i]=='A') sum+=ans*dp[i];
30     }
31     printf("%d
",sum);
32     return 0;
33 }
B. Ralph And His Magic Field
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Ralph has a magic field which is divided into n × m blocks. That is to say, there are n rows and mcolumns 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 nm 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.

题意:在  n * m 个格子上放整数 , 使得 每行 、每列 数字的乘积为 k (k == 1 || k == -1) ; 则这些 n*m个空格上只能放 1 || -1 ,

      每一个空格都可以放 1 || -1 而且在每一行或者每一列都可以 通过放下最后一个来改变这一行或者一列的乘积从而达到题目的要求。

思路:每一行和每一列的最后一个空格留下来 改变这一行或者这一列的状态,其他所有的空格都是可以随意放的 所以就有 2^((n-1)*(m-1))的情况

     另外注意 当 k == -1 时 假设所有的空格都放置 -1 如果 n 和 m 都是奇数 或者偶数 可以 通过把 一部分 -1 换成 1 来满足题目的要求 ,

          但是 当 n m 一个奇数 一个 偶数的时候 , 使一列有 奇数个 -1 则必回 时一行有 偶数个 -1 则无法满足需求 !!!

          又因为n和m都特别大,所以需要自己写一个快速幂。如果n和m的奇偶性不同且k是-1,就一定没有办法填数

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lowbit(x) (x&(-x))
 4 #define max(x,y) (x>=y?x:y)
 5 #define min(x,y) (x<=y?x:y)
 6 #define MAX 100000000000000000
 7 #define MOD 1000000007
 8 #define pi acos(-1.0)
 9 #define ei exp(1)
10 #define PI 3.1415926535897932384626433832
11 typedef long long ll;
12 #define INF 0x3f3f3f3f
13 const int mod=1e9+7;
14 
15 ll q_pow(ll n,ll m){
16     ll  ans=1;
17     while(m){
18         if(m&1) ans=ans*n%mod;
19         m>>=1;
20         n=n*n%mod;
21     }
22     return ans;
23 }
24 int main(){
25     ios::sync_with_stdio(0);
26     ll n, m,k;
27     while(cin>>n>>m>>k){
28         if(((n%2)!=(m%2))&&k==-1) cout<<0<<endl;
29             else cout<<q_pow(q_pow(2,n-1),m-1)<<endl;
30     }
31     return 0;
32 }
原文地址:https://www.cnblogs.com/z-712/p/7886613.html