High precision, big integer power modulo

Source: Internet
Author: User

Format:

Cha ^ CHB % C

Both Cha and CHB are strings.

/** Fzu1759.cpp ** created on: 2011-10-11 * Author: bjfuwangzhu */# include <stdio. h> # include <string. h> # include <math. h> # include <stdlib. h> # define ll long # define nnum 1000005 # define Nmax limit 25int flag [Nmax], prime [Nmax]; int Plen; void mkprime () {int I, J; memset (flag,-1, sizeof (FLAG); for (I = 2, Plen = 0; I <Nmax; I ++) {If (flag [I]) {Prime [Plen ++] = I ;}for (j = 0; (j <Plen) & (I * prim E [J] <Nmax); j ++) {flag [I * prime [J] = 0; if (I % prime [J] = 0) {break ;}}} int getphi (int n) {int I, Te, Phi; Te = (INT) SQRT (N * 1.0); for (I = 0, phi = N; (I <Plen) & (prime [I] <= tE); I ++) {If (N % prime [I] = 0) {Phi = PHI/prime [I] * (Prime [I]-1); While (N % prime [I] = 0) {n/= prime [I] ;}}if (n> 1) {Phi = PHI/N * (N-1);} return Phi ;} int cmpbignum (int p, char * Ch) {Int I, Len; ll res; Len = strlen (CH); for (I = 0, Res = 0; I <Len; I ++) {res = (RES * 10 + (CH [I]-'0'); If (RES> P) {return 1 ;}} return 0 ;} int getmodbignum (int p, char * Ch) {int I, Len; ll res; Len = strlen (CH); for (I = 0, Res = 0; I <Len; I ++) {res = (RES * 10 + (CH [I]-'0') % P;} return (INT) RES;} int modular_exp (int, int B, int c) {ll res, temp; Res = 1% C, temp = A % C; While (B) {If (B & 1) {res = res * temp % C;} temp = temp * temp % C; B >>= 1 ;} return (INT) res;} void solve (int A, int C, char * Ch) {int Phi, res, B; Phi = getphi (c); If (cmpbignum (PHI, CH )) {B = getmodbignum (PHI, CH) + Phi;} else {B = atoi (CH);} res = modular_exp (A, B, C ); printf ("% d \ n", Res);} int main () {# ifndef online_judge freopen ("data. in "," r ", stdin); # endif int A, C; char Cha [nnum ], ChB [nnum]; mkprime (); While (~ Scanf ("% S % d", cha, CHB, & C) {A = getmodbignum (C, CHA); solve (A, C, CHB );} return 0 ;}

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.