一、codeforces
1.Good ol’ Numbers Coloring
题意:
Consider the set of all nonnegative integers: 0,1,2,…. Given two integers a and b (1≤a,b≤104). We paint all the numbers in increasing number first we paint 0, then we paint 1, then 2 and so on.
Each number is painted white or black. We paint a number i according to the following rules:
if i=0, it is colored white;
if i≥a and i−a is colored white, i is also colored white;
if i≥b and i−b is colored white, i is also colored white;
if i is still not colored white, it is colored black.
In this way, each nonnegative integer gets one of two colors.
For example, if a=3, b=5, then the colors of the numbers (in the order from 0) are: white (0), black (1), black (2), white (3), black (4), white (5), white (6), black (7), white (8), white (9), …
Note that:
It is possible that there are infinitely many nonnegative integers colored black. For example, if a=10 and b=10, then only 0,10,20,30 and any other nonnegative integers that end in 0 when written in base 10 are white. The other integers are colored black.
It is also possible that there are only finitely many nonnegative integers colored black. For example, when a=1 and b=10, then there is no nonnegative integer colored black at all.
Your task is to determine whether or not the number of nonnegative integers colored black is infinite.
If there are infinitely many nonnegative integers colored black, simply print a line containing “Infinite” (without the quotes). Otherwise, print “Finite” (without the quotes).
输入:
The first line of input contains a single integer t (1≤t≤100) — the number of test cases in the input. Then t lines follow, each line contains two space-separated integers a and b (1≤a,b≤104).
输出:
For each test case, print one line containing either “Infinite” or “Finite” (without the quotes). Output is case-insensitive (i.e. “infinite”, “inFiNite” or “finiTE” are all valid answers).
样例输入:
4
10 10
1 10
6 9
7 3
样例输出:
Infinite
Finite
Infinite
Finite
解题思路:
这题刚开始写的时候我根本毫无头绪,但是我看到6 9,真的是灵光一现,我觉得应该是有公因数或者两个数相等,就无限,否则就有限,我就抱着试试的心态,居然对了。嘻嘻,运气真好,虽然我现在还是不知道它的原因。
程序代码:
#include<iostream>
using namespace std;
const int N=1e4+5;
int t[N];
int main(){
int T;
scanf("%d",&T);
while(T--){
int a,b;
scanf("%d %d",&a,&b);
int mn,mx;
if(a>=b){
mx=a;
mn=b;
}else{
mx=b;
mn=a;
}
int flag=0;
for(int i=2;i<=mn;i++){
if(a%i==0 && b%i==0){
flag=1;
break;
}
}
// memset(t,0,sizeof(t));
// t[0]=1;
// int flag=0;
// int cnt=0;
// for(int i=1;i<=mx;i++){
// if((i>=mn&&t[i-mn]==1)){
// t[i]=1;
// cnt++;
// }
// }
if(flag){
printf("Infinite\n");
}else{
printf("Finite\n");
}
}
return 0;
}
2.Restricted RPS
题意:
Let n be a positive integer. Let a,b,c be nonnegative integers such that a+b+c=n.
Alice and Bob are gonna play rock-paper-scissors n times. Alice knows the sequences of hands that Bob will play. However, Alice has to play rock a times, paper b times, and scissors c times.
Alice wins if she beats Bob in at least ⌈n2⌉ (n2 rounded up to the nearest integer) hands, otherwise Alice loses.
Note that in rock-paper-scissors:
rock beats scissors;
paper beats rock;
scissors beat paper.
The task is, given the sequence of hands that Bob will play, and the numbers a,b,c, determine whether or not Alice can win. And if so, find any possible sequence of hands that Alice can use to win.
If there are multiple answers, print any of them.
输入:
The first line contains a single integer t (1≤t≤100) — the number of test cases.
Then, t testcases follow, each consisting of three lines:
The first line contains a single integer n (1≤n≤100).
The second line contains three integers, a,b,c (0≤a,b,c≤n). It is guaranteed that a+b+c=n.
The third line contains a string s of length n. s is made up of only ‘R’, ‘P’, and ‘S’. The i-th character is ‘R’ if for his i-th Bob plays rock, ‘P’ if paper, and ‘S’ if scissors.
输出:
For each testcase:
If Alice cannot win, print “NO” (without the quotes).
Otherwise, print “YES” (without the quotes). Also, print a string t of length n made up of only ‘R’, ‘P’, and ‘S’ — a sequence of hands that Alice can use to win. t must contain exactly a 'R’s, b 'P’s, and c 'S’s.
If there are multiple answers, print any of them.
The “YES” / “NO” part of the output is case-insensitive (i.e. “yEs”, “no” or “YEs” are all valid answers). Note that ‘R’, ‘P’ and ‘S’ are case-sensitive.
样例输入:
2
3
1 1 1
RPS
3
3 0 0
RPS
样例输出:
YES
PSR
NO
注意:
In the first testcase, in the first hand, Alice plays paper and Bob plays rock, so Alice beats Bob. In the second hand, Alice plays scissors and Bob plays paper, so Alice beats Bob. In the third hand, Alice plays rock and Bob plays scissors, so Alice beats Bob. Alice beat Bob 3 times, and 3≥⌈32⌉=2, so Alice wins.
In the second testcase, the only sequence of hands that Alice can play is “RRR”. Alice beats Bob only in the last hand, so Alice can’t win. 1<⌈32⌉=2.
解题思路:
就是知道对方的出拳顺序,让你判断你能不能赢,只要赢一半就行了。
刚开始忘了判断如果输了怎么出拳,最后才想到,加了判断就对了。
程序代码:
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main(){
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
int a,b,c;
int a1,b1,c1;
scanf("%d %d %d",&a,&b,&c);
a1=a;
b1=b;
c1=c;
string s1;
getchar();
getline(cin,s1);
// cout<<s1<<endl;
int len1=s1.length();
int cnt=0;
int cnt1=0;
char ans[100+5];
for(int i=0;i<len1;i++){
if(s1[i]=='R'){
if(b>=1){
b--;
ans[cnt++]='P';
cnt1++;
}else{
ans[cnt++]='k';
}
}else if(s1[i]=='P'){
if(c>=1){
c--;
ans[cnt++]='S';
cnt1++;
}else{
ans[cnt++]='k';
}
}else if(s1[i]=='S'){
if