目录
A1. Counterexample
A2. Good Number
A3. Dice Tower
★A4. Alyona and Numbers
A5. Mountain Scenery
rzt A6. Help Vasilisa the Wise 2
A7. Chewbaсca and Number
★A8. Ksenia and Pan Scales
★A9. Launch of Collider
A10. Polo the Penguin and Segments
★A11. IQ Test
★A12. Yaroslav and Permutations
★A13. Snow Footprints
★A14. Raising Bacteria
★A15. BowWow and the Timetable
A16. Balanced Rating Changes
A1. Counterexample
Problem - A - Codeforces
题意:
求在 l 到 r 之间满足a,b的最大公约数等于 b ,c 的最大公约数等于 1 ,且 a,c的最大公约数不等于 1 的数 a,b ,c
三个for循环暴力查找+开long long
十年OI一场空,不开long long见祖宗
#include#define ll long long using namespace std; ll gcd(ll a,ll b) { return b? gcd(b,a%b):a; } int main() { ll l,r; cin>>l>>r; for(ll i=l;i<=r;i++) for(ll j=i;j<=r;j++) for(ll k=j;k<=r;k++) if(gcd(i,j)==1&&gcd(j,k)==1&&gcd(i,k)!=1) { cout< A2. Good Number
Problem - A - Codeforces
题意:
第一行输入n和k n—n行数据
接下来n行输入n个数
如果该数的各个位数包含0~k的每个数 则该数为k-好数
求k-好数的个数
用标记法
#include#include using namespace std; int main() { int n,k,x,sum=0,i; int index[9]={0}; cin>>n>>k; while(n--) { cin>>x; for(i=0;i<=k;i++) index[i]=0; while(x) { index[x%10]=1; x/=10; } for(i=0;i<=k;i++) if(index[i]==0) break; if(i==k+1) sum++; } cout< A3. Dice Tower
Problem - A - Codeforces
题意:
第一行输入骰子数n
第二行输入第一个骰子的顶面
接下来n行输入骰子的正面 和侧面
注意叠起来的骰子塔相连的部分应该是一样的
判断给定数据是否能叠成一个合格的骰子塔
我们只需要一个一个骰子分析
给的正面和侧面是否合格 不合格输出NO 反之YES
然后每次循环更新一下顶面数据即可
#includeusing namespace std; int main() { int n,top,a,b; cin>>n; cin>>top; while(n--) { cin>>a>>b; if(a==b||a+b==7||a==top||b==top||a==7-top||b==7-top) { cout<<"NO"; return 0; } top=7-top; } cout<<"YES"; return 0; } ★A4. Alyona and Numbers
Problem - A - Codeforces
题意:
给定两个数n, m(1 <= n,m <= 1000000)。分别从1~n, 1~m中选择两个数,使它们的和为5的倍数,求这样的数对共有多少(a+b, b+a视为不同的数对)。
暴力双for肯定不行的 会TLE(要不然也太简单了)
这题是数论题……啊烦人的数学
思路:
想要两个数相加为 5 的倍数,那它们除5的余数相加必须等于 5
或者两者除5的余数都为0
本题要计算满足两数之和是5倍数的数对个数
那么就是先各找出n m范围内余数为0 1 2 3 4的个数 存数组里
然后根据排列组合的原理算出结果即可(因为题目说a+b和b+a算不同的数对)
#includeusing namespace std; long long n,m; long long a[5],b[5]; int main() { cin>>n>>m; for(int i=1;i<=n;i++) a[i%5]++; for(int i=1;i<=m;i++) b[i%5]++; cout< A5. Mountain Scenery
Problem - A - Codeforces
题意:
第一行给出n k
第二行给出序列
比如: 3 2
0 5 3 5 1 5 2
其中偶数位 - 1仍然满足大于前后两个数 题目要求其中有2个下标为偶数的数相比输入的序列被减了1 也就是可以是0 4 3 4 1 5 2
这题有sqj 所以跟样例不同很正常
#includeusing namespace std; int main() { int n,k,a[201],i; cin>>n>>k; for(i=1;i<=2*n+1;i++) cin>>a[i]; for(i=2;i<=2*n+1;i+=2) { if(a[i]-1>a[i-1]&&a[i]-1>a[i+1]) k--,a[i]--; if(k==0) break; } for(i=1;i<=2*n+1;i++) cout< rzt A6. Help Vasilisa the Wise 2
Problem - A - Codeforces
题意:
有一个 2×2 的棋盘,有 9 个棋子,上面分别写着 1,2,3,…9。现在给出每行每列及每条对角线上两个棋子 的数之和,求相应的棋子摆放方法。无解输出 -1
暴力枚举即可 O(9^4)
#includeusing namespace std; int main() { int r1,r2,c1,c2,d1,d2; cin>>r1>>r2>>c1>>c2>>d1>>d2; for(int i1=1;i1<=9;i1++) for(int i2=1;i2<=9;i2++) for(int i3=1;i3<=9;i3++) for(int i4=1;i4<=9;i4++) if(i1+i2==r1&&i3+i4==r2&&i1+i3==c1&&i2+i4==c2&&i1+i4==d1&&i2+i3==d2&&(i1!=i2)&&(i1!=i3)&&(i1!=i4)&&(i2!=i3)&&(i2!=i4)&&(i3!=i4)) { cout< A7. Chewbaсca and Number
Problem - A - Codeforces
题意:
#include#include using namespace std; int main() { string a; cin>>a; for(int i=0;i ★A8. Ksenia and Pan Scales
Problem - A - Codeforces
题意:
令人头大的一道题 烦死了呜呜
思路:
先计算天平两侧的初始个数 l 和 r
根据下图思路划定中位线(找规律)
然后进行分配 如果左右个数不等 则输出impossible
否则输出结果就行
#includeusing namespace std; int main() { string l,r,a,b; int flag=0,len=0,ren=0; cin>>a>>b; int lenb=b.size(); for(int i=0;i ★A9. Launch of Collider
Problem - A - Codeforces
题意:
找规律即可发现只有 RL这种情况才能碰撞(一个向右 一个向左)
所以只要找到RL这种组合 然后循环比较出最小时间即可
时间=后面-前面 / (v1+v2)
注意0x3f3f3f3f是最大数
#include#include using namespace std; int main() { int n,res=0x3f3f3f3f; string d; cin>>n>>d; int a[n]; for(int i=0;i >a[i]; for(int i=1;i A10. Polo the Penguin and Segments
Problem - A - Codeforces
题意:
这题根本不用管区间的l和r数是什么 只要管该区间所包含的整数数量
用sum把整数数量和求出 因为只能将区间扩展 意思是只能sum++
也就是求sum%k==0 时 sum++的次数
#includeusing namespace std; int main() { int n,k,l,r,sum=0,cnt=0; cin>>n>>k; while(n--) { cin>>l>>r; sum+=r-l+1; } while(sum%k!=0) { sum++; cnt++; } cout< ★A11. IQ Test
Problem - A - Codeforces
题意:
这题究极找规律 看了下面这篇题解 看完我顿悟了(没有)
但其实理解错了 我第一次以为是统计4×4的表格 但其实是统计2×2的表格
大佬的思路太强了 这方法的代码又简短又易懂 找规律yyds
#includeusing namespace std; int q(char ch) { return ch=='.'? -1:1; } int main() { char a[4][4]; int i,j,flag=0; for(i=0;i<4;i++) for(j=0;j<4;j++) cin>>a[i][j]; for(i=0;i<3;i++) for(j=0;j<3;j++) //或运算 如果出现1 flag就为1 flag|=q(a[i][j])+q(a[i+1][j])+q(a[i][j+1])+q(a[i+1][j+1]); if(!flag) cout<<"NO";//如果循环完flag都是0 则说明没有符合题意的2×2方格 else cout<<"YES"; }
★A12. Yaroslav and Permutations
Problem - A - Codeforces
题意:
有一个数组,现在你要在有限的次数内移动相邻的两个数,最后是否能使数组中没有数字相同的相邻元素。
思路:
想要元素不相邻,即同一个元素要间隔放,已知相同一个元素最多可以这样
k a k b k c k n=7 k出现4次
即最多可以出现(n+1)/ 2次,只要k出现的次数小于该最大次数,就能实现间隔排放
其中k为该数组中出现次数最大的数
#includeusing namespace std; int main() { int n,x,maxn=0,index[1001]={0}; cin>>n; for(int i=1;i<=n;i++) { cin>>x; index[x]++; if(maxn ★A13. Snow Footprints
Problem - A - Codeforces
题意:
这题1300 而且是spj题 有1000种解法
思路:
三种情况:只有L 只有R 和RRLL这种 至于LLRR这种模拟一下就知道不可能
#includeusing namespace std; int main() { int n; string s; cin>>n>>s; int lst=-1,led=-1,rst=-1,red=-1; for(int i=0;i ★A14. Raising Bacteria
Problem - A - Codeforces
题意:
每个细菌每天分裂成2个新细菌可以得到,要看到x个细菌就是求x里面有多少个2^n,2^n-1……2^0 由二进制的定义可得,这个题目就是求x在二进制下有多少个1
#include#include using namespace std; int main() { int n,cnt=0; cin>>n; while(n) { if(n%2==1) cnt++; n/=2; } cout< ★A15. BowWow and the Timetable
Problem - A - Codeforces
题意:
我的思路是先把二进制转十进制 然后再在十进制数里找有多少个4的次方 cnt计数
但是卡第四个test 看了一下发现是精度问题(kao)
错误代码:
#include#include using namespace std; signed main() { long long int n,sum=0,t=0,i=0,cnt=0; cin>>n; while(n) { sum+=n%10*pow(2,i++); n/=10; } i=0; while(t =sum) break; cnt++; } cout< AC代码:
#includeusing namespace std; int main() { int cnt=0; string s,p="1"; cin>>s; while(s.size()>=p.size()) { if(s>p) cnt++; p+="00"; } cout< A16. Balanced Rating Changes
Problem - A - Codeforces
题意:



