HDU - 3003 - Pupu(快速幂)

来源:互联网 发布:网络直播系统 编辑:程序博客网 时间:2024/05/02 02:34

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=3003;

题目大意:

有一种生物,这种生物有N层外壳,每一层壳每经过一天太阳照射会由透明变成不透明,或者由不透明变成透明,
阳光可以穿过透明的壳照到里层的壳,问需要经过多少天可以让所有壳都变透明一次。

题解:

推出公式 ans = (2 ^ (n-1) + 1)mod n

AC代码:

#include <iostream>#include <cstdio>using namespace std;long long int N;long long int multi(long long int a, long long int b){    long long int re = 1;    while(b > 0)    {        if(b & 1)        {            re = (re * a)%N;        }        a = (a * a)%N ;        b >>= 1;    }    return re%N;}int main(){    while(cin >> N)    {        if(N == 0)break;        cout << (multi(2, N-1) + 1 )% N <<endl;    }    return 0;}

tips:

有一种计算2的K次方的黑科技:

__int64 d = (__int64)1<<k;

0 0
原创粉丝点击