Fzu 1759-super a^b MoD C (Fast power + large integer modulo + Euler function)

Source: Internet
Author: User

Topic Link: Click to open the link

Test instructions: Calculates a^b%c but where B is large and may reach 10^1000000, so there is a power-down formula a^b%c= a^ (B%phi (c) +phi (c))%c (B>=phi (c))

#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < string> #include <cctype> #include <vector> #include <cstdio> #include <cmath> #include < queue> #include <stack> #include <map> #include <set> #define MAXN 10100#define _ll __int64#define ll Long long#define INF 0x3f3f3f3f#define Mod 1000000007#define pp pair<int,int> #define ull unsigned long longusing nam Espace std;int A,c;char B[1000010];ll Phi (ll N) {ll m= (ll) sqrt (n+0.5), ans=n;for (ll i=2;i<=m;i++) {if (n%i==0) {Ans=ans /i* (i-1); while (n%i==0) n/=i;}} if (n>1) ans=ans/n* (n-1); return ans;} ll Pow_mod (ll a,ll n,ll p) {if (n==0) return 1;ll ans=pow_mod (a,n/2,p); Ans=ans*ans%p;if (n&1) Ans=ans*a%p;return ans;} void Solve () {int Len=strlen (b); ll Tem=phi (c), Sb;if (len<=10) {sscanf (b, "%i64d", &AMP;SB); if (Sb>=tem) printf ("%I 64d\n ", Pow_mod (A,sb%tem+tem,c)); elseprintf ("%i64d\n ", Pow_mod (A,sb,c)); return;} ll ans=0;for (int i=0;i<len;i++) ans= (ans*10+ (b[i]-' 0 '))%tem;printf ("%i64d\n", Pow_mod (A,ans+tem,c));}    int main () {while (~scanf ("%i64d%s%i64d", &a,b,&c)) solve (); return 0;}


Fzu 1759-super a^b MoD C (Fast power + large integer modulo + Euler function)

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.