1. 程式人生 > >ACM-ICPC 2018 焦作賽區網路預賽 G. Give Candies 【快速模冪+費馬小定理】

ACM-ICPC 2018 焦作賽區網路預賽 G. Give Candies 【快速模冪+費馬小定理】

  •  1000ms
  •  65536K

There are N children in kindergarten. Miss Li bought them N candies. To make the process more interesting, Miss Li comes up with the rule: All the children line up according to their student number (1...N), and each time a child is invited, Miss Li randomly gives him some candies (at least one). The process goes on until there is no candy. Miss Li wants to know how many possible different distribution results are there.

Input

The first line contains an integer T, the number of test case.

The next T lines, each contains an integer N.

1≤T≤100

1≤N≤10^100000

Output

For each test case output the number of possible results (mod 1000000007).

樣例輸入

1
4

樣例輸出

8

題目來源

問題描述

輸入n求2^(n-1)%mod的值,注意n的值比較大,mod=1000000007

思路

由費馬小定理可得2^(n-1)%mod等於2^((n-1)%(mod-1)) % mod

AC的C++程式碼:

#include<iostream>
#include<string> 

using namespace std;
typedef long long ll;
const ll mod=1000000007;

ll f(ll x,ll n)
{
	ll res=1L;
	while(n){
		if(n&1) res=res*x%mod;
		x=x*x%mod;
		n>>=1;
	}
	return res;
}

int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		string s;
		cin>>s;
		ll v=0;
		for(int i=0;i<s.length();i++)
		  v=(v*10+s[i]-'0')%(mod-1);
		v=(v-1+mod-1)%(mod-1);
		printf("%lld\n",f(2,v));
	}
	return 0;
}