結果

問題 No.391 CODING WAR
ユーザー confconf
提出日時 2016-07-09 14:03:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 135 ms / 2,000 ms
コード長 1,772 bytes
コンパイル時間 566 ms
コンパイル使用メモリ 65,784 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 10:42:52
合計ジャッジ時間 2,253 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,248 KB
testcase_01 AC 4 ms
5,376 KB
testcase_02 AC 4 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 135 ms
5,376 KB
testcase_10 AC 130 ms
5,376 KB
testcase_11 AC 96 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 125 ms
5,376 KB
testcase_14 AC 100 ms
5,376 KB
testcase_15 AC 117 ms
5,376 KB
testcase_16 AC 72 ms
5,376 KB
testcase_17 AC 85 ms
5,376 KB
testcase_18 AC 58 ms
5,376 KB
testcase_19 AC 58 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;

int p=1000000007;

int extgcd(int a, int b, int&x, int&y){
    int d=a;
    if(b!=0){
        d=extgcd(b,a%b,y,x);
        y-=(a/b)*x;
    }else{
        x=1;y=0;
    }
    return d;
}

int mod_pow(long long int a, long long int e, int p){
    long long int res = 1;
    for(;e>0;e>>=1){
        if(e&1)res=(res*a)%p;
        a=(a*a)%p;
    }
    return (int)res;
}

int mod_inverse(int a, int m){
    int x, y;
    extgcd(a,m,x,y);
    return (m+x%m)%m;
}

int fact(int n, int p){
    static int F[100001];
    if(F[n]) return F[n];
    else{
        F[0]=1;
        for(long long int i=1;i<=100000;i++){
            F[i]=i*F[i-1]%p;
        }
        return F[n];
    }

    long long int res=1;
    while(n){
        res*=n;
        if(res>p)res%=p;
        if(res==0)return 0;
        n--;
    }
    return (int)res;
}

int mod_fact(int n,int p,int &e){
    static int F[100001];
    if(F[n]) return F[n];
    e=0;
    if(n==0)return 1;
    long long int res=mod_fact(n/p,p,e);
    e+=n/p;
    if(n/p%2!=0) return F[n]=res*(p-fact(n%p,p))%p;
    return F[n]=res*fact(n%p,p)%p;
}

int mod_comb(int n, int k,int p){
    if(n<0||k<0||n<k)return 0;
    int e1,e2,e3;
    long long int a1=mod_fact(n,p,e1),a2=mod_fact(k,p,e2),a3=mod_fact(n-k,p,e3);
    //if(e1>e2+e3)return 0;
    //return (a1*mod_inverse(a2*a3%p,p))%p;
    return a1*mod_pow(a2*a3%p,p-2,p)%p;
}

int main(){
    long long int N,M;
    cin>>N>>M;
    long long int ans=0;
    for(int i=1;i<=M;i++){
        //cout<<i<<endl;
        if((M-i)%2){
            ans=(p+ans-((long long int)mod_comb(M,i,p)*mod_pow(i,N,p))%p)%p;
        }else{
            ans=(ans+((long long int)mod_comb(M,i,p)*mod_pow(i,N,p))%p)%p;
        }
    }
    cout<<ans<<endl;
    return 0;
}
0