codevs 1497取余运算
2017-06-15 19:47
204 查看
1497 取余运算
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamon 题目描述 Description输入b,p,k的值,编程计算bp mod k的值。其中的b,p,k*k为长整型数(2^31范围内)。
输入描述 Input Descriptionb p k
输出描述 Output Description输出b^p mod k=?
=左右没有空格
样例输入 Sample Input2 10 9
样例输出 Sample Output2^10 mod 9=7
【code】
#include<iostream> #include<cstdio> #include<cstdlib> using namespace std; int b,p,k; int f(int); int main() { scanf("%d%d%d",&b,&p,&k); int tmpb=b; b%=k;//防止b过大 printf("%d^%d mod %d=%d\n",tmpb,p,k,f(p)); return 0; } int f(int x) { if(x==0)return 1;//任何数的0次方模k都等于1 int tmp=f(x/2)%k;//a*b%k=a%k*b%k%k; tmp=(tmp*tmp)%k; if(x%2==1)tmp=(tmp*b)%k; return tmp; }
相关文章推荐
- [数论][CODEVS 1497 取余运算]解题报告
- 取余运算(codevs 1497)
- Codevs 取余运算
- codevs2178 表达式运算Cuties
- 1497 取余运算
- [CODEVS1295]N皇后(位运算+搜索)
- 乘法运算(codevs 3254)
- CODEVS 1497 取余运算
- 1497 取余运算
- N皇后_codevs1295_dfs+位运算_水
- 1497 取余运算
- codevs 2190 有理逼近 题解
- codevs3145 汉诺塔游戏
- CODEVS 1230 元素查找
- VS CODE 下的 ESLint 安装以及使用
- codevs 1082 线段树:区间更新,区间求和
- 【codevs3990】中国余数定理2
- Java中的取余运算和取模运算
- 【BZOJ1037】【codevs1410】生日聚会,DP
- 【codevs1380】没有上司的舞会 树形dp