1. 程式人生 > >【數論】2016中國大學生程序設計競賽 - 網絡選拔賽 A. A water problem (大整數取模)

【數論】2016中國大學生程序設計競賽 - 網絡選拔賽 A. A water problem (大整數取模)

判斷 eight ron lin 大學生 con while php bubuko

鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=5832

技術分享圖片

技術分享圖片


題意:兩個星球,一個星球一年只有137天,一個星球一年只有73天

輸入N(爆炸後第N天),判斷這是否為這兩個星球的第一天

只要這個數是137與73的公倍數就好了(0比較特殊)

坑點:N的長度不超過10000000

只能用字符串來存儲

大整數整除:紫書P314

大整數整除:首先把大整數寫成“自左向右”的形式:1234 = (((1*10)+2)*10+3)*10+4

      然後每步取模

代碼

1 char n[10000010];
2 int m;
3 scanf("%s %d",n,&m);   // n 存儲大整數 
4 int len = strlen(n); 5 int ans = 0; 6 for(int i = 0;i < len;i++) 7 ans = (int)(((long long)ans*10+n[i]-0)%m); 8 printf("%d\n",ans);

代碼:

 1 #include <string>
 2 #include <cstdio>
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 const int m = 137*73;
 8
char n[10000010]; 9 int main() 10 { 11 int ca = 1; 12 while(~scanf("%s",n)) 13 { 14 int ans = 0; 15 int len = strlen(n); 16 for(int i = 0;i < len;i++) 17 ans = (int)(((long long)ans*10+n[i]-0)%m); 18 if(ans == 0) 19 printf("Case #%d: YES\n
",ca++); 20 else 21 printf("Case #%d: NO\n",ca++); 22 } 23 24 return 0; 25 }

【數論】2016中國大學生程序設計競賽 - 網絡選拔賽 A. A water problem (大整數取模)