zoukankan      html  css  js  c++  java
  • HDU 4704 Sum (高精度+快速幂+费马小定理+二项式定理)

    Sum
    Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u

    Description

     

    Sample Input

    2
     

    Sample Output

    2

    Hint

     1. For N = 2, S(1) = S(2) = 1. 2. The input file consists of multiple test cases. 
    题意:给定一个很大的整数N,把N分成1,2,3,4....N个数,问一共有多少种方案。
    题解:把这个问题看做是有N个箱子,N-1个空,插空分组,一共有多少种方案。
       
    分成1份则是C(n-1,0);
       分成2份则是C(n-1,1);
       分成3份则是C(n-1,2);
       ...
       分成n份则是C(n-1,n-1);
       ans = sum( C(n-1,i) ) (0<=i<=n-1)=2^(n-1);(二项式定理)
       由于要取模 而且 2 与 mod 互质 ,因此可以用费马小定理来降幂。
    #include <iostream>
    #include <cstring>
    using namespace std;
    typedef long long ll;
    const int mod=1e9+7;
    ll pow(ll a,ll b,ll m)
    {
        int ans=1;
        while(b)
        {
            if(b&1)
                ans=ans*a%m;
            b>>=1;
            a=a*a%m;
        }
        return ans;
    }
    ll get(char c[])
    {
        ll sum=c[0]-'0';
        int len=strlen(c);
        for(int i=1;i<len;i++)
            sum=(sum*10+(c[i]-'0'))%(mod-1);
        return sum;
    }
    int main()
    {
        char c[100005];
        while(cin>>c)
        {
            ll sum=get(c);
            ll ans=pow(2,sum-1,mod);
            cout<<ans<<endl;
        }
    }
    
    
  • 相关阅读:
    UITabar 设置字体大小/颜色
    NSURLSession的基本使用
    报错/警告提示
    实现毛玻璃模糊效果/DRNRealTimeBlur
    免证书真机调试
    xcode添加音效
    NSCalenda日历类
    NSDate--日期格式
    NSArray其中的方法--遍历,
    Mysql学习笔记004
  • 原文地址:https://www.cnblogs.com/Ritchie/p/5648110.html
Copyright © 2011-2022 走看看