码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1212 大整数的取模运算

时间:2015-01-01 00:09:45      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

因为这里是MOD最大为100000

所以我将字符串看作5个一组,并记录后面跟了多少个100000

每次取5个数根据其数据进行取模更新

注意过程中 100000*100000会超int

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define ll long long
int b;
char s[1005];

int main()
{
  //  freopen("a.in" , "r" , stdin);
    while(~scanf("%s%d" , s , &b))
    {
        int len = strlen(s);
        int p = len-1 , t = 0; //t表示后面要乘几次100000
        int cur = 0 ;
        while(p >= 0){
            ll num = 0 , mul = 1 ;
            for(int i = 0 ; i<5&&p>=0 ; i++){
                num += mul * (s[p] - 0);
                mul *= 10;
                p--;
            }
          //  cout<<"num: "<<num<<endl;
            num = num % b;
            for(int i = 1 ; i<=t ; i++){
                num *= 100000;
                num %= b;
            }
            num += cur;
            num %= b;
            cur = num;
            t++;
        }
        printf("%d\n" , cur);
    }
    return 0;
}

 

HDU 1212 大整数的取模运算

标签:

原文地址:http://www.cnblogs.com/CSU3901130321/p/4196799.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!