# ACM-ICPC 2018 焦作賽區網絡預賽 G. Give Candies [ 費馬小定理 + 快速冪 + 大數 ]

## 題目鏈接：

ACM-ICPC 2018 焦作賽區網絡預賽 G. Give Candies

## AC代碼：

#include <stdio.h>
#include <string.h>
#include <memory.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
char str[maxn];
int n[maxn];

ll mod_mult(ll a, ll b, ll mod) {
a %= mod;
b %= mod;
ll ans = 0;
while (b > 0) {
if (b & 1) {
ans += a;
if (ans >= mod)
ans -= mod;
}
a <<= 1;
if (a >= mod) a = a - mod;
b >>= 1;
}
return ans;
}

ll mod_pow(ll x, ll n, ll mod) {
if (n == 0) return 1;
ll res = mod_pow(mod_mult(x , x , mod), n / 2, mod);
if (n & 1) res = mod_mult(res , x , mod);
return res;
}

ll Cut() {
int len = (int)strlen(str);
for(int i = 0; i < len; i++)
n[i] = str[i] - '0';
ll ans = 0;
for(int i = 0; i < len; i++)
ans = (ans * 10 + n[i]) % (mod - 1);
return ans;
}

int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%s", str);
ll res = Cut();
if(res == 0)
printf("1\n");
else
printf("%lld\n", mod_pow(2, res - 1, mod));
memset(n, 0, sizeof(n));
memset(str, 0, sizeof(str));
}
return 0;
}


# Give Candies

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 .

### Output

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

### 樣例輸⼊

1
4

### 樣例輸出

8

### 題目來源

ACM-ICPC 2018 焦作賽區網絡預賽