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. tomcat7下载地址

    tomcat7下载地址:https://tomcat.apache.org/download-70.cgi

  2. Windows Subsystem for Linux(WSL)安装记录

    什么是WSL Windows Subsystem for Linux(简称WSL)是一个为在Windows 10上能够原生运行Linux二进制可执行文件(ELF格式)的兼容层.它是由微软与Canoni ...

  3. Linux crontab 实现秒级定时任务

    1   crontab 的延时: 原理:通过延时方法 sleep N  来实现每N秒执行. crontab -e 输入以下语句,然后 :wq 保存退出. * * * * * /usr/bin/curl ...

  4. ubuntu配置命令

    sudo apt-get update 更新源 sudo apt-get install package 安装包 sudo apt-get remove package 删除包 sudo apt-ca ...

  5. Alice’s Stamps HDU - 6249 (区间DP)

    点击传送 Alice’s Stamps Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  6. Codeforces Round #438 C - Qualification Rounds 思维

    C. Qualification Rounds time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  7. [Link-Cut-Tree][BZOJ2002]弹飞绵羊

    题面 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上\(n\)个 ...

  8. Android面试收集录16 Android动画总结

    一.Android 动画分类 总的来说,Android动画可以分为两类,最初的传统动画和Android3.0 之后出现的属性动画: 传统动画又包括 帧动画(Frame Animation)和补间动画( ...

  9. TouTiao开源项目 分析笔记19 问答内容

    1.真实页面预览 1.1.成果预览 首先是问答列表 然后每个item设置点击事件,进入问答内容列表 然后每一个问答内容也设置点击事件,进入问答详情 1.2.触发事件. 在WendaArticleOne ...

  10. [bzoj1999][noip2007]Core树网的核

    好久没写题解了.这题不算太水就写一下题解. 话说回来,虽然不水但是挺裸.可以说题意即一半题解了. 我猜粘了题面也没有人去看的,所以直接人话题意了. 给一棵树,点数1e6,(当年noip的n当然是只有3 ...