快速幂模板

来源:互联网 发布:大鱼海棠影评知乎 编辑:程序博客网 时间:2024/06/17 06:41
#include <iostream>
#include <cstdio>
using namespace std;
int power2(int a, int b, int c)
{
    int res = 1;
    a %= c;
    while (b)
    {
        if (b%2==1)
            res = (res * a) % c;
        a = (a * a) % c;
        b=b/2;
    }
    return res; 
}
int main()
{
    int n;
    while (~scanf("%d", &n))
    {
        cout << power2(2, n, 7) << endl;
    }
    return 0;
}