福州大学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...比赛时想不出状 ...
随机推荐
- kolla-ansible 源码下载
下载地址: https://pypi.org/project/kolla-ansible/ ansible下载: https://releases.ansible.com/ansible/rpm/re ...
- 在QtCreater中配置Artistic Style格式化Qt程序源代码!!
Qt很吸引人,可能是我对Qt开发工具QtCreater不熟悉,只发现里面提供了一个快捷键:"ctrl+i",很多人说这就是格式化代码快捷键,我发现这仅仅是代码缩进,并不是真正意义上 ...
- 730. Count Different Palindromic Subsequences
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- dfs找負環
某些無良出題人可能會卡bfs找負環,所以要用dfs 核心代碼(以jzoj5173為例): #include<bits/stdc++.h> using namespace std; #def ...
- 从getwebshell到绕过安全狗云锁提权再到利用matasploit进服务器
本文作者:i春秋签约作家——酷帥王子 一. 利用getwebshell篇 首先对目标站进行扫描,发现是asp的,直接扫出网站后台和默认数据库,下载解密登陆如图: 下面进后台发现有fckeditor,而 ...
- beyondCompare试用期到期解决办法
找到beyond Compare 4文件夹下面的BCUnrar.dll,将其删掉或者重命名,再重新打开接着使用! 我这是转的,到期之后试试.
- vue 学前班003(生命周期)
ue把整个生命周期划分为创建.挂载.更新.销毁等阶段,每个阶段都会给一些“钩子”让我们来做一些我们想实现的动作.学习实例的生命周期,能帮助我们理解vue实例的运作机制,更好地合理利用各个钩子来完成我们 ...
- 深入理解 js this 绑定机制
函数调用位置 与词法作用域相反的是,this的指向由函数运行时决定,它是动态的,随着函数调用位置变化而变化. 要理解 this,首先要理解调用位置:调用位置就是函数在代码中被调用的位置(而 不是声明的 ...
- 读/写锁的实现和应用(高并发状态下的map实现)
程序中涉及到对一些共享资源的读和写操作,且写操作没有读操作那么频繁.在没有写操作的时候,两个线程同时读一个资源没有任何问题,所以应该允许多个线程能在同时读取共享资源.但是如果有一个线程想去写这些共享资 ...
- linux内核移植过程问题总结
移植内核:2.6.30.4内核根目录下的.config为当前配置内核的且已经配置好的内核配置.make zImage以此为依据配置内核的过程:cd linux-2.6.30.4(进入Linux根目录) ...