bupt328 cony(指数求模)

cony
Accept:16     Submit:79
Time Limit:1000MS     Memory Limit:65536KB
Description
Dante 最近研究出了一种新的兔子cony,这种兔子是一种雌雄同体的生物,具有超强的繁殖能力,每个月它可以产下a-1个新兔子,但每月只能产一次.在b个月以后Dante实验室已经拥有了一大群兔子,有一次他突发奇想准备给cony们照相,他有无数个长凳,每个长凳上可以站c个cony,而且每个长凳上要站满了所有的cony才能使用下一个长凳,可是Dante的兔子们不可能正好让每个长凳都站满,于是他只好舍弃一些cony,现在你能告诉他最少需要舍弃多少个cony呢?(Dante第一个月只有一只兔子)


Input
多组数据测试,每行输入三个整数 a(1 < a < 1000) b(0 < b < 1000000000) c(0 < c< 1000000)

Output
每行输出一个数,需要舍弃的兔子数

Sample Input

3 4 5


Sample Output

2


算法分析这个题:就是一个指数求模 使用 long long

View Code
#include<iostream>
using namespace std;

long long pow_mod(long long a,long long b,long long n)
{
long long t;
if(b==0) return 1;
if(b==1) return a;
t=pow_mod(a,b/2,n);
if(b%2==0)

return ((t%n)*(t%n))%n;

else

return (a*(t%n)*(t%n))%n;

}

int main()
{
long long a,b,n;
while(cin>>a>>b>>n)
{
cout<<pow_mod(a,b-1,n)<<endl;
}
return 0;
}

 

posted @ 2012-03-07 19:22  mtry  阅读(273)  评论(0编辑  收藏  举报