The correct usage of God and set

Source: Internet
Author: User
Tags modulus

Transmission Door

The problem is really not good.

A word describing test instructions is asking for unlimited 2 bases on your index ... The value of mod p, where p does not exceed 107

This thing is actually a recursive function (of course we are recursive solution), I do not know how to take the language description 233.

We're going to use Euler's power-down formula for this problem!

First, what is Euler's power-down formula?

Its basic form is as long as this:

Ak≡ak%φ (M) +φ (m) (mod m) (K >φ (M))

Ak≡ak (mod m) (K <=φ (M))

How to prove it ...? It proved that the process was too long and I only copied it. we can take a look at this big guy's proof! Transmission Door

Well, then we'll use it to solve the problem, the problem is that the base is always 2, then the index ... Because it's infinite. 2 Let's just give him the same! In this case, for each k modφ (m), this number can be calculated recursively, the formula is 2k form, but because of the infinite 2, so in fact, K can also be expressed as 2k form ... Only the modulus will change each time the downward recursion is passed.

We may have doubts, but the index is infinite! When are we going to finish it?

But do not forget that each time we recursive, the number used to take the modulo will change from M to φ (m), so we do not need recursion many times we will calculate the modulus of 1, this time directly return 0, and then recursion back to have the solution! So we just have to deal with the Euler function.

By the way also learned a quick multiply 2333, and the fast power is like, is to multiply into addition.

Take a look at the code.

#include <iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<queue>#include<cstring>#defineRep (i,a,n) for (int i = a;i <= n;i++)#definePer (i,n,a) for (int i = n;i >= a;i--)#defineEnter Putchar (' \ n ')#definePR pair<int,int>#defineMP Make_pair#defineFi first#defineSC Secondusing namespaceStd;typedefLong Longll;Const intM =100005;Const intN =10000005; ll read () {ll ans=0, op =1; CharCH =GetChar ();  while(Ch <'0'|| CH >'9')    {    if(ch = ='-') op =-1; CH=GetChar (); }     while(Ch >='0'&& CH <='9') {ans*=Ten; Ans+ = CH-'0'; CH=GetChar (); }    returnAns *op;} ll T,n,pri[n],phi[n],tot;BOOLNp[n];ll Qmul (ll a,ll b,ll MoD) {ll P=0;  while(b) {if(b&1) p = (p + a)%MoD; A= (A + a)%MoD; b>>=1; }    returnp;} ll Qpow (ll a,ll b,ll MoD) {ll P=1;  while(b) {if(b&1) P *= a,p%=MoD; A*= A,a%=MoD; b>>=1; }    returnp;}voidEuler () {np[1] =1; Rep (I,2, N-5)    {    if(!np[i]) Pri[++tot] = i,phi[i] = i-1;  for(intj =1; I * Pri[j] <= N-5; j + +) {Np[i* Pri[j]] =1; if(! IPri[j])) {Phi[i* Pri[j]] = phi[i] *Pri[j];  Break; }        ElsePhi[i * Pri[j]] = phi[i] * (PRI[J)-1); }}}ll solve (ll MoD) {if(MoD = =1)return 0; Else returnQpow (2, Solve (Phi[mod]) +phi[mod],mod);}intMain () {Euler (); T=read ();  while(t--) n = read (), printf ("%lld\n", Solve (n)); return 0;}

The correct usage of God and set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.