• fzu 1759Super A^B mod C 指数循环节


    Problem 1759 Super A^B mod C

    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

    Source

    FZU 2009 Summer Training IV--Number Theory

    欧拉定理的推广——有关的高次幂取模指数循环节

    公式: ax mod(c)=a(x mod phi(c) +phi(c)) mod(c), (x>=phi(c))

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4 using namespace std;
     5 typedef long long int ll;
     6 
     7 const int MAXN = 1e6;
     8 int prime[MAXN+10], cnt = 0;
     9 int a[MAXN+10];
    10 
    11 ll pow_mod(ll a, ll n, ll mod){
    12     ll ans = 1;
    13     while(n > 0){
    14         if(n&1) {
    15             n--;
    16             ans = ans*a%mod;
    17         }
    18         n >>= 1;
    19         a = a*a %mod;
    20     }
    21     return ans;
    22 }
    23 
    24 void init(){
    25     for(int i = 2; i <= MAXN; i++) a[i] = true;
    26     for(int i = 2; i <= MAXN; i++){
    27         if(a[i]){
    28             prime[++cnt] = i;
    29         }
    30         for(int j = 1; j <= cnt; j++){
    31             if(prime[j]*i > MAXN) break;
    32             a[prime[j]*i] = false;
    33             if(i%prime[j] == 0) break;
    34         }
    35     }
    36 }
    37 ll Euler(ll n){
    38     ll ans = n;
    39     for(int i = 1; i <= cnt && prime[i] <= n; i++){
    40         if(n%prime[i] == 0){
    41             while(n%prime[i] == 0){
    42                 n /= prime[i];
    43             }
    44             ans = ans/prime[i]*(prime[i]-1);
    45         }
    46     }
    47     return ans;
    48 }
    49 ll slove(ll a, char s[], ll c, ll mod){
    50     ll sum = 0;
    51     int len = strlen(s);
    52     sum = (s[0] - '0') %mod;
    53     for(int i = 1; i < len; i++){
    54         sum *= 10;
    55         sum = (sum + s[i] - '0')%mod;
    56     }
    57    // printf("sum = %I64d
    ", sum);
    58     return pow_mod(a, mod+sum, c);
    59 }
    60 int main(){
    61     ll a, c;
    62     char b[MAXN+10];
    63     init();
    64     while(scanf("%I64d%s%I64d", &a, b, &c) != EOF){
    65         ll p = Euler(c);
    66        // printf("p = %I64d
    ", p);
    67         printf("%I64d
    ", slove(a, b, c, p));
    68     }
    69 
    70 }
  • 相关阅读:
    常见数据结构考题
    [转]Win7 系统安装VS2008没反应 点击安装一闪就没有反应 .
    [转]40个实习生最基本的规矩
    [转]C++中重载(overload)、覆盖(override)、隐藏(hide)的区别
    iPhone开源项目
    eclipse快捷键
    [转]cocos2d游戏开发,常用工具集合
    discuz中常用的一些东西
    抽象类与接口的区别
    Head.First设计模式学习笔记
  • 原文地址:https://www.cnblogs.com/cshg/p/5896445.html
Copyright © 2020-2023  润新知