LightOJ 1214 Large Division(大整数取模)

题目链接:
LightOJ 1214 Large Division
题意:
大整数取模。输入a,b(-10^200 <= a <= 10^200, b != 0, b是int),判断a能否整除b。
分析:
把大整数写成“从左向右”的形式:如:1234 = ((1 * 10 + 2) * 10 + 3) * 10 + 4.然后根据(n + m) % p = ((n % p) + (m % p)) % p,每步去模。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;
const int MAX_N = 250;

int T, cases = 0, p;
char s[MAX_N];

int main()
{
    scanf("%d", &T);
    while(T--){
        scanf("%s%d", &s, &p);
        printf("Case %d: ", ++cases);
        int len = strlen(s);
        if(s[0] == '0' && len == 1){
        printf("divisible\n");
        continue;
        }
        if(p < 0) p = -p;
        int ans = 0, st = 0;
        if(s[0] == '-') st = 1;
        for(int i = st; i < len; i++){
        ans = (int)(((ll) ans * 10 + s[i] - '0') % p);
        }
        if(ans == 0) printf("divisible\n");
        else printf("not divisible\n");
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值