結果
問題 | No.577 Prime Powerful Numbers |
ユーザー | Ryuhei Mori |
提出日時 | 2017-10-27 18:37:59 |
言語 | C (gcc 12.3.0) |
結果 |
AC
|
実行時間 | 17 ms / 2,000 ms |
コード長 | 3,495 bytes |
コンパイル時間 | 1,128 ms |
コンパイル使用メモリ | 31,360 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-21 21:20:43 |
合計ジャッジ時間 | 1,133 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 4 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 17 ms
5,248 KB |
testcase_06 | AC | 3 ms
5,248 KB |
testcase_07 | AC | 17 ms
5,248 KB |
testcase_08 | AC | 4 ms
5,248 KB |
testcase_09 | AC | 4 ms
5,248 KB |
testcase_10 | AC | 1 ms
5,248 KB |
ソースコード
#include <stdio.h> #include <stdint.h> typedef unsigned __int128 uint128_t; void ex_gcd(uint64_t y, uint64_t *a, uint64_t *b){ int i; uint64_t u, v; u = 1; v = 0; uint64_t x = 1LL<<63; for(i=0;i<64;i++){ if(u&1){ u = (u + y) / 2; v = v/2 + x; } else { u >>= 1; v >>= 1; } } *a = u; *b = v; } uint64_t MR(uint128_t x, uint64_t m, uint64_t n){ uint64_t y = (uint64_t) x * m; uint64_t z = ((uint128_t) y * n + x) >> 64; return z < n ? z : z - n; } uint64_t RM(uint64_t x, uint64_t n){ return ((uint128_t) x << 64) % n; } uint64_t modpow64(uint64_t a, uint64_t k, uint64_t m, uint64_t n){ uint64_t r; for(r=RM(1, n);k;k/=2){ if(k&1) r = MR((uint128_t)r*a, m, n); a = MR((uint128_t) a*a, m, n); } return r; } uint64_t modpow_o(uint64_t a, uint64_t k, uint64_t n){ uint64_t r; for(r=1;k;k/=2){ if(k&1) r = (uint128_t)r*a% n; a = (uint128_t) a*a % n; } return r; } const uint64_t as64[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}; int is_prime64(uint64_t n){ int i, j, r; uint64_t d, one, mone, s, m; if(n <= 1) return 0; if(n <= 3) return 1; if(!(n & 1)) return 0; r = __builtin_ctzll(n-1); d = (n-1) >> r; ex_gcd(n, &s, &m); one = RM(1, n); mone = n - one; for(i=0;i<7;i++){ uint64_t a = RM(as64[i]%n, n); if(a == 0) return 1; /* uint64_t t = MR(modpow64(a, d, m, n), m, n); if(t == 1) continue; for(j=0;t!=n-1;j++){ if(j == r-1) return 0; t = (uint128_t) t * t % n; if(t == 1) return 0; } */ uint64_t t = modpow64(a, d, m, n); if(t == one) continue; for(j=0;t!=mone;j++){ if(j == r-1) return 0; t = MR((uint128_t) t * t, m, n); if(t == one) return 0; } } return 1; } uint64_t gcd64(uint64_t x, uint64_t y){ if(x == 0 || y == 0) return x^y; int bx = __builtin_ctzll(x); int by = __builtin_ctzll(y); int k = (bx < by) ? bx : by; x >>= bx; y >>= by; while(x!=y){ if(x < y){ y -= x; y >>= __builtin_ctzll(y); } else { x -= y; x >>= __builtin_ctzll(x); } } return x << k; } int is_power64(uint64_t n, uint64_t p){ int i; uint64_t a[49]; uint64_t x; a[0] = p; a[1] = p * p; for(i=1; a[i] <= n && a[i] > a[i-1]; i++){ a[i+1] = a[i] * a[i]; } /* if(a[--i] == n) return 1; while(i){ if(n % a[i]) return 0; n /= a[i]; while(i > 0 && n < a[--i]) ; } */ x = a[--i]; if(x == n) return 1; for(--i; i>=0;--i){ uint64_t y = x * a[i]; if(y == n) return 1; else if(y < x) return 0; else if(y < n) x = y; } return 0; } uint64_t is_primepower64(uint64_t n){ uint64_t i; if(n <= 1) return 0; if(!(n&(n-1))) return 2; if(!(n&1)) return 0; uint64_t s, m; ex_gcd(n, &s, &m); for(i=0;i<7;i++){ uint64_t y = as64[i]%n; uint64_t x = RM(y, n); uint64_t in = MR((uint128_t)modpow64(x, n, m, n), m, n); uint64_t inmi = in >= y ? in - y : in + n - y; uint64_t p = gcd64(inmi, n); if(p == 1) return 0; if(is_prime64(p)){ return is_power64(n, p); } // n = p; } return 0; } int main(){ int i, q; scanf("%d", &q); for(i=0;i<q;i++){ uint64_t n; scanf("%ld", &n); if(n<=2) puts("No"); else if((n&1)==0) puts("Yes"); else { uint64_t j; for(j=2; j<n; j<<=1){ if(is_primepower64(n-j)){ break; } } if(j>=n) puts("No"); else puts("Yes"); } } return 0; }