The theory of first-involved

Source: Internet
Author: User

Title Description

For a given x, find the smallest positive integer y that makes x^y%mod = 1 , where mod = 790660457.

Input

Multiple input groups (up to 50000 groups)

One x (0 < x < MoD) per group

Output

First test the number of groups, and then output the corresponding minimum positive integer y, see example

Sample input
13
Sample output
Case #1:1Case #2:790660456
Tips

Fast power of Fermat theorem

A conclusion from the Fermat theorem that this y is bound to be a mod-1 factor.

//asimple #include <bits/stdc++.h>#defineINF 0XFFFFFFF#defineMoD 790660457#defineSwap (a,b,t) T = A, a = B, b = t#defineCLS (A, V) memset (A, V, sizeof (a))#defineDebug (a) cout << #a << "=" << a <<endl#defineABS (x) x<0?-x:x#defineSRD (a) scanf ("%d", &a)#defineSRC (a) scanf ("%c", &a)#defineSRS (a) scanf ("%s", a)#defineSrdd (A, b) scanf ("%d%d", &a, &b)#defineSRDDD (a,b,c) scanf ("%d%d%d", &a, &b, &c)#definePRD (a) printf ("%d\n", a)#definePrdd (b) printf ("%d%d\n", A, B)#definePRS (a) printf ("%s\n", a)#definePRC (a) printf ("%c", a)using namespaceStd;typedefLong Longll;Const intMAXN =1001;intN, M, num, T, K, Len, ans, sum;intDP[MAXN];
Find a factor
voidsolve () {Len=0; ll Num= mod-1; for(intI=1; i*i<=num; i++) { if(num% i = =0) {Dp[len++] =i; if(num/i!=i) {Dp[len+ +] = num/i; }}} sort (DP, DP+len);} //Fast power ll Qpow (ll A, LL B, ll MD) {ll ans=1; while(b) {if(B &1) ans = ans * a%MD; A= A * A%MD; b= b >>1; } returnans;} voidinput () {intCI =0; Solve (); while( ~SRD (n)) {printf ("Case #%d:", ++ci); for(intI=0; i<len; i++) { if(Qpow (n, dp[i], mod) = =1) {PRD (dp[i]); Break; } } }} intMain () {input (); return 0;}

The theory of first-involved

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.