zoukankan      html  css  js  c++  java
  • poj 2635

    大素数的问题,小心大数相乘时越界~~~~

      1 #include <iostream>
      2 #include <string>
      3 #include <vector>
      4 #include <cstdlib>
      5 #include <cmath>
      6 #include <map>
      7 #include <algorithm>
      8 #include <list>
      9 #include <ctime>
     10 #include <set>
     11 #include <cstring>
     12 #include <queue>
     13 #include <cstdio>
     14 typedef long long ll;
     15 #define CLR(arr, what) memset(arr, what, sizeof(arr))
     16 const int MAX = 1000000;
     17 const int pmax = 1000001;
     18 using namespace std;
     19 const int nmax = 20;
     20 int num[nmax];
     21 bool p[pmax];
     22 int pri[pmax];
     23 int prict;
     24 int n;
     25 void priList() { //   打素数表的模板
     26     int i, j;
     27     prict = 0;
     28     memset(p, true, sizeof(p));
     29     for (i = 2; i * i < pmax; i++)
     30         if (p[i]) {
     31             for (j = i << 1; j < pmax; j += i)
     32                 p[j] = false;
     33         }
     34     for (prict = 0, i = 2; i < pmax; i++)
     35         if (p[i])
     36             pri[prict++] = i;
     37 }
     38 
     39 int test(ll factor) {
     40     ll curnum = 0;
     41     ll nummax=MAX;
     42     ll nextnum;
     43     for (int i = 0; i < n; i++) {
     44         ll nextnum=num[n - 1 - i];
     45         curnum = curnum * MAX + nextnum;
     46         curnum = curnum % factor;
     47     }
     48     if (curnum)
     49         return 0;
     50     return 1;
     51 }
     52 
     53 
     54 int check(string t, int d) {
     55     int sz = t.size();
     56     int tmp = t[0] - '0';
     57     if (t.size() == 1 && tmp == 0 && d == 0) {
     58         return 1;
     59     }
     60     return 0;
     61 }
     62 
     63 void print_res(bool ok,int pos){
     64     if (ok) {
     65         printf("BAD %d
    ", pri[pos]);
     66     } else {
     67         printf("GOOD
    ");
     68     }
     69 }
     70 int main() {
     71     priList();
     72     string t;
     73     int d, sz;
     74     while (cin >> t >> d) {
     75         sz = t.size();
     76         n = 0;
     77         if (check(t, d)) {
     78             return 0;
     79         }
     80         reverse(t.begin(), t.end());
     81         for (int i = 0; i < sz;) {
     82             int cur = 0;
     83             int pow = 1;
     84             for (int j = 0; j < 6 && (i + j) < sz; j++) {
     85                 int curnum = (t[i + j] - '0');
     86                 cur = cur + curnum * pow;
     87                 pow *= 10;
     88             }
     89             num[n++] = cur;
     90             i = i + 6;
     91         }
     92         bool ok = false;
     93         int i;
     94         for (i = 0; i < prict; i++) {
     95             if (pri[i] >= d) {
     96                 break;
     97             } else {
     98                 ok = test(pri[i]);
     99             }
    100             if (ok)
    101                 break;
    102         }
    103         print_res(ok,i);
    104     }
    105     return 0;
    106 }

    from kakamilan

  • 相关阅读:
    L317 电子烟
    L316 波音737Max 危机
    2.19.3月 专业综合错题
    L314 单音节词读音规则(二)-元音字母发音规则
    L313 珊瑚裸鼠灭绝
    L312 难看懂的
    Pycharm写代码中文输入法不跟随
    Windows下Python多版本共存
    Python之批处理字符串(打开文件)
    Python Url请求代码
  • 原文地址:https://www.cnblogs.com/kakamilan/p/3161144.html
Copyright © 2011-2022 走看看