2018 ACM/ICPC 焦作赛区网络赛 G 大数取模,费马小定理

There are NN children in kindergarten. Miss Li bought them NN 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)(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 TT, the number of test case.

The next TT lines, each contains an integer NN.

1 \le T \le 1001≤T≤100

1 \le N \le 10^{100000}1≤N≤10100000

Output

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

样例输入复制

1
4

样例输出复制

8

题意:简单规律题,打个表找到规律,就是求2^(n-1)对1e9+7进行取模。

题解:n很大,有10^100000,所以要用大数取模。

很容易想到费马小定理。 当p为质数时,2^(p-1)mod p = 1,所以用费马小定理降幂。

设n-1中有x个(p-1),那么 2^(n-1) mod p= 2^(x*(p-1)+(n-1)%(p-1)) mod p = 2^((n-1)%(p-1)) mod p 

大数取模求出(n-1)%(p-1) 然后快速幂就行了。


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<vector> 
using namespace std;
#define inf 1e18
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a));
#define lowbit(x)  x&-x;
typedef  long long ll;
const int maxn = 1e5+5;
const int mod = 1e9 + 7;
char s[1005];
ll quick(ll a,ll b){
	ll res = 1;
	while(b){
		if(b&1) res = (res*a)%mod;
		b >>= 1;
		a = (a*a)%mod;
	}
	return res;
}
int main(){
	int t;
	cin>>t;
	
	while(t--){
		scanf("%s",s);
		ll n = s[0] - '0';
		ll MOD = mod - 1;
		int len = strlen(s);
		for(int i = 1; i < len; i++){
			n = (n*10 + s[i]-'0')%MOD;
		}
		n--;
		ll N = quick(2,n);
		printf("%lld\n",N);
	}
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值