前言
听说上回出的分支结构题很简单?这回难度怎么样(何意味)。
T1. Key-Check (密码检验)
本题题目较简单,考察对于基本类型的变量的处理及对输入输出语句的灵活应用。
Main Idea:
发现题目给出的字符串 根本不需要存储或用到 string 等较高级的数据类型。
而是可以通过记录上一次出现的字符(记为 lst,即公式中 )来实现仅用 char 或 int 类型来存储。
计算公式可以为(现今字符记为 now,即公式中 ):
ans=(ans+now+lst*1013)%mod;
最后只要不断更新 lst,使 lst=now。
Idea 1:
发现 scanf 的一种应用可以为 scanf("%c;",&now),即读入时自动忽略 ; 字符并正确读入 now。
但是注意这样的读入在代码中不会停止,注意加上 !=EOF 的判断(以便程序在评测机上自动终止)。
Solution 1 (Code By mengnn):
#include<bits/stdc++.h>
using namespace std;
const int mod=998244853;
int ans;char lst=0,now;
int main(){
while (scanf("%c;",&now)!=EOF){
ans=(ans+now+lst*1013)%mod;
lst=now;
}
cout<<ans;
return 0;
}Idea 2:
如果不会用 scanf() 的读入方式,建议使用更简单的 cin。
注意到当 cin 读到输入文段末尾的时候会返回 false。
利用这一特性,判断读入时的 ; 符并跳过循环即可。
Solution 2 (Code By SP_beyond_xxxx):
#include<bits/stdc++.h>
using namespace std;
const int mod=998244853;
char lst,now;
int ans;
int main(){
while(cin>>now){
if(now==';')continue;
ans=(ans+now+(1013*lst)%mod)%mod;
lst=now;
}cout<<ans;
return 0;
}注意
- 模数 而不是 。
- 在本地评测(测样例)时,用本题解给出的代码并不能给出答案,而是会不断的读入。此时若输入完成(打完样例输入),应先输入一个换行,再同时按下
Ctrl键和Z键输入进一个^Z符号,换行即可正确评测。
本题的 Solution 1 代码本地可能并不能评测(可以使用 Luogu IDE 评测),所以更见一些 Solution 2 的代码。
T2. Key-Decrypt (密码破译)
本题题目较难,但考虑到大家应该也在数学上学过最大公约数的概念及其运算方式,数学比较好的同学做本题时应该也感觉到很简单吧。
Main Idea:
Solution (Code By mengnn):
#include<bits/stdc++.h>
using namespace std;
int T;long long a,b;
int main(){
cin>>T;
while (T--){
cin>>a>>b;
while (__gcd(a,b)!=1) a=__gcd(a,b),b/=a;
if (b==1) puts("N0");
else cout<<b<<'\n';
}
return 0;
}注意
本题无解时输出的是 N0,不是 NO(如果你除 2,3 数据全 WA)。