zoukankan      html  css  js  c++  java
  • fzou 1759 Super A^B mod C

    Problem 1759 Super A^B mod CAccept: 456    Submit: 1488
    Time Limit: 1000 mSec    Memory Limit : 32768 KB

    Problem Description

    Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B<=10^1000000).

    Input

    There are multiply testcases. Each testcase, there is one line contains three integers A, B and C, separated by a single space.

    Output

    For each testcase, output an integer, denotes the result of A^B mod C.

    Sample Input

    3 2 4
    2 10 1000

    Sample Output

    1
    24
     
     1 /*
     2 高次同余第一题:
     3 
     4 大牛提起过这道题,当时觉得b太大了10^1000000,无从下手。
     5 a^b%c=a^(b%phi(c))%c ,注意a==c的情况
     6 这句话,想起了  费马小定理 a^b%c=a^(b%(c-1))%c;  c是互数,a,c互质的时候。
     7 费马小定理是一个特殊的总结吧。这个是通式。
     8 */
     9 
    10 #include<iostream>
    11 #include<cstdio>
    12 #include<cstdlib>
    13 #include<cstring>
    14 using namespace std;
    15 
    16 char b[1000005];
    17 
    18 __int64 Euler(__int64 n)
    19 {
    20     __int64 i,temp=n;
    21     for(i=2;i*i<=n;i++)
    22     {
    23         if(n%i==0)
    24         {
    25             while(n%i==0)
    26             n=n/i;
    27             temp=temp/i*(i-1);
    28         }
    29     }
    30     if(n!=1) temp=temp/n*(n-1);
    31     return temp;
    32 }
    33 
    34 __int64 power_sum2(__int64 a,__int64 n,__int64 mod)
    35 {
    36     __int64 ans=1;
    37     while(n)
    38     {
    39         if(n&1)
    40         {
    41             ans=(ans*a)%mod;
    42         }
    43         n=n>>1;
    44         a=(a*a)%mod;
    45     }
    46     return ans;
    47 }
    48 
    49 int main()
    50 {
    51     __int64 a,c,len,k,cur,i;
    52     while(scanf("%I64d",&a)>0)
    53     {
    54         scanf("%s",b+1);
    55         scanf("%I64d",&c);
    56         if(a==c)
    57         {
    58             printf("0
    ");
    59             continue;
    60         }
    61         len=strlen(b+1);
    62         k=Euler(c);
    63         for(i=1,cur=0;i<=len;i++)
    64         {
    65             cur=cur*10+b[i]-'0';
    66             cur=cur%k;
    67         }
    68         printf("%I64d
    ",power_sum2(a%c,cur,c));
    69     }
    70     return 0;
    71 }
  • 相关阅读:
    强制数据类型转换之Number类型
    强制类型转换之String类型
    数据类型之字符串类型与Number类型
    favicon.ico是什么?
    什么是自幂数?
    JavaScript基础之变量的自增与自减
    CMD 命令
    那些看完很有同感的语句
    HTML5 canvas 元素
    HTML的多媒体属性
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3284120.html
Copyright © 2011-2022 走看看