God and the correct use of the set (bzoj3884)

Title Description

According to some records on the book, God's creation a failed experience is this:
The first day, God created a world of basic elements, called "dollar."
The next day, God created a new element called "α". "[Alpha]" is defined as a "meta" configuration set. Easy to find, a total of two different "α".
On the third day, God also created a new element, called "β". "Β" is defined as "α" set constituted. Easy to find, a total of four different "β".
The fourth day, God created a new element "γ", "γ" is defined as a collection of "β" of. Obviously, there will be a total of 16 different "γ".
If you follow this continues, God created the fourth element will be 65,536, the fifth element will have 2 ^ 65,536 kinds. This will be an astronomical figure.
However, God did not expect to increase the number of element types is so fast. He wanted to let the elements of the world's rich, therefore, day after day, year after year, he repeatedly creating new elements ......
Soon, however, when God created the final element "θ", he found that the elements of this world is too much, so that the lack of capacity in the world, can not afford. So on this day, God destroyed the world.
So far, God's creation still remember the experience of failure, and now he would like to ask you, the last time he created element "θ" a total number of species?
God that this figure may be too large and can not be represented, so you only need to answer a number of values ​​of p modulo can be.
You can think of God from "α" to "θ" created a total of 10 ^ 9 elements, or 10 ^ 18, or simply ∞ times.
One topic is intended to:

 

Input

Next T lines each a positive integer p, the representative value you need modulo

Output

T lines, each a positive integer, the value of the answer to the modulus p

Sample Input

3
2
3
6

Sample Output

0
1
4

HINT

对于100%的数据,T<=1000,p<=10^7

输入

输出

提示

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 10001000
using namespace std;
int Phi(int x)
{
    int i,re=x;
    for(i=2;i*i<=x;i++)
        if(x%i==0)
        {
            re/=i;re*=i-1;
            while(x%i==0)
                x/=i;
        }
    if(x^1) re/=x,re*=x-1;
    return re;
}
int Quick_Power(long long x,int y,int p)
{
    long long re=1;
    while(y)
    {
        if(y&1) (re*=x)%=p;
        (x*=x)%=p; y>>=1;
    }
    return re;
}
int Solve(int p)
{
    if(p==1) return 0;
    int temp=0;
    while(~p&1) p>>=1,++temp;
    int phi_p=Phi(p);
    int re=Solve(phi_p);
    (re+=phi_p-temp%phi_p)%=phi_p;
    re=Quick_Power(2,re,p)%p;
    return re<<temp;
}
int main()
{
    int T,p;
    for(cin>>T;T;T--)
    {
        scanf("%d",&p);
        printf("%d\n",Solve(p));
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/hfang/p/11256764.html