zoukankan      html  css  js  c++  java
  • icpc 江苏 D Persona5 组合数学 大数阶乘(分段阶乘) 大数阶乘模板

    Persona5 is a famous video game.

    In the game, you are going to build relationship with your friends.

    You have NN friends and each friends have his upper bound of relationship with you. Let's consider the i^{th}ith friend has the upper bound U_iUi. At the beginning, the relationship with others are zero. In the game, each day you can select one person and increase the relationship with him by one. Notice that you can't select the person whose relationship with you has already reach its upper bound. If your relationship with others all reach the upper bound, the game ends.

    It's obvious that the game will end at a fixed day regardless your everyday choices. Please calculate how many kinds of ways to end the game. Two ways are said to be different if and only if there exists one day you select the different friend in the two ways.

    As the answer may be very large, you should output the answer mod 10000000071000000007

    Input Format

    The input file contains several test cases, each of them as described below.

    • The first line of the input contains one integers N(1 le N le 1000000)(1N1000000), giving the number of friends you have.
    • The second line contains NN integers. The i^{th}ith integer represents U_iUi ( 1 le U_i le 1000000)(1Ui1000000), which means the upper bound with i^{th}ith friend. It's guarantee that the sum of U_iUi is no more than 10000001000000.

    There are no more than 1010 test cases.

    Output Format

    One line per case, an integer indicates the answer mod 10000000071000000007.

    样例输入

    3
    1 1 1
    3
    1 2 3

    样例输出

    6
    60

    题目来源

    The 2018 ACM-ICPC China JiangSu Provincial Programming Contest

     

    题意:你有n个朋友,开始你们的默契为0,每天都可以增加一,当你和你的朋友的默契达到每一个人的最高默契值(题目中给出的n个数代表n个朋友的默契值)结束,问你有多少种方式达到最高默契

    这个题目可以抽象成我有a1,a2,...,an个球,将这些球放进a1+a2+...+an个箱子里,看有多少种方法?

    接下来就是一个排列组合了,很明显我最开始放a1的时候有C(a1,n)种方法,放a2时有C(a2,n-a1)种方法,放an时有C(an,an)种方法,放最后一个时只剩下an个箱子了

    所以我的所有方案数是C(a1,n)* C(a2,n-a1)* C(an,an)

    考虑到C(a1,n)= (n*(n-1)*...*(n-a1+1))/a1!,  C(a2,n-a1)=  ((n-a1)*(n-a1-1)*...*(n-a1-a2+1))/a2! ...

    所以C(a1,n)* C(a2,n-a1)* C(an,an)= (a1+a2+...+an)!/(a1!*a2!*...*an!)

    开始写题目的时候没看到所有的数的和不超过10^6这句话,于是用了分段求阶乘的方法求了大数的阶乘(以为所有数加在一起总和超过了10^12)

    这里顺便提下大数的阶乘计算方法把,也是一种应该掌握的计算方法

    分块打表,每10000000打一个表,算出阶乘对mod取模的结果

    打表程序

    #include<iostream>
    #include<cstdio>
    #define lon long long
    using namespace std;
    const int maxn=110;
    lon n,p,a[maxn];
    int main()
    {
        freopen("1.out","w",stdout);lon ans=1;
        for(lon i=0;i<=1000000007;i+=10000000)
        {
            for(lon j=i+1;j<=i+10000000;j++)
            ans=(ans*j)%1000000007;
            cout<<ans<<",";
        }
        return 0;
    }
    

     然后判断一下数位于哪个区间,在哪个一路乘就行

    #include <map>
    #include <set>
    #include <stack>
    #include <cmath>
    #include <queue>
    #include <cstdio>
    #include <vector>
    #include <string>
    #include <cstring>
    #include <iomanip>
    #include <iostream>
    #include <algorithm>
    #define debug(a) cout << #a << " " << a << endl
    using namespace std;
    const int maxn = 1e6;
    const int mod = 1e9 + 7;
    typedef long long ll;
    ll a[maxn+10], b[maxn+10];
    ll num[110]={1,682498929,491101308,76479948,723816384,67347853,27368307,
    625544428,199888908,888050723,927880474,281863274,661224977,623534362,
    970055531,261384175,195888993,66404266,547665832,109838563,933245637,
    724691727,368925948,268838846,136026497,112390913,135498044,217544623,
    419363534,500780548,668123525,128487469,30977140,522049725,309058615,
    386027524,189239124,148528617,940567523,917084264,429277690,996164327,
    358655417,568392357,780072518,462639908,275105629,909210595,99199382,
    703397904,733333339,97830135,608823837,256141983,141827977,696628828,
    637939935,811575797,848924691,131772368,724464507,272814771,326159309,
    456152084,903466878,92255682,769795511,373745190,606241871,825871994,
    957939114,435887178,852304035,663307737,375297772,217598709,624148346,
    671734977,624500515,748510389,203191898,423951674,629786193,672850561,
    814362881,823845496,116667533,256473217,627655552,245795606,586445753,
    172114298,193781724,778983779,83868974,315103615,965785236,492741665,
    377329025,847549272,698611116};
    ll qow( ll x, ll y ) {
        ll ans = 1;
        while( y ) {
            if( y&1 ) {
                ans = (ans*x)%mod;
            }
            x = x*x%mod;
            y /= 2;
        }
        return ans;
    }
    int main() {
        std::ios::sync_with_stdio(false);
        ll sum = 1;
        for( ll i = 1; i <= maxn; i ++ ) {
            sum = sum*i%mod;
            a[i] = sum;
        }
        ll n;
        while( cin >> n ) {
            ll ans = 0;
            for( ll i = 0; i < n; i ++ ) {
                cin >> b[i];
                //debug(a[b[i]]);
                ans += b[i];
            }
            if( ans >= mod ) {
                cout << 0 << endl;
                continue;
            }
            ll now = ans/10000000, t = ans;
            ans = num[now];
            //debug(ans);
            for( ll i = now*10000000+1; i <= t; i ++ )
                ans = ans*i%mod;
            //ans = num[ans];
            //debug(ans);
            for( ll i = 0; i < n; i ++ ) {
                //ans = ans/a[b[i]];
                ans = (ans*qow(a[b[i]],mod-2))%mod;
            }
            cout << ans << endl;
        }
        return 0;
    }
    

      

    彼时当年少,莫负好时光。
  • 相关阅读:
    116. 跳跃游戏
    182. 删除数字
    unity判断LayerMask里面是否包含你想要的Layer
    Unity Animator自动连线
    Behavior designer 行为树插件 笔记
    unity2d 完美斜坡方案
    youtube 油管视频带字幕下载
    unity资源提取工具AssetStudio
    ugui 通用页签管理器
    unity拓展编辑器入门
  • 原文地址:https://www.cnblogs.com/l609929321/p/9362517.html
Copyright © 2011-2022 走看看