Advertisement
cmiN

Dk

Jun 9th, 2011
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #define len 3
  3.  
  4. const short base[len] = {2, 7, 61};
  5. int total;
  6.  
  7. inline int logpow(long long bs, int ex, const int mod)
  8. {
  9.     long long res = 1;
  10.     while (ex) {
  11.         if (ex & 1) {
  12.             res = res * bs % mod;
  13.             --ex;
  14.         }
  15.         bs = bs * bs % mod;
  16.         ex >>= 1;
  17.     }
  18.     return (int)res;
  19. }
  20.  
  21. inline short process(int nr)
  22. {
  23.     short tmp, i, s, a;
  24.     int aux = 1, d;
  25.     --nr;
  26.     for (i = 0; aux <= nr; ++i) {
  27.         if (!(nr % aux)) {
  28.             s = i;
  29.             d = nr / aux;
  30.         }
  31.         aux <<= 1;
  32.     }
  33.     //fprintf(stderr, "%d %d\n", s, d);
  34.     for (i = 0; i < len; ++i) {
  35.         a = base[i];
  36.         aux = logpow(a, d, nr + 1);
  37.         //fprintf(stderr, "%d %d\n", a, aux);
  38.         if (aux != 1 && aux != nr) {
  39.             for (tmp = 0; tmp < s; ++tmp) {
  40.                 if (aux == s) {
  41.                     return 0;
  42.                 }
  43.                 aux = logpow(aux, 2, nr + 1);
  44.                 if (aux == 1) {
  45.                     return 0;
  46.                 } else if (aux == nr) {
  47.                     break;
  48.                 }
  49.             }
  50.         }
  51.     }
  52.     return 1;
  53. }
  54.  
  55. int main()
  56. {
  57.     short n;
  58.     int a, b;
  59.     freopen("print.in", "rt", stdin);
  60.     for (scanf("%hd", &n); n; --n) {
  61.         scanf("%d %d", &a, &b);
  62.         if (a == 2) {
  63.             puts("2");
  64.             ++a;
  65.         }
  66.         if (a == 3 && a <= b) {
  67.             puts("3");
  68.             a += 2;
  69.         }
  70.         if (!(a & 1)) {
  71.             ++a;
  72.         }
  73.         while (a <= b) {
  74.             if (process(a)) {
  75.                 printf("%d\n", a);
  76.             }
  77.             a += 2;
  78.         }
  79.     }
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement