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. Linux关闭开启防火墙命令

    在外部访问CentOS中部署应用时,需要关闭防火墙. 关闭防火墙命令:systemctl stop firewalld.service 开启防火墙:systemctl start firewalld. ...

  2. 交换机基础配置之stp生成树实验

    实验的要求是以上面的拓扑图为例,查看当前根桥的所在并把三成交换机switch1指定为根桥 我们可以用show spanning-tree来查看stp的信息 输入后会显示两部分 上面一部分为当前交换机自 ...

  3. 学习python第十五天,面向对象

    Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的. 面向对象技术简介 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集 ...

  4. Drazil and Tiles CodeForces - 516B (类拓扑)

    Drazil created a following problem about putting 1 × 2 tiles into an n × m grid: "There is a gr ...

  5. 003---wsgi和wsgiref模块

    WSGI: 全称:Web Server Gatway Interface ,web服务网关接口,独立的,与django无关,他们俩只是遵循一个约定,是一个协议. wsgiref模块: 实现了WSGI协 ...

  6. C++机试笔记

  7. HTML5 canvas 圆盘抽奖

    使用html5 canvas 绘制的圆盘抽奖程序 效果图: 贴上全部代码:  1 <!DOCTYPE html> <html> <head> <meta ch ...

  8. Linux与BSD不同

    https://linux.cn/article-3186-1.html https://www.howtogeek.com/190773/htg-explains-whats-the-differe ...

  9. WPF图片预览之移动、旋转、缩放

    原文:WPF图片预览之移动.旋转.缩放 RT,这个功能比较常见,但凡涉及到图片预览的都跑不了,在说自己的实现方式前,介绍一个好用的控件:Extended.Toolkit中的Zoombox,感兴趣的同学 ...

  10. 2 semantic ui 框架的应用

    为什么使用css框架 1.使用基础样式 :  ui segment 分段:内容片段 <link rel="stylesheet" href="css/semanti ...