Pupu
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1665 Accepted Submission(s): 652
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?
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
虽然是快速幂,但首先要用规律推出来 2^(n-1)+1这个公式, 然后取模就可以了
#include <iostream>
using namespace std;
typedef long long LL;
int main()
{
LL n;
while(cin>>n,n!=0)
{
LL x=2, result, mod=n;
n=n-1;
while((n&1)==0)
{
x*=x;
x%=mod;
n>>=1;
}
result=x;
result%=mod;
n>>=1;
while(n!=0)
{
x*=x;
x%=mod;
if(n&1)
{
result*=x;
result%=mod;
}
n>>=1;
}
cout<<result+1<<endl;
}
return 0;
}
using namespace std;
typedef long long LL;
int main()
{
LL n;
while(cin>>n,n!=0)
{
LL x=2, result, mod=n;
n=n-1;
while((n&1)==0)
{
x*=x;
x%=mod;
n>>=1;
}
result=x;
result%=mod;
n>>=1;
while(n!=0)
{
x*=x;
x%=mod;
if(n&1)
{
result*=x;
result%=mod;
}
n>>=1;
}
cout<<result+1<<endl;
}
return 0;
}
下面是快速幂的模版
常规求幂
int pow1( int a, int b ) { int r = 1; while( b-- ) r *= a; return r; }
二分求幂(一般)
int pow2( int a, int b ) { int r = 1, base = a; while( b != 0 ) { if( b % 2 ) r *= base; base *= base; b /= 2; } return r; }
快速求幂(位操作)
int pow3( int a, int b ) { int r = 1, base = a; while( b != 0 ) { if( b & 1 ) r *= base; base *= base; b >>= 1; } return r; }
快速求幂(更高效率的位运算法)
int pow4(int x, int n) { int result; if (n == 0) return 1; else { while ((n & 1) == 0) { n >>= 1; x *= x; } } result = x; n >>= 1; while (n != 0) { x *= x; if ((n & 1) != 0) result *= x; n >>= 1; } return result; }