Problem 1759 Super A^B mod C

Accept: 1368    Submit: 4639
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
 
 
题意:求(a^b)%c的值,由于是mod一个数,数据范围又非常大,我们非常容易想到循环节这个东西
那么我们就可以根据一个数学公式

求出c的欧拉函数,最后再求快速幂,就可以得到long long范围内的结果了

tips:FOJ不支持万能头文件和%lld

http://acm.fzu.edu.cn/problem.php?pid=1759

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e6+;
typedef long long ll;
char str[maxn];
int phi(int n){
int rea=n;
for(int i=;i*i<=n;i++){
if(n%i==){
rea=rea-rea/i;
while(n%i==) n/=i;
}
}
if(n>) rea=rea-rea/n;
return rea;
}
ll multi(ll a,ll b,ll m){
ll ans=;
a%=m;
while(b){
if(b&){
ans=(ans+a)%m;
b--;
}
b>>=;
a=(a+a)%m;
}
return ans;
} ll quick_mod(ll a,ll b,ll m){
ll ans=;
a%=m;
while(b){
if(b&){
ans=multi(ans,a,m);
b--;
}
b>>=;
a=multi(a,a,m);
}
return ans;
}
void solve(ll a,char str[],ll c){
ll len=strlen(str);
ll ans=;
ll p=phi(c);
if(len<=){
for(int i=;i<len;i++){
ans=ans*+str[i]-'';
}
}else{
for(int i=;i<len;i++){
ans=ans*+str[i]-'';
ans%=p;
}
ans+=p;
}
printf("%I64d\n",quick_mod(a,ans,c));
}
int main(){
ll a,c;
while(~scanf("%I64d%s%I64d",&a,str,&c)){
solve(a,str,c);
}
return ;
}

FOJ ——Problem 1759 Super A^B mod C的更多相关文章

  1. fzou 1759 Super A^B mod C

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

  2. FZU 1759 Super A^B mod C 指数循环节

    Problem 1759 Super A^B mod C Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description G ...

  3. FZU:1759-Problem 1759 Super A^B mod C (欧拉降幂)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1759 欧拉降幂是用来干啥的?例如一个问题AB mod c,当B特别大的时候int或者longlong装不下的时 ...

  4. FZU Super A^B mod C(欧拉函数降幂)

    Problem 1759 Super A^B mod C Accept: 878    Submit: 2870 Time Limit: 1000 mSec    Memory Limit : 327 ...

  5. Day7 - B - Super A^B mod C FZU - 1759

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

  6. Super A^B mod C (快速幂+欧拉函数+欧拉定理)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1759 题目:Problem Description Given A,B,C, You should quick ...

  7. foj Problem 2107 Hua Rong Dao

    Problem 2107 Hua Rong Dao Accept: 503    Submit: 1054Time Limit: 1000 mSec    Memory Limit : 32768 K ...

  8. foj Problem 2282 Wand

     Problem 2282 Wand Accept: 432    Submit: 1537Time Limit: 1000 mSec    Memory Limit : 262144 KB Prob ...

  9. FOJ Problem 2273 Triangles

    Problem 2273 Triangles Accept: 201    Submit: 661Time Limit: 1000 mSec    Memory Limit : 262144 KB P ...

随机推荐

  1. nginx配置安装

    先安装pcrepcre作用是让Nginx支持Rewrite功能下载地址:https://sourceforge.net/projects/pcre/files/pcre/,选择最新版本进行下载下载之后 ...

  2. Windows Server 2008 IIS 并发请求设置

    更新服务器的时候,突然发现部分机器出现了错误,大致描述如下 HTTP Error 503.2 - Service Unavailable 正在超过 serverRuntime@appConcurren ...

  3. ethereum(以太坊)(二)--合约中属性和行为的访问权限

    pragma solidity ^0.4.0; contract Test{ /* 属性的访问权限 priveta public internal defualt internal interlnal ...

  4. Oracle创建表空间,添加用户及授权

    工具/原料 PLSQL 方法/步骤 1.在PLSQL里,用system/css(orcl系统用户)登陆,登陆的时候普通用户登陆选择normal就可以了   2.Oracle用户新增 执行语句 crea ...

  5. 779. K-th Symbol in Grammar

    class Solution { public: int kthGrammar(int N, int K) { return helper(N, K, false); } int helper(int ...

  6. PAT (Basic Level) Practice 1006 换个格式输出整数

    个人练习 让我们用字母B来表示“百”.字母S表示“十”,用“12...n”来表示个位数字n(&lt10),换个格式来输出任一个不超过3位的正整数.例如234应该被输出为BBSSS1234,因为 ...

  7. POJ 3581 三段字符串(后缀数组)

    Sequence Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7923   Accepted: 1801 Case Tim ...

  8. 在intellij idea 里来回跳转查询方法

    在intellij idea 里来回跳转查询方法,在不知道快捷键的时候真是抓狂. 看到key map中的快捷键 后退back  ctrl +alt + ← 或者button4 click 前进forw ...

  9. 第一章:Hello, World!

    感谢作者 –> 原文链接 本文翻译自The Flask Mega-Tutorial Part I: Hello, World! 一趟愉快的学习之旅即将开始,跟随它你将学会用Python和Flas ...

  10. 笔记-python-元类metaclass

    笔记-python-元类metaclass 1.      元类 1.1.    类也是对象 class Person(object): pass 上面的代码会在内存中创建一个类,它也是对象, 可以将 ...