zoukankan      html  css  js  c++  java
  • ACM-ICPC 2018 焦作赛区网络预赛- G:Give Candies(费马小定理,快速幂)

    There are N children in kindergarten. Miss Li bought them NNN 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 NNN.

    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 焦作赛区网络预赛

    题意

    计算2^(n-1)的值

    思路

    利用费马小定理:a^{b}modc=a^{b*mod{(c-1)}}modc求出(n-1)%(1e9+7)的值,然后利用快速幂计算结果

    注意:用int型会TLE,要用long long

    #include <bits/stdc++.h>
    #define ms(a) memset(a,0,sizeof(a))
    #define ll long long
    const ll maxn=1e6+10;
    const int mod=1e9+7;
    using namespace std;
    char ch[maxn];
    ll Pow(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 argc, char const *argv[])
    {
    	ios::sync_with_stdio(false);
    	register int t;
    	ll ans,l;
    	cin>>t;
    	while(t--)
    	{
    		ans=0;
    		cin>>ch;
    		l=strlen(ch);
    		for(register int i=0;i<l;i++)
    		{
    			ans=ans*10+ch[i]-'0';
    			ans%=(mod-1);
    		}
    		cout<<Pow(2,ans-1)<<endl;
    	}
    	return 0;
    }
  • 相关阅读:
    python中的反射
    ZOJ 3827 Information Entropy 水
    我的软考之路(七)——数据结构与算法(5)之查找
    nginx.conf 集群完整配置
    恼人的函数指针(二)
    C语言100个经典的算法
    spring事务心得积累
    Vue报错:OPTIONS 405 Method Not Allowed以及CORS跨域错误
    IDA脚本dump内存的示例
    lightProxy
  • 原文地址:https://www.cnblogs.com/Friends-A/p/10324355.html
Copyright © 2011-2022 走看看