Description According to some books, God's one failure of the creation experience is this: the first day,God created the basic elements of a world, called "Yuan". The next day,God created a new element, called "alpha". "Alpha" is defined as a set of "meta" components. It is easy to see that there are two different "alpha" types. On the third day,God has created a new element called "beta". "Beta" is defined as a set of "alpha" components. It is easy to find a total of four different "beta". Day Fourth,God created the new element "gamma", "gamma" is defined as the set of "beta". Obviously, there will be 16 different "gamma" altogether. If this goes on, the fourth element God created will be 65536, and the fifth element will have 2^65536. This is going to be an astronomical one. However, God did not expect the growth of the number of elements to be so rapid. He wants to enrich the elements of the world, so, day after day, year after year, he repeats the creation of new elements ... But soon, when God created the last element, "Theta," he found that there were so many elements in the world that the world's capacity was inadequate and unbearable. So on this day, God destroys the world. So far, God remembers the failure of the creation experience, and now he wants to ask you how many of the elements he created last time, "Theta"? God thinks the number may be too large to be expressed, so you just need to answer the value of this number to P modulo. You can think of God from "alpha" to "theta" altogether creating 10^9 elements, or 10^18 times, or simply ∞ times. Word test instructions: Input next T line, a positive integer p for each line, represents the value you need to modulo outputt line, a positive integer per line, the value after the answer p modulo First feeling: Not to do--. This question is very interesting, please think carefully before you look at the problem--. There are two approaches to this problem. 1. Practice of the Author: http://blog.csdn.net/popoqqq/article/details/43951401 2. Another witty God-ox practice: we know that
But P and 2 are not necessarily coprime, so we can make the following form.
Set F (P) =.
Then we can get a constant recursive formula F (P) =2^ (F (Phi(p)) +phi (p))%p. Boundary: P=1,f (P) =0.
Proving the complexity of the algorithm is as follows:
IfPis an even number, the? (p)≤P2 ;
IfPis odd, thePThere is an odd factorQMakes? (P) There is an even factor(q−1) , converted to an even number of cases.
Can be seen? (? (. . . ? (P))) The calculation passeso(logp) The iteration of the time is over.1Sof(P) The calculation iso ( p√ Log p) . (Some think (dai) Want (MA) selected from http://blog.csdn.net/skywalkert/article/details/43955611)
The code for the second method:
1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 6 #definell Long Long7 using namespacestd;8 9 intmod,num[10000001];Ten One intQvod (intKintmo) A { -ll ans=1, x=2; - while(k!=0) the { - if(k&1) ans=ans*x%mo; -x=x*x%mo; -k>>=1; + } - returnans; + } A at intPhiintx) - { - intAns=x,aa=x; - for(intI=2; i<=sqrt (ans); i++)if(x%i==0){ - while(x%i==0) x/=i; -Aa= (LL) aa* (i-1)/i; in } - if(x!=1) aa= (LL) aa* (x1)/x; to returnAA; + } - the intFintx) * { $ if(x==1)return 0;Panax Notoginseng if(Num[x]) - returnNum[x]; the intsb=Phi (x); +Num[x]=qvod (f (SB) +sb,x); A returnNum[x]; the } + - intMain () $ { $ intT; -scanf"%d",&T); - while(t--) the { -scanf"%d",&MoD);Wuyiprintf"%d\n", F (mod)); the } - return 0; Wu}
View Code
"Mathematics" [Bzoj 3884] God with the correct usage of the set