ural 1268. Little Chu
1268. Little Chu
Time limit: 0.25 second
Memory limit: 64 MB
Memory limit: 64 MB
The favorite occupation of Little Chu is to sleep. Strictly speaking, he is busy with nothing but sleeping. Sometimes he wakes up and than the mankind makes some Great Discovery. For the first time Little Chu woke up K days after his birth. For the second time he woke up K2 after his birth. For the third time — K3 days after his birth. This rule still holds true.
Each time whem Little Chu wakes up he looks at the calendar and remembers what day of week is today. They say that if the day of week will be repeated, than Litle Chu will start crying and his tears will flood the world.
Your task is to make the largest number of the Great Discoveries and maximally to delay the doomsday. Determine when should Little Chu be awaken for the first time if it is known that he can’t sleep more than one week after his birth.
Input
The first line contains integer T (1 ≤ T ≤ 6553) — the number of tests. Each of the next T lines contains integer N (2 < N < 65536) — the number of days in the week. N is prime.
Output
K for each input test.
Sample
input | output |
---|---|
4 3 5 7 11 |
2 3 5 8 |
Problem Author: Pavel Atnashev
Problem Source: Ural State University championship, October 25, 2003
Problem Source: Ural State University championship, October 25, 2003
Tags: number theory
Difficulty: 805
题意:给出m,找出一个k是的k^1 k^2 k^3...k^x mod m 后各不相同
分析:
如果发现有
k^t = k (mod m)
k^(t-1) = 1(mod m)
换个形式
q^t=1(mod m)
因为m是质数,根据xx定理,有 q^(m-1) = 1(mod m)
所以,t跟定有 t%(m-1) == 0
因为t < m-1,且t%(m-1) == 0
那是不是我们只用枚举m-1的因数?
太多了。
发现t至少整除(m-1)/pi中的一个。
q^t = 1(mod m)
q^(m-1) = 1(mod m)
显然q^((m-1)/pi) = 1(mod m)
所以只需检验是否存在一个pi使q^((m-1)/pi) = 1(mod m)
检验一个数的复杂度降至(m-1)的质因数个数。
1 /** 2 Create By yzx - stupidboy 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <cmath> 8 #include <deque> 9 #include <vector> 10 #include <queue> 11 #include <iostream> 12 #include <algorithm> 13 #include <map> 14 #include <set> 15 #include <ctime> 16 #include <iomanip> 17 using namespace std; 18 typedef long long LL; 19 typedef double DB; 20 #define For(i, s, t) for(int i = (s); i <= (t); i++) 21 #define Ford(i, s, t) for(int i = (s); i >= (t); i--) 22 #define Rep(i, t) for(int i = (0); i < (t); i++) 23 #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--) 24 #define rep(i, x, t) for(int i = (x); i < (t); i++) 25 #define MIT (2147483647) 26 #define INF (1000000001) 27 #define MLL (1000000000000000001LL) 28 #define sz(x) ((int) (x).size()) 29 #define clr(x, y) memset(x, y, sizeof(x)) 30 #define puf push_front 31 #define pub push_back 32 #define pof pop_front 33 #define pob pop_back 34 #define ft first 35 #define sd second 36 #define mk make_pair 37 inline void SetIO(string Name) 38 { 39 string Input = Name+".in", 40 Output = Name+".out"; 41 freopen(Input.c_str(), "r", stdin), 42 freopen(Output.c_str(), "w", stdout); 43 } 44 45 46 inline int Getint() 47 { 48 int Ret = 0; 49 char Ch = ' '; 50 bool Flag = 0; 51 while(!(Ch >= '0' && Ch <= '9')) 52 { 53 if(Ch == '-') Flag ^= 1; 54 Ch = getchar(); 55 } 56 while(Ch >= '0' && Ch <= '9') 57 { 58 Ret = Ret * 10 + Ch - '0'; 59 Ch = getchar(); 60 } 61 return Flag ? -Ret : Ret; 62 } 63 64 const int N = 70000; 65 bool Visit[N]; 66 int Prime[N], Tot; 67 int n; 68 69 inline void GetPrime() 70 { 71 For(i, 2, N - 1) 72 { 73 if(!Visit[i]) Prime[++Tot] = i; 74 For(j, 1, Tot - 1) 75 { 76 if(i * Prime[j] > N - 1) break; 77 Visit[i * Prime[j]] = 1; 78 if(!(i % Prime[j])) break; 79 } 80 } 81 } 82 83 inline void Solve(); 84 85 inline void Input() 86 { 87 GetPrime(); 88 int TestNumber; 89 scanf("%d", &TestNumber); 90 while(TestNumber--) 91 { 92 scanf("%d", &n); 93 Solve(); 94 } 95 } 96 97 inline int Power(int y, int Times) 98 { 99 LL Ret = 1, x = 1LL * y; 100 while(Times) 101 { 102 if(Times & 1) Ret = (Ret * x) % n; 103 x = (x * x) % n, Times >>= 1; 104 } 105 return Ret; 106 } 107 108 inline void Solve() 109 { 110 static int Arr[N], Len; 111 Len = 0; 112 int Tmp = n - 1; 113 For(i, 1, Tot) 114 { 115 if(Tmp < Prime[i]) break; 116 if(!(Tmp % Prime[i])) 117 { 118 Arr[++Len] = Prime[i]; 119 while(!(Tmp % Prime[i])) 120 Tmp /= Prime[i]; 121 } 122 } 123 if(Tmp > 1) Arr[++Len] = Tmp; 124 125 Ford(Ans, n - 1, 2) 126 { 127 bool Flag = 0; 128 For(i, 1, Len) 129 if(Power(Ans, (n - 1) / Arr[i]) == 1) 130 { 131 Flag = 1; 132 break; 133 } 134 if(!Flag) 135 { 136 printf("%d\n", Ans); 137 break; 138 } 139 } 140 } 141 142 int main() 143 { 144 #ifndef ONLINE_JUDGE 145 SetIO("D"); 146 #endif 147 Input(); 148 //Solve(); 149 return 0; 150 }