ypizarroza's blog

By ypizarroza, 10 years ago, In English

please someone that help me whith this problem http://www.spoj.com/problems/POWERUP/ i'm using the fermat little theorem a^p-1=a (mod p). Grace in advance

  • Vote: I like it
  • +2
  • Vote: I do not like it

»
10 years ago, # |
  Vote: I like it -24 Vote: I do not like it

By Fermat theorem a^phi(m) == a (mod m) => a^(phi(m) — 1) == 1 (mod m). In this problem m == 10^9 + 7 is prime, so phi(m) == m — 1.

So A^(B^C) (mod m) == A^(B^C mod (phi(m) — 1)) (mod m) == A^(B^C mod (m — 2)) (mod m).

  • »
    »
    10 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Yes, I'm applying that theory,but the judge sentence is wrong answer. I would appreciate if you provide me extreme cases in which my code it can fail

  • »
    »
    10 years ago, # ^ |
      Vote: I like it +16 Vote: I do not like it

    You are wrong, aφ(m) = 1.

  • »
    »
    10 years ago, # ^ |
      Vote: I like it +6 Vote: I do not like it

    It's Euler theorem, not Fermat's theorem...

»
10 years ago, # |
  Vote: I like it 0 Vote: I do not like it

There are some tricky cases when a mod p == 0

  • »
    »
    10 years ago, # ^ |
    Rev. 2   Vote: I like it -8 Vote: I do not like it
    I have proven to these cases. Please one user that has AC this problem. please  look my code:
    #include <bits/stdc++.h>
    #define ifc(x)(flag[x>>6]&(1<<((x>>1)&31)))
    #define isc(x)(flag[x>>6]|=(1<<((x>>1)&31)))
    using namespace std;
    typedef long long i64;
    #define MOD 1000000007
    typedef long long i64;
    i64 multiply(i64 a,i64 b, i64 mod){
     i64 rx=0,sx=0;
      for (int bx=0; b>>bx>0; ++bx){
        sx+=(bx)?sx:a;
        if(sx>=mod) sx-=mod;
        rx+=((b>>bx)&1)?sx:0;
        while(rx>=mod) rx-=mod;
      }
      return rx;
    }
    i64 modpow(i64 a,i64 b, i64 mod){
      i64 rx=1,sx=0;
      for(int bx=0; b>>bx>0; ++bx){
        sx=(bx)?multiply(sx,sx,mod):a;
        rx=((b>>bx)&1)?multiply(rx,sx,mod):rx;
      }
      return rx;
    }
    i64 sol;
    int cas,a,b,c;
    int main(){
    
    	      while(1){
    	      scanf("%d %d %d",&a,&b,&c);
    	      if(a+b+c==-3)break;
    		  i64 x=modpow(b,c,MOD-1);
    		  printf("%lld\n",modpow(a,x,MOD));
    	      }
    }
    
    
    

    ``