0
点赞
收藏
分享

微信扫一扫

HDU - 3003 - Pupu (快速幂取模!)


Pupu
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1133    Accepted Submission(s): 445

Problem Description
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?
 

Input
There are many testcase, each testcase only contains one integer N, the number of skins, process until N equals 0
 

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

Sample Input
2 3 0
 

Sample Output
1 2
 

Source
2009 Multi-University Training Contest 11 - Host by HRBEU
 

题意:PuPu有n层皮肤,每层皮肤都有2种状态:透明和不透明,每层皮肤如果能被太阳照射到,则被太阳照射一天后都会变换状态。PuPu在出生的时候,所有的皮肤都为不透明的,直到每一层的皮肤都有过变为透明状态的时候,PuPu也就长大了。

问有n层皮肤的PuPu几天后能长大。

分析:

1:透明  0:不透明

例如:

00   10 01   3天

0000    1000 0100 1100  0010 1010  0110  1110 0001     九天

推出(2^(n-1)+1)(忽略最后一位,就是前三位的全排列+最后一个0001)

题意要求%n

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
using namespace std;
typedef long long ll;
const int MAXN=50000+5;//最大元素个数
ll n;//元素个数
ll quick_qow(ll a,ll b,ll p)///计算a^b %p
{
ll ans=1%p;///ans不能为1,b为0,p为1的情况不成立
for(;b;b>>=1)
{
if(b&1)
ans=(ans*a)%p ;
a=a*a%p ;
}
return (ans+1)%p;
}
int main()
{

while(scanf("%lld",&n)!=-1&&n!=0)
{

printf("%lld\n",quick_qow(2,n-1,n));
}
return 0;
}

 

举报

相关推荐

0 条评论