22周赛 - Virtual Judge (vjudge.net)
https://vjudge.net/problem/LightOJ-1214/origin
Input starts with an integer T (≤ 525), denoting the number of test cases.
Each case starts with a line containing two integers a (-10200 ≤ a ≤ 10200) and b (|b| > 0, b fits into a 32 bit signed integer). Numbers will not contain any leading zeroes.
a能否被b整除
卡壳原因是mod没开long long,没有重视debug的重要步骤,爆范围的bug出现率目前是排行第一的,优先检查
#include
#include
#include
#include
using namespace std;
typedef long long ll;
int main(){int t;cin >> t;for(int tt = 1;tt <= t;tt++){string s;ll m;cin >> s >> m;if(s[0] == '-') s.erase(0,1);ll mod = 0;for(int i = 0;i < s.size();i++){mod = ((mod * 10 % m) + (s[i] - '0') % m ) % m;}if(!mod)printf("Case %d: divisible\n",tt);else printf("Case %d: not divisible\n",tt);}return 0;
}
我本来是把思路记在自己的word文档里的,但为了帮助想搞懂题意的朋友们,还是把思路粘在文章最末尾
我的思路是没什么思路,只有一个两百次方的数肯定要用字符串来存
没见过此类大数取模的题,吃亏了
题解思路,字符串存数字,然后由同余定理,1234 % m等价于1000 % m + 200 % m + 3 % m + 4 % m
答案巧妙在于从左边开始枚举数位,组成一个数,来不断模m,通过数位操作使忽略后导零也合法。
字符串起始位置根据正负的不同,起点也不同,for循环的i在外面定义,根据字符串正负来取不同的值作为for循环初始可以较好的解决这个问题
此外s.erase()也是不错的选择
上一篇:机器学习经典算法:感知机PLA
下一篇:CAS简单理解