Ultra-large number modulus of UVA 10692 Huge Mods

Source: Internet
Author: User
Tags modulus mul

Vjudge on the topic Link: Huge Mods

Attached:

Test instructions is not difficult to understand, because the index range is too large, so I think it is necessary to use power Dafa: ab% C = ab% phi (c) + phi (c)% c (B > Phi (c)) ? Later found that really need to use, and because it has a lot of heavy index, so need DFS, deep search to the last layer before returning, each up a layer to return with a power formula to deal with the index, and then this layer with the same principle to deal with the current layer of the value of the modulus, and back up a layer. Euler function preprocessing can, the end of the problem is also a bit of people, I am using input hanging to deal with.

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 Const intM =10006;6 7 intPhi[m] = {0,1,};8InlinevoidInitintn = M-3) {9      for(inti =2; I <= N; ++i)Ten         if(!Phi[i]) One          for(intj = i; J <= N; J + =i) { A             if(!phi[j]) phi[j] =J; -PHI[J] = phi[j]/I * (i-1); -         } the } -  -#include <cctype> -InlineintReadint&x) { +x =0; -     CharCH =GetChar (); +      while(!isdigit (CH) && ch! ='#') ch =GetChar (); A     if(ch = ='#')return 0; at      while(IsDigit (ch)) { -x = x *Ten+ (CH-'0'); -CH =GetChar (); -     } -     return 1; - } in  - intQuick_mod (intAintBintm) { to     intres =1; +      while(b) { -         if(B &1) Res = res * A%m; theA = a * a%m; *b >>=1; $     }Panax Notoginseng     returnRes; - } the  + intm,n,a[ -]; A  the //The ID is the array subscript for the current layer, and the mod is the modulus for the current layer, + //It is known from the Power formula that the mod is the Euler function of the MoD that passes the current layer, that is, the value of Phi[mod] each time the next level of the argument is passed. - //DFS up one layer returns the exponent that is processed by the exponentiation formula, and more see the code. $ intDfsintIdintMoD) { $     if(id = =N) { -         if(A[id] > MoD)returnA[id]% mod +MoD; -         Else    returnA[id]; the     } -     intPOW = DFS (id +1, Phi[mod]);Wuyi  the     intMul =1, C = a[id], num =POW; -  Wu     //because ignoring the C <= MoD led to an intermediate data overflow that caused me to TLE several times, and T had to know the truth, -     //still thinking is not the complexity of the wrong, so I step by step to the painful to debug t.t About      while(Num && mul <= mod && c <=MoD) { $         if(Num &1) Mul *=C; -C *=C; -Num >>=1; -     } A     if(Num && (mul > MoD | | c > MoD))returnQuick_mod (A[id], pow, MoD) +MoD; +     Else    returnMul; the } -  $ intMain () { the     intCase =0; the init (); the      while(Read (m)) { the read (n); -          for(inti =1; I <= N; ++i) in read (a[i]); theprintf"Case #%d:%d\n", ++case, DFS (1, m)%m); the     } About     return 0; the}
View Code

For a long time did not do number theory, it is really cool feeling! Although it is very difficult, although I have more than n XX theorem will not, but I will not give up this so attractive branch of mathematics, think of the original ACM has a lot of reason also because she ~

Ultra-large number modulus of UVA 10692 Huge Mods

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.