ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies 【快速模幂+费马小定理】

本文探讨了一个有趣的数学问题,即幼儿园老师如何随机分配糖果给孩子们,以产生不同的分配结果。文章详细介绍了问题背景,包括输入输出格式,以及解决此问题的高效算法。通过运用费马小定理和快速幂运算,提出了一种计算2^(n-1)%mod值的方法,适用于n值较大的情况。
  •  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

题目来源

ACM-ICPC 2018 焦作赛区网络预赛

问题描述

输入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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值