2015上海网络赛 HDU 5478 Can you find it 数学
题意略。
思路:先求出n = 1 时候满足条件的(a,b), 最多只有20W对,然后对每一对进行循环节判断即可
1 #include <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm> 5 #include <cmath> 6 #include <deque> 7 #include <vector> 8 #include <queue> 9 #include <string> 10 #include <cstring> 11 #include <map> 12 #include <stack> 13 #include <set> 14 #define LL long long 15 #define eps 1e-8 16 #define INF 0x3f3f3f3f 17 #define MAXN 200005 18 using namespace std; 19 LL C, k1, b1, k2; 20 LL quick_mod(LL x, LL y, LL mod){ 21 if (y == 0) return 1; 22 LL res = quick_mod(x, y >> 1, mod); 23 res = res * res % mod; 24 if (y & 1){ 25 res = res * x % mod; 26 } 27 return res; 28 } 29 LL a[MAXN][3]; 30 LL b[MAXN]; 31 int main() 32 { 33 #ifndef ONLINE_JUDGE 34 freopen("in.txt", "r", stdin); 35 //freopen("out.txt", "w", stdout); 36 #endif // OPEN_FILE 37 int cas = 1; 38 while (~scanf("%I64d%I64d%I64d%I64d", &C,&k1, &b1, &k2)){ 39 printf("Case #%d:\n", cas++); 40 int t = 0; 41 for (int i = 1; i < C; i++){ 42 LL m = quick_mod((LL)i, k1 + b1, C); 43 if (m == 0) continue; 44 a[++t][1] = i; 45 a[t][2] = C - m; 46 b[t] = m; 47 } 48 bool noans = true; 49 for (LL i = 1; i <= t; i++){ 50 LL pa = b[i]; 51 LL pa_k1 = quick_mod(a[i][1], k1, C); 52 LL pb_k2 = quick_mod(a[i][2], k2, C); 53 LL pb = a[i][2]; 54 LL xx = pa; 55 LL yy = pb; 56 // pb_k2 = quick_mod(pb_k2, 10000, C); 57 LL x = pa_k1, y = pb_k2; 58 bool flag = false; 59 for (int j = 2; j <= 1500; j++){ 60 pa = pa * x % C; 61 pb = pb * y % C; 62 if (pa == xx && pb == yy){ 63 break; 64 } 65 if ((pa + pb) % C != 0){ 66 flag = true; 67 break; 68 } 69 } 70 if (flag) continue; 71 printf("%I64d %I64d\n", a[i][1], a[i][2]); 72 noans = false; 73 } 74 if (noans){ 75 printf("-1\n"); 76 } 77 } 78 }