結果

問題 No.498 ワープクリスタル (給料日編)
ユーザー mamekinmamekin
提出日時 2017-04-01 19:32:39
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 2,739 bytes
コンパイル時間 1,094 ms
コンパイル使用メモリ 107,256 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-22 02:09:46
合計ジャッジ時間 2,727 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 37 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 36 ms
4,376 KB
testcase_07 AC 37 ms
4,376 KB
testcase_08 AC 36 ms
4,380 KB
testcase_09 AC 36 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 6 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 37 ms
4,376 KB
testcase_19 AC 37 ms
4,376 KB
testcase_20 AC 36 ms
4,380 KB
testcase_21 AC 36 ms
4,376 KB
testcase_22 AC 36 ms
4,376 KB
testcase_23 AC 36 ms
4,380 KB
testcase_24 AC 36 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

// a,b の最大公約数と、ax + by = gcd(a,b) となる x,y を求める
long long extgcd(long long a, long long b, long long &x, long long &y) {
    long long g = a;
    if(b != 0){
        g = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    }else{
        x = 1;
        y = 0;
    }
    return g;
}

// ax ≡ gcd(a, m) (mod m) となる x を求める
// a, m が互いに素ならば、関数値は mod m での a の逆数となる
long long mod_inverse(long long a, long long m)
{
    long long x, y;
    extgcd(a, m, x, y);
    return (x % m + m) % m;
}

// 階乗数の事前計算を用いた各種組合せ
class FactorialCalculation
{
private:
    const int mod;
    vector<long long> factorial;
    vector<long long> invFactorial;
public:
    FactorialCalculation(int n, int mod) : mod(mod)
    {
        factorial.resize(n+1, 1);
        invFactorial.resize(n+1, 1);
        for(int i=1; i<=n; ++i){
            factorial[i] = factorial[i-1] * i % mod;
            invFactorial[i] = mod_inverse(factorial[i], mod);
        }
    }
    long long getFactorial(int n){
        return factorial[n];
    }
    long long getPermutation(int n, int r){
        if(n < r)
            return 0;
        return factorial[n] * invFactorial[n-r] % mod;
    }
    long long getCombination(int n, int r){
        if(n < r)
            return 0;
        return getPermutation(n, r) * invFactorial[r] % mod;
    }
    long long getHomogeneous(int n, int r){
        return getCombination(n+r-1, r);
    }
};

const int MOD = 1000000007;
int w, h, n;
vector<int> dx, dy, m;
FactorialCalculation calc(75, MOD);

long long solve(int i, int y, int x, int cnt, long long tmp)
{
    if(i == n){
        if(y == h && x == w)
            return tmp;
        else
            return 0;
    }

    long long ans = 0;
    for(int j=0; j<=m[i]; ++j){
        int y2 = y + dy[i] * j;
        int x2 = x + dx[i] * j;
        long long tmp2 = tmp * calc.getCombination(cnt+j, j) % MOD;
        ans += solve(i+1, y2, x2, cnt + j, tmp2);
        ans %= MOD;
    }
    return ans;
}

int main()
{
    cin >> w >> h >> n;
    dx.resize(n);
    dy.resize(n);
    m.resize(n);
    for(int i=0; i<n; ++i)
        cin >> dx[i] >> dy[i] >> m[i];

    cout << solve(0, 0, 0, 0, 1) << endl;

    return 0;
}
0