福州大学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...比赛时想不出状 ...
随机推荐
- 838. Push Dominoes
There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we si ...
- 转载:在spring中嵌入activemq
转载:http://www.dev26.com/blog/article/137 web开发站中的邮件发送使用了activemq我这是从网上找的进行了一些修改,记录下来,为了避免发送邮件时程序对用户操 ...
- linux系统上内网ip和和外网ip的关系
1.不同服务之间的访问需要使用公网IP+端口才能访问 2.服务器上一般都是域名访问,服务器会把ip+端口映射成固定的域名,所以如果想访问服务器上其他应用,就必须的放开应用限制 问题,在服务器上放开对某 ...
- #阿里云#云服务器部署Django(基础篇)
前言 本人能力有限,本文只是简单介绍基础部署流程,没有过多考虑系统安全等因素,请谅解.初学者参考了解,大神勿喷. 纯测试部署,采用阿里云ECS,系统Ubuntu 16.04 64位,部署采用nginx ...
- easyui datagrid 清除缓存方法
easyui datagrid 清除缓存方法 今天在项目中做了一个添加合同编号页面,添加合同编号了,在datagrid列表上没有显示刚才添加的那个合同编号. 这个问题在IE上特别明显. 原因是添加编号 ...
- (转)CentOS 7 单用户模式+救援模式
原文:http://blog.51cto.com/asd9577/1931442 https://www.cnblogs.com/zhangzeyu/p/6379754.html-------Cent ...
- ASP.NET 负载均衡 StateServer Session共享问题(经验记录)
(源地址:http://www.cnblogs.com/ryhan/p/3748976.html) 最近在改造公司的一个系统 支持F5硬件负载,由于系统后面还跟了个异步工具,需要将Admin上传的文件 ...
- 关于开发板用tftp下载失败分析
一.想实现开发板和PC ping通:(1)windows和linux桥接(2)用路由器将开发板和PC连接起来(3)将windows和linux以及开发板的IP设置成同一网段,注意不要和你同一个局域网的 ...
- java学习-AES加解密之AES-128-CBC算法
AES算法简介 AES是一种对称加密算法,或称分组对称加密算法. 是Advanced Encryption Standard高级加密标准,简称AES AES的基本要求是,采用对称分组密码体制.分组密 ...
- GRU
GRU模型(比LSTM减少了计算量) LSTM的模型,LSTM的重复网络模块的结构很复杂,它实现了三个门计算,即遗忘门.输入门和输出门. 而GRU模型如下,它只有两个门了,分别为更新门和重置门,即图中 ...