码迷,mamicode.com
首页 > 其他好文 > 详细

codevs 1497取余运算

时间:2017-06-15 20:25:49      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:等于   body   amp   ret   middle   names   ble   输出   int   

1497 取余运算

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 钻石 Diamon
 
题目描述 Description

输入b,p,k的值,编程计算bp mod k的值。其中的b,p,k*k为长整型数(2^31范围内)。

输入描述 Input Description

b p k 

输出描述 Output Description

输出b^p mod k=?

=左右没有空格

样例输入 Sample Input

2  10  9

样例输出 Sample Output

2^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取余运算

标签:等于   body   amp   ret   middle   names   ble   输出   int   

原文地址:http://www.cnblogs.com/zzyh/p/7019693.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!