首页 > 代码库 > hdu1212(大数取模)

hdu1212(大数取模)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1212

题意:给出两个数a, b,求a%b;

 

思路:(c+d)%e=c%e+d%e,(c*d)%e=(c%e*d%e)%e;

 

代码:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #define ll long long
 5 #define MAXN 1000+10
 6 using namespace std;
 7 
 8 int main(void){
 9     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
10     int b;
11     char a[MAXN];
12     while(~scanf("%s%d", a, &b)){
13         int ans=0, cnt=1;
14         for(int i=strlen(a)-1; i>=0; i--){
15             int gg=(a[i]-0)*cnt;
16             ans=(ans+gg)%b;
17             cnt=cnt*10%b;
18         }
19         printf("%d\n", ans);
20     }
21     return 0;
22 }

据说java大数很厉害。。然后get了一下。。果然威力无穷。。。

代码:

 1 import java.math.BigDecimal;
 2 import java.util.Scanner;
 3 
 4 public class Main{
 5     public static void main(String args[]){
 6         Scanner scan = new Scanner(System.in);
 7         while(scan.hasNext()){
 8             BigDecimal a = scan.nextBigDecimal();
 9             int b = scan.nextInt();
10             System.out.println(a.remainder(new BigDecimal(b)));
11         }
12     }
13 }

 

hdu1212(大数取模)