結果

問題 No.802 だいたい等差数列
ユーザー tarattata1tarattata1
提出日時 2019-09-28 08:00:06
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 221 ms / 2,000 ms
コード長 2,102 bytes
コンパイル時間 790 ms
コンパイル使用メモリ 73,180 KB
実行使用メモリ 32,512 KB
最終ジャッジ日時 2024-04-08 17:32:36
合計ジャッジ時間 7,961 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 204 ms
24,760 KB
testcase_01 AC 205 ms
24,760 KB
testcase_02 AC 205 ms
24,760 KB
testcase_03 AC 9 ms
24,632 KB
testcase_04 AC 203 ms
24,760 KB
testcase_05 AC 9 ms
24,632 KB
testcase_06 AC 203 ms
24,760 KB
testcase_07 AC 203 ms
24,760 KB
testcase_08 AC 203 ms
24,760 KB
testcase_09 AC 203 ms
24,760 KB
testcase_10 AC 216 ms
31,776 KB
testcase_11 AC 217 ms
32,372 KB
testcase_12 AC 209 ms
27,072 KB
testcase_13 AC 217 ms
32,488 KB
testcase_14 AC 211 ms
29,412 KB
testcase_15 AC 209 ms
27,612 KB
testcase_16 AC 211 ms
29,748 KB
testcase_17 AC 10 ms
24,632 KB
testcase_18 AC 10 ms
24,632 KB
testcase_19 AC 209 ms
27,824 KB
testcase_20 AC 217 ms
30,168 KB
testcase_21 AC 221 ms
32,512 KB
testcase_22 AC 215 ms
32,512 KB
testcase_23 AC 208 ms
25,528 KB
testcase_24 AC 10 ms
24,632 KB
testcase_25 AC 204 ms
24,760 KB
testcase_26 AC 207 ms
24,760 KB
testcase_27 AC 204 ms
24,760 KB
testcase_28 AC 210 ms
27,064 KB
testcase_29 AC 220 ms
32,512 KB
testcase_30 AC 204 ms
24,760 KB
testcase_31 AC 202 ms
24,760 KB
testcase_32 AC 10 ms
24,632 KB
testcase_33 AC 209 ms
27,904 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main(int, char**)’:
main.cpp:72:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   72 |     scanf("%d%d%d%d", &N, &M, &D1, &D2);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996) 
 
typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
using namespace std;


const int max_comb=1400000;
vector<ll> fac(max_comb+1); //n! (mod M)
vector<ll> ifac(max_comb+1); //k!^(-1) (mod M)

ll mpow(ll x, ll n){ //x^n(mod M)
    ll ans = 1;
    while(n != 0){
        if(n&1) ans = ans*x % MOD;
        x = x*x % MOD;
        n = n >> 1;
    }
    return ans;
}

ll minv(ll x){
    return mpow( x, MOD-2 );
}

ll comb(int a, int b){     // C(a,b) = a! * b!^(-1) * (a-b)^(-1)
    if(a == 0 && b == 0)return 1;
    if(a < b || a < 0)return 0;
    ll tmp = ifac[a-b]* ifac[b] % MOD;
    return tmp * fac[a] % MOD;
}

ll perm(int a, int b){     // P(a,b) = a! * (a-b)!^(-1)
    if(b == 0)return 1;
    if(a < b || a < 0)return 0;
    ll tmp = ifac[a-b] % MOD;
    return tmp * fac[a] % MOD;
}

void pre_comb()
{
    fac[0] = 1;
    ifac[0] = 1;
    for(int i = 0; i<max_comb; i++){
        fac[i+1] = fac[i]*(i+1) % MOD; // n!(mod M)
        ifac[i+1] = ifac[i]*minv(i+1) % MOD; // k!^(-1) (mod M)
    }
    return;
}

int main(int argc, char* argv[])
{
    int N,M,D1,D2;
    scanf("%d%d%d%d", &N, &M, &D1, &D2);

    int d = D2 - D1;
    if(M - (ll)D1*(N-1)-1<0) {
        printf("0\n"); return 0;
    }
    int m = M - (ll)D1*(N-1)-1;
    pre_comb();

    vector<ll> a(m+1);
    int i;
    for(i=0; i<=m; i++) {
        a[i]=comb(i+N-1, i);
    }
    for(i=1; i<=m; i++) {
        a[i]=(a[i]+a[i-1])%MOD;
    }

    ll ans=0;
    for(i=0; i<N; i++) {
        int tmp=m-i*(d+1);
        if(tmp<0) break;
        ll ans0 = (a[tmp] * (i%2==0? 1: MOD-1))%MOD;
        ans0 = ans0 * comb(N-1, i) %MOD;
        ans = (ans + ans0) %MOD;
    }
    printf("%lld\n", ans);

    return 0;
}
0