福州大学oj 1752 A^B mod C ===>数论的基本功。位运用。五星*****
Problem 1752 A^B mod C
Accept: 579 Submit: 2598
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,B,C<2^63).
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
Sample Output
Source
FZU 2009 Summer Training IV--Number Theory
Source Code
RunID: 508246UserID: 987690183Submit time: -- ::52Language: Visual C++Length: Bytes.Result: Wrong Answer #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std; unsigned __int64 mgml(unsigned __int64 a,unsigned __int64 n,unsigned __int64 m )
{
unsigned __int64 ans=;
a=a%m;
while(n)
{
if(n&)
{
ans=(ans*a)%m;//当数字很大的时候,ans*a就溢出了。
}
n=n>>;
a=(a*a)%m;//这边的也是一样的。
}
return ans;
} int main()
{
unsigned __int64 a,b,c;
while(scanf("%I64u%I64u%I64u",&a,&b,&c)>)
{
printf("%I64u\n",mgml(a,b,c));
}
return ;
}
因此,通过以前的方法就难以解决这个问题,这也是这道题好的地方了。
处理的方法,加一个a*b%m的函数,解决在错误代码里遇到的问题。
AC代码:
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std; typedef __int64 LL; LL pow1_sum(LL a,LL b,LL mod)
{
a=a%mod;
b=b%mod;
LL cur=;
while(b)
{
if(b&)
{
cur=cur+a;
if(cur>=mod) cur=cur-mod;
}
a=a<<;
if(a>=mod) a=a-mod;
b=b>>;
}
return cur;
}
LL pow_sum(LL a,LL b,LL mod) //a^b%mod
{
LL cur= ;
a=a%mod;
while(b)
{
if(b&)
{
cur=pow1_sum(cur,a,mod);
}
a=pow1_sum(a,a,mod);
b=b>>;
}
return cur;
}
void solve(LL a,LL b,LL mod)
{
LL result = pow_sum(a,b,mod);
printf("%I64d\n",result);
}
int main()
{
LL a,b,mod;
while(scanf("%I64d%I64d%I64d",&a,&b,&mod)>)
{
solve(a,b,mod);
}
return ;
}
这道题,没有很复杂的算法,但是又容易出错,很值得推荐
福州大学oj 1752 A^B mod C ===>数论的基本功。位运用。五星*****的更多相关文章
- [FOJ 1752] A^B mod C
Problem 1752 A^B mod C Accept: 750 Submit: 3205Time Limit: 1000 mSec Memory Limit : 32768 KB ...
- 51Nod1123 X^A Mod B 数论 中国剩余定理 原根 BSGS
原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1123.html 题目传送门 - 51Nod1123 题意 $T$ 组数据. 给定 $A,B,C$,求 ...
- 51Nod1039 N^3 Mod P 数论 原根 BSGS
原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1039.html 题目传送门 - 51Nod1039 题意 题解 这题我用求高次剩余的做法,要卡常数. ...
- 51Nod1038 X^A Mod P 数论 原根 BSGS
原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1038.html 题目传送门 - 51Nod1038 题意 题解 在模质数意义下,求高次剩余,模板题. ...
- FZU 1650 1752 a^b mod c
http://acm.fzu.edu.cn/problem.php?pid=1752 http://acm.fzu.edu.cn/problem.php?pid=1650 给跪了. 我的快速幂会越界. ...
- FZU 1752 A^B mod C(快速加、快速幂)
题目链接: 传送门 A^B mod C Time Limit: 1000MS Memory Limit: 65536K 思路 快速加和快速幂同时运用,在快速加的时候由于取模耗费不少时间TLE了 ...
- 中国剩余定理的应用:猪的安家 ->福州大学 OJ
Problem 1402 猪的安家 Accept: 984 Su ...
- HDU 4389 X mod f(x)
题意:求[A,B]内有多少个数,满足x % f(x) == 0. 解法:数位DP.转化为ans = solve(b) - solve(a - 1).设dp[i][sum][mod][r]表示长度为i, ...
- HDU - 4389 X mod f(x)(数位dp)
http://acm.hdu.edu.cn/showproblem.php?pid=4389 题意 为[A,B] 区间内的数能刚好被其位数和整除的数有多少个. 分析 典型的数位dp...比赛时想不出状 ...
随机推荐
- JQuery Mobile - 为什么绑定事件后会被多次执行?
JQuery Mobile 在绑定事件时候,发现会被多次执行,为什么啊? 原来,jquery click 不是替换原有的function ,而是接着添加,所以才会执行次数越来越多,怎么办才能按需实现 ...
- 782. Transform to Chessboard
An N x N board contains only 0s and 1s. In each move, you can swap any 2 rows with each other, or an ...
- CRUD组件的高阶使用
1.list页面自定列显示: class PermissionConfig(sites.AryaConfig): def dabo(self, obj=None, is_header=Fa ...
- Queue-621. Task Scheduler
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where diffe ...
- linux parallel rsync 拷贝N多文件
先来个对比图看一下, 左边图是普通 rsync 目录拷贝, 右边图是借助 parallel 工具并发起了多个 rsync centos6.5安装 parallel #!/bin/bash # Inst ...
- powerDesiner设计数据库的一些用法
数据库的设计主要有以下几个步骤: 1:需求分析:根据业务需求分析出满足客户的需求,从而建立相应的数据库 2:概念设计:通过数据抽象,设计系统概念模型,一般为E-R模型:(entity-relation ...
- ubuntu14.0 安装 node v8.11.1(任意版本)
由于众所周知的原因,通过node官网下载速度十分慢,我这里通过淘宝镜像安装 node8.11.1,其他版本同理. node版本淘宝镜像地址:https://npm.taobao.org/mirrors ...
- Pyplot绘图的格式
字符 颜色 ‘b’ 蓝色,blue ‘g’ 绿色,green ‘r’ 红色,red ‘c’ 青色,cyan ‘m’ 品红,magenta ‘y’ 黄色,yellow ‘k’ 黑色,black ‘w’ ...
- iOS 代码混淆的简单使用
1.工具下载 http://stevenygard.com/projects/class-dump/ 选择dmg安装包 2.打开终端输入:open/usr/local/bin 3. 4.修改权限在 ...
- django框架--cookie/session
目录 一.http协议无状态问题 二.会话跟踪技术--cookie 1.对cookie的理解 2.cookie的使用接口 3.cookie的属性 4.使用cookie的问题 三.会话跟踪技术--ses ...