Ural 1268. Little Chu 求原根

来源:互联网 发布:什么是淘宝鹊桥活动 编辑:程序博客网 时间:2024/06/05 03:51

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

inputoutput
435711
2358
Problem Author: Pavel Atnashev
Problem Source: Ural State University championship, October 25, 2003
求K个素数pi对应的ni。ni满足:ni,ni^2,ni^3,...,ni^m对pi取模各不相同(i=1,2,3,...),且m最大,ni最大。求一个数,在满足是p的原根的情况下,n^(p-1)的值最大。就是小于p的最大原根。

原根设m是正整数,a是整数,若a模m的阶等于φ(m),则称a为模m的一个原根。(其中φ(m)表示m的欧拉函数)
假设一个数g对于P来说是原根,那么g^i mod P的结果两两不同,且有 1<g<P, 0<i<P,那么g可以称为是P的一个原根,归根到底就是g^(P-1) = 1 (mod P)当且仅当指数为P-1的时候成立.(这里P是素数).
详细请见:http://blog.csdn.net/jsun_moon/article/details/9878909
#include <cstdio>#include <cstdlib>#include <cmath>#include <iostream>using namespace std;long long t,n,m,tot,upper,ans,f[110];int int_pow(long long x,int y){    long long t=1;    while(y>0)    {        if(y&1)t=(t*x)%n;        x=(x*x)%n;        y>>=1;    }    return (int)t;}bool check(int x){    for (int i=1; i<=tot; ++i)        if (int_pow((long long)x,(n-1)/f[i]) == 1)            return false;    return true;}long long solve(){    if (n <= 3)  return n-1;    tot=0,m=ans=n-1;    upper=(int)sqrt((double)m);    for (int i=2; (m!=1)&&(i<=upper); ++i)        if (!(m%i))        {            f[++tot]=i;            while (!(m%i))  m/=i;        }    if (m != 1)  f[++tot]=m;    while (!check(ans))  --ans;    return ans;}int main(){    cin>>t;    while (t--)    {        cin>>n;        cout<<solve()<<endl;    }    return 0;}


原创粉丝点击