1268. Little Chu

Time limit: 0.25 second
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
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)的质因数个数。
 /**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define For(i, s, t) for(int i = (s); i <= (t); i++)
#define Ford(i, s, t) for(int i = (s); i >= (t); i--)
#define Rep(i, t) for(int i = (0); i < (t); i++)
#define Repn(i, t) for(int i = ((t)-1); i >= (0); i--)
#define rep(i, x, t) for(int i = (x); i < (t); i++)
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair
inline void SetIO(string Name)
{
string Input = Name+".in",
Output = Name+".out";
freopen(Input.c_str(), "r", stdin),
freopen(Output.c_str(), "w", stdout);
} inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = ;
bool Visit[N];
int Prime[N], Tot;
int n; inline void GetPrime()
{
For(i, , N - )
{
if(!Visit[i]) Prime[++Tot] = i;
For(j, , Tot - )
{
if(i * Prime[j] > N - ) break;
Visit[i * Prime[j]] = ;
if(!(i % Prime[j])) break;
}
}
} inline void Solve(); inline void Input()
{
GetPrime();
int TestNumber;
scanf("%d", &TestNumber);
while(TestNumber--)
{
scanf("%d", &n);
Solve();
}
} inline int Power(int y, int Times)
{
LL Ret = , x = 1LL * y;
while(Times)
{
if(Times & ) Ret = (Ret * x) % n;
x = (x * x) % n, Times >>= ;
}
return Ret;
} inline void Solve()
{
static int Arr[N], Len;
Len = ;
int Tmp = n - ;
For(i, , Tot)
{
if(Tmp < Prime[i]) break;
if(!(Tmp % Prime[i]))
{
Arr[++Len] = Prime[i];
while(!(Tmp % Prime[i]))
Tmp /= Prime[i];
}
}
if(Tmp > ) Arr[++Len] = Tmp; Ford(Ans, n - , )
{
bool Flag = ;
For(i, , Len)
if(Power(Ans, (n - ) / Arr[i]) == )
{
Flag = ;
break;
}
if(!Flag)
{
printf("%d\n", Ans);
break;
}
}
} int main()
{
#ifndef ONLINE_JUDGE
SetIO("D");
#endif
Input();
//Solve();
return ;
}

最新文章

  1. C# Stream
  2. Leetcode 254. Factor Combinations
  3. --hdu 1800 Flying to the Mars(贪心)
  4. Farseer.Net
  5. spring、springmvc、mybatis整合笔记
  6. JSONModel的基本使用
  7. 【C语言探索之旅】 第一部分第八课:第一个C语言小游戏
  8. JS模块化写法
  9. React Native 之 项目实战(一)
  10. IIS Service Unavailable HTTP Error 503. The service is unavailable.
  11. ppp 完全理解(二)【转】
  12. 效率生产力工具 —— idea 插件
  13. [Key] RegCure Pro
  14. VIN码识别/车架号OCR识别:快速占领汽车后市场数据入口
  15. VT-x VT-d 虚拟化在win10中的问题
  16. 20180824Noip模拟赛10分总结
  17. java 概括
  18. 分享到qq空间等代码
  19. poj 1795 DNA Laboratory
  20. String字符串去掉双引号

热门文章

  1. 淘宝(阿里百川)手机客户端开发日记第七篇 Service,Handler和Thread
  2. 获取IOS 设备基本信息
  3. 响应式Web设计(Responsive Web design)
  4. Redis使用介绍
  5. 多表利用DIH批量导入数据并建立索引注意事项
  6. C#模拟百度登录
  7. Java for LeetCode 199 Binary Tree Right Side View
  8. Java for LeetCode 076 Minimum Window Substring
  9. php的socket通信(一)
  10. static_cast, dynamic_cast, const_cast