1. 程式人生 > >894】 A【暴力】B【思維+尤拉降冪】C【構造+思維】

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<bits/stdc++.h>
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;i<n;i++){ if(s[i]=='Q') { for(int j=i+1;j<n;j++) { if(s[j]=='A') { for(int k=j+1;k<n;k++) { if(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<bits/stdc++.h>
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<bits/stdc++.h>
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;
}