您的位置:首页 > 其它

HDOJ 题目3003Pupu(数学,(n^e)%m的快速求法)

2014-08-14 19:01 337 查看

Pupu

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1092    Accepted Submission(s): 435


[align=left]Problem Description[/align]
There is an island called PiLiPaLa.In the island there is a wild animal living in it, and you can call them PuPu. PuPu is a kind of special animal, infant PuPus play under the sunshine, and adult PuPus hunt near the seaside. They
fell happy every day.

But there is a question, when does an infant PuPu become an adult PuPu?

Aha, we already said, PuPu is a special animal. There are several skins wraping PuPu's body, and PuPu's skins are special also, they have two states, clarity and opacity. The opacity skin will become clarity skin if it absorbs sunlight a whole day, and sunshine
can pass through the clarity skin and shine the inside skin; The clarity skin will become opacity, if it absorbs sunlight a whole day, and opacity skin will keep sunshine out.

when an infant PuPu was born, all of its skins were opacity, and since the day that all of a PuPu's skins has been changed from opacity to clarity, PuPu is an adult PuPu.

For example, a PuPu who has only 3 skins will become an adult PuPu after it born 5 days(What a pity! The little guy will sustain the pressure from life only 5 days old)

Now give you the number of skins belongs to a new-laid PuPu, tell me how many days later it will become an adult PuPu?
 

[align=left]Input[/align]
There are many testcase, each testcase only contains one integer N, the number of skins, process until N equals 0
 

[align=left]Output[/align]
Maybe an infant PuPu with 20 skins need a million days to become an adult PuPu, so you should output the result mod N
 

[align=left]Sample Input[/align]

2
3
0

 

[align=left]Sample Output[/align]

1
2

 

[align=left]Source[/align]
2009 Multi-University Training Contest 11 - Host by HRBEU

 

[align=left]Recommend[/align]
gaojie   |   We have carefully selected several similar problems for you:  3001 3004 3002 3007 3006 
 

ac代码

只能说神奇,记住吧

#include<stdio.h>
__int64 fun(__int64 a,__int64 b,__int64 c)//要用64位整型
{
__int64 s=1;
while(b)
{
if(b%2==1)
s=s*a%c;
a=a*a%c;
b/=2;
}
return s;
}
int main()
{
__int64 n;
while(scanf("%I64d",&n)!=EOF,n)
{
__int64 s;
s=(fun(2,n-1,n)+1)%n;
printf("%I64d\n",s);
}
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: