HDU 4704 Sum

Problem Description
 
Sample Input
2
 
Sample Output
2
Hint
1. For N = 2, S(1) = S(2) = 1. 2. The input file consists of multiple test cases.
 
化简后得到其实式子就是求2^(n-1) % p
注意快速幂降幂
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <bitset>
using namespace std;
typedef long long LL;
#define MOD 1000000007


LL Fast_Mod(LL a,LL b){
   LL res = 1,base = a;
   while (b){
       if (b&1) res = (res * base) % MOD;
       base = (base * base) % MOD;
       b = b >> 1;
   }
   return res;
}
string n;
LL num = 0;
int main(){
  // freopen("test.in","r",stdin);
  ios::sync_with_stdio(false);
  while (cin >> n) {
    int len = n.length();
    num = 0;
    for (int i=0;i<len;i++){
      num = (num * 10 + (LL)(n[i] - '0')) % (MOD-1);
    }
    if (num == MOD-1){
      cout << Fast_Mod(2,MOD-2) << endl;
    }
    else {
      cout << Fast_Mod(2,num-1) << endl;
    }
  }
  return 0;
}
View Code
原文地址:https://www.cnblogs.com/ToTOrz/p/7382263.html