1. 程式人生 > >2018ACM-ICPC焦作賽區網路賽題目---Give Candies

2018ACM-ICPC焦作賽區網路賽題目---Give Candies

                                                  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

思路:題目經過分析,答案就為計算2^(n-1)%mod的值即可,當n很大時,最大為10^100000,求2^n%mod   費馬小定理求解,即有 2^n%mod=(2^(n%(mod-1))%mod; 

AC程式碼:

//當n很大時,最大為10^100000,求2^n%mod   費馬小定理求解
//即有 2^n%mod=(2^(n%(mod-1))%mod; 
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
string s;
ll a[100010];
ll mod_pow(ll x,ll n){
    if(n==0) return 1;
    ll res=mod_pow(x*x%mod,n/2);
    if(n&1) res=res*x%mod;
    return res;
}
ll fun(ll k){
	ll sum=0;
	for(ll i=1;i<=k;i++){
		sum=(sum*10+a[i])%(mod-1);
	}
	return sum;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		cin>>s;
		ll k=s.size();
		for(ll i=0;i<k;i++){
			a[i+1]=s[i]-'0';
		}
		ll n=fun(k);
		cout<<mod_pow(2,n-1)<<endl;
	}
	return 0;
}