zoukankan      html  css  js  c++  java
  • 【费马小定理+快速幂取模】ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies

    G. 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.

    1≤T≤100

    1≤N≤10^100000

    Output

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

    样例输入

    1
    4

    样例输出

    8

    有n个孩子,n个糖果,从第一个孩子开始给,任意给随便数量的糖果,至少给一个

    问你一共有多少种不同的给的方案,答案应该为2^(n-1)%1e9+7

    n可以到10的100000次方,所以应当用降幂公式

    费马小定理 (a^n)%mod,如果mod为质数,a^(n%(mod-1))%mod

    #include <bits/stdc++.h>
    #define ll long long
    using namespace std;
    const ll mod=1e9+7;
    ll quickpow(ll a,ll b)
    {
        ll r=1;
        while(b)
        {
            if(b&1) r=r*a%mod;
            a=a*a%mod;
            b/=2;
        }
        return r;
    }
    char s[100005];
     
    int main()
    {
        int T;
        scanf("%d",&T);
     
        while(T--)
        {
            scanf("%s",s);
            int len=strlen(s);
            ll temp=0;
            for(int i=0;i<len;i++)
            {
                temp=(temp*10+s[i]-'0')%(mod-1);
            }
            temp--;
            if(temp<0) temp=mod-1;
            printf("%lld
    ",quickpow(1LL*2,temp));
        }
        return 0;
    }

    转载来自大佬 cherry:

    https://blog.csdn.net/qq_41037114/article/details/82716896
  • 相关阅读:
    设计模式的六大原则 ---- 理论知识
    动手编写TCP服务器系列之一:日志文件
    Shell语言系列之一:文件处理
    给Amazon ec2 增加卷(Volume)并挂载到系统
    Java打包问题之一:打包出现java.io.IOException: invalid header field
    struct中长度为0的数组用途与原理
    child和childNodes的区别
    学习es6 setter/getter研究
    tabIndex-bootstrap中Get到的
    tml兼容性
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/9653109.html
Copyright © 2011-2022 走看看