题目:
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
说一下我的理解:
这种动物总共有两种类型的皮肤:透明&不透明,不透明的皮肤被太阳晒一整天就可以变成透明的,阳光可以透过透明的皮肤进入内部(因为有多层皮肤)。透明的皮肤晒一整天后就会变成不透明的,并且会隔绝接下来一天的阳光。当所有的皮肤都变过一次透明的(初始所有的皮肤都不是透明的),这只动物也就长大了。问一只n层皮肤的动物经过多少天才能长大。
题意懂了就可以根据数据推出公式: (2的n-1次方+1) % n.
参考代码:
#include <iostream>
#include <cstdio>
typedef long long ll;
using namespace std;
ll n;
ll quickmod(ll a,ll b,ll mod) {//快速幂;
ll res = 1;
a = a % mod;
while (b > 0) {
if (b & 1) {
res = res * a % mod;
}
b = b / 2;
a = a * a % mod;
}
return res;
}
int main() {
while (scanf("%lld", &n) != EOF && n) {
ll a = 2;
ll res = quickmod(a,n-1,n);
ll ans = (res + 1 % n) % n;
printf("%lld\n", ans);
}
return 0;
}