結果

問題 No.1214 Market
ユーザー chocoruskchocorusk
提出日時 2020-09-28 10:52:37
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 3,825 bytes
コンパイル時間 1,576 ms
コンパイル使用メモリ 140,984 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 11:55:23
合計ジャッジ時間 3,945 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 35 ms
4,376 KB
testcase_09 AC 34 ms
4,376 KB
testcase_10 AC 36 ms
4,376 KB
testcase_11 AC 38 ms
4,376 KB
testcase_12 AC 40 ms
4,376 KB
testcase_13 AC 38 ms
4,380 KB
testcase_14 AC 39 ms
4,380 KB
testcase_15 AC 35 ms
4,380 KB
testcase_16 AC 39 ms
4,380 KB
testcase_17 AC 38 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 7 ms
4,376 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 27 ms
4,380 KB
testcase_24 AC 4 ms
4,376 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 11 ms
4,376 KB
testcase_27 AC 5 ms
4,376 KB
testcase_28 AC 22 ms
4,380 KB
testcase_29 AC 4 ms
4,376 KB
testcase_30 AC 6 ms
4,376 KB
testcase_31 AC 3 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 14 ms
4,380 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 4 ms
4,380 KB
testcase_36 AC 9 ms
4,380 KB
testcase_37 AC 15 ms
4,380 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
using ll=long long;
typedef pair<int, int> P;
template<int MOD>
struct ModInt{
	int x;
	ModInt(): x(0){}
	ModInt(ll y): x(y>=0 ? y%MOD : (MOD-(-y)%MOD)%MOD){}

	ModInt &operator+=(const ModInt &p){
		if((x+=p.x)>=MOD) x-=MOD;
		return *this;
	}
	ModInt &operator-=(const ModInt &p){
		if((x+=MOD-p.x)>=MOD) x-=MOD;
		return *this;
	}
	ModInt &operator*=(const ModInt &p){
		x=(int)(1ll*x*p.x%MOD);
		return *this;
	}
	ModInt &operator/=(const ModInt &p){
		*this*=p.inv();
		return *this;
	}

	ModInt operator-() const{ return ModInt(-x);}
	ModInt operator+(const ModInt &p) const{ return ModInt(*this)+=p;}
	ModInt operator-(const ModInt &p) const{ return ModInt(*this)-=p;}
	ModInt operator*(const ModInt &p) const{ return ModInt(*this)*=p;}
	ModInt operator/(const ModInt &p) const{ return ModInt(*this)/=p;}
	bool operator==(const ModInt &p) const{ return x==p.x;}
	bool operator!=(const ModInt &p) const{ return x!=p.x;}

	ModInt pow(ll n) const{
		ModInt ret(1), p(x);
		while(n){
			if(n&1) ret*=p;
			p*=p;
			n>>=1;
		}
		return ret;
	}
	ModInt inv() const{
		return pow(MOD-2);
	}
};
const int MOD=1e9+7;
using mint=ModInt<MOD>;
mint dp0[41][41];
mint f[41], invf[41];
int main()
{
    int n, m, k;
    cin>>n>>m>>k;
    f[0]=invf[0]=mint(1);
    for(int i=1; i<=n; i++) f[i]=f[i-1]*mint(i), invf[i]=f[i].inv();
    dp0[0][0]=mint(1);
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            for(int l=1; l<=j; l++){
                dp0[i][j]+=dp0[i-1][j-l]*invf[l];
            }
        }
    }
    auto comb=[&](int x, int y){
        if(!(0<=y && y<=x)) return mint(0);
        mint res=invf[y];
        for(int i=0; i<y; i++){
            res*=mint(x-i);
        }
        return res;
    };
    auto calc=[&](int x, int l){
        mint res(0);
        for(int i=0; i<=l; i++){
            res+=dp0[i][l]*comb(x, i);
        }
        return res;
    };
    int a[41], b[41];
    map<int, vector<int>> mp;
    for(int i=0; i<m; i++) cin>>a[i]>>b[i];
    for(int i=0; i<m; i++){
        mp[a[i]].push_back(b[i]);
    }
    int a1[41];
    int m1=0;
    for(auto p:mp) a1[m1++]=p.first;
    a1[m1]=k+1;
    mint calci[42][42];
    for(int i=0; i<=m1; i++){
        for(int j=0; j<=n; j++){
            if(i) calci[i][j]=calc(a1[i]-a1[i-1], j);
            else calci[i][j]=calc(a1[i], j);
        }
    }
    mint ans(0);
    for(int t=0; t<m; t++){
        int b0=b[t];
        mint dp[41][41][41];
        dp[0][0][0]=mint(1);
        for(int i=0; i<=n; i++) dp[0][i][0]=calci[0][i];
        bool myon=0;
        for(int i=0; i<m1; i++){
            auto v=mp[a1[i]];
            int c=0;
            for(auto x:v){
                if(x>b0) c++;
                else if(x==b0) myon=1;
            }
            for(int j=0; j<=n; j++){
                for(int l=0; l<=m; l++){
                    if(dp[i][j][l].x==0) continue;
                    for(int p=0; p<=n-j; p++){
                        if(myon && l+c-p<0) continue;
                        dp[i+1][j+p][max(0, l+c-p)]+=dp[i][j][l]*calci[i+1][p];
                    }
                }
            }
        }
        for(int i=0; i<=m; i++) ans+=dp[m1][n][i]*f[n]*mint(b0);
    }
    mint s(0);
    for(int i=0; i<m; i++){
        s+=mint(b[i]);
    }
    mint tot=mint(k+1).pow(n);
    ans=s-ans/tot;
    cout<<ans.x<<endl;
    return 0;
}
0