結果

問題 No.214 素数サイコロと合成数サイコロ (3-Medium)
ユーザー HaarHaar
提出日時 2019-10-10 18:24:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 721 ms / 3,000 ms
コード長 7,001 bytes
コンパイル時間 2,432 ms
コンパイル使用メモリ 218,648 KB
実行使用メモリ 5,208 KB
最終ジャッジ日時 2023-08-13 22:51:58
合計ジャッジ時間 5,824 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 721 ms
5,208 KB
testcase_01 AC 711 ms
5,176 KB
testcase_02 AC 721 ms
5,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define LLI long long int
#define FOR(v, a, b) for(LLI v = (a); v < (b); ++v)
#define FORE(v, a, b) for(LLI v = (a); v <= (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define REPE(v, n) FORE(v, 0, n)
#define REV(v, a, b) for(LLI v = (a); v >= (b); --v)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define ITR(it, c) for(auto it = (c).begin(); it != (c).end(); ++it)
#define RITR(it, c) for(auto it = (c).rbegin(); it != (c).rend(); ++it)
#define EXIST(c,x) ((c).find(x) != (c).end())
#define fst first
#define snd second
#define popcount __builtin_popcount
#define UNIQ(v) (v).erase(unique(ALL(v)), (v).end())
#define bit(i) (1LL<<(i))

#ifdef DEBUG
#include <misc/C++/Debug.cpp>
#else
#define dump(...) ((void)0)
#endif

#define gcd __gcd

using namespace std;
template <class T> constexpr T lcm(T m, T n){return m/gcd(m,n)*n;}

template <typename I> void join(ostream &ost, I s, I t, string d=" "){for(auto i=s; i!=t; ++i){if(i!=s)ost<<d; ost<<*i;}ost<<endl;}
template <typename T> istream& operator>>(istream &is, vector<T> &v){for(auto &a : v) is >> a; return is;}

template <typename T, typename U> bool chmin(T &a, const U &b){return (a>b ? a=b, true : false);}
template <typename T, typename U> bool chmax(T &a, const U &b){return (a<b ? a=b, true : false);}
template <typename T, size_t N, typename U> void fill_array(T (&a)[N], const U &v){fill((U*)a, (U*)(a+N), v);}

struct Init{
  Init(){
    cin.tie(0);
    ios::sync_with_stdio(false);
  }
}init;

template <uint32_t M> class ModInt{
public:
  uint64_t val;
  ModInt(): val(0){}
  ModInt(int64_t n){
    if(n >= M) val = n % M;
    else if(n < 0) val = n % M + M;
    else val = n;
  }
  
  inline constexpr ModInt operator+(const ModInt &a) const {return ModInt((val+a.val)%M);}
  inline constexpr ModInt operator-(const ModInt &a) const {return ModInt((val-a.val+M)%M);}
  inline constexpr ModInt operator*(const ModInt &a) const {return ModInt((val*a.val)%M);}
  inline constexpr ModInt operator/(const ModInt &a) const {return ModInt((val*a.inv().val)%M);}
  
  inline constexpr ModInt& operator=(const ModInt &a){val = a.val; return *this;}
  inline constexpr ModInt& operator+=(const ModInt &a){if((val += a.val) >= M) val -= M; return *this;}
  inline constexpr ModInt& operator-=(const ModInt &a){if(val < a.val) val += M; val -= a.val; return *this;}
  inline constexpr ModInt& operator*=(const ModInt &a){(val *= a.val) %= M; return *this;}
  inline constexpr ModInt& operator/=(const ModInt &a){(val *= a.inv().val) %= M; return *this;}

  inline constexpr bool operator==(const ModInt &a) const {return val==a.val;}
  inline constexpr bool operator!=(const ModInt &a) const {return val!=a.val;}

  inline constexpr ModInt& operator++(){*this += 1; return *this;}
  inline constexpr ModInt& operator--(){*this -= 1; return *this;}

  inline constexpr ModInt operator++(int){ModInt t = *this; *this += 1; return t;}
  inline constexpr ModInt operator--(int){ModInt t = *this; *this -= 1; return t;}
  
  inline constexpr static ModInt power(int64_t n, int64_t p){
    ModInt ret = 1, e = n;
    for(; p; e *= e, p >>= 1) if(p&1) ret *= e;
    return ret;
  }

  inline constexpr ModInt power(int64_t p) const {return power(val,p);}
  
  inline constexpr ModInt inv() const {
    int64_t a = val, b = M, u = 1, v = 0;

    while(b){
      int64_t t = a/b;
      a -= t*b; swap(a,b);
      u -= t*v; swap(u,v);
    }
    u %= M;
    if(u < 0) u += M;
    
    return u;
  }
};

template <uint32_t M> ModInt<M> operator-(const ModInt<M> &a){return M-a.val;}

template <uint32_t M> ModInt<M> operator+(int64_t a, const ModInt<M> &b){return ModInt<M>(ModInt<M>(a)+b.val);}
template <uint32_t M> ModInt<M> operator-(int64_t a, const ModInt<M> &b){return ModInt<M>(ModInt<M>(a)-b.val);}
template <uint32_t M> ModInt<M> operator*(int64_t a, const ModInt<M> &b){return ModInt<M>(ModInt<M>(a)*b.val);}
template <uint32_t M> ModInt<M> operator/(int64_t a, const ModInt<M> &b){return ModInt<M>(ModInt<M>(a)/b.val);}

template <uint32_t M> istream& operator>>(istream &is, ModInt<M> &a){is >> a.val; return is;}
template <uint32_t M> ostream& operator<<(ostream &os, const ModInt<M> &a){ os << a.val; return os;}

template <typename T> struct KitamasaAlgorithm{
  int size;
  vector<T> initial_values, coeff;

  KitamasaAlgorithm(int size, const vector<T> &initial_values, const vector<T> &coeff):
    size(size), initial_values(initial_values), coeff(coeff){}

  inline vector<T> inc(const vector<T> &a){
    vector<T> ret(size);

    REP(i,size) ret[i] += a[size-1] * coeff[i];
    FOR(i,1,size) ret[i] += a[i-1]; 

    return ret;
  }
  
  inline vector<T> dbl(const vector<T> &a){
    vector<T> ret(size), b(a);
    
    REP(j,size){
      REP(i,size){
        ret[i] += a[j] * b[i];
      }

      b = inc(b);
    }
    
    return ret;
  }
  
  inline T at(int64_t index){
    if(index < size) return initial_values[index];
    return calc(get(index));
  }

  inline T calc(const vector<T> &v){
    T ret = 0;
    REP(i,size) ret += v[i] * initial_values[i];

    return ret;
  }

  inline vector<T> get(int64_t index){
    vector<T> ret(size);
    ret[0] = 1;

    for(int i = 63; i >= 0; --i){
      ret = dbl(ret);

      //dump(i);

      if(index & (1LL << i)){
        ret = inc(ret);
      }
    }
    
    return ret;
  }
};





using mint = ModInt<1000000007>;




constexpr int prime_dice[6] = {2,3,5,7,11,13};
constexpr int composite_dice[6] = {4,6,8,9,10,12};

constexpr int MAX = 1300;

int main(){
  LLI N,P,C;

  while(cin >> N >> P >> C){
    vector<vector<mint>> dp(P+C+1, vector<mint>(MAX));

    dp[0][0] = 1;

    for(auto x : composite_dice){
      vector<vector<mint>> temp(P+C+1, vector<mint>(MAX));

      REPE(i,C){
        REP(j,MAX){
          FORE(k,0,C){
            if(i+k <= C and j+x*k < MAX){
              temp[i+k][j+x*k] += dp[i][j];
            }
          }
        }
      }

      swap(dp, temp);
    }

    for(auto x : prime_dice){
      vector<vector<mint>> temp(P+C+1, vector<mint>(MAX));

      FORE(i,C,C+P){
        REP(j,MAX){
          FORE(k,0,P){
            if(i+k <= C+P and j+x*k < MAX){
              temp[i+k][j+x*k] += dp[i][j];
            }
          }
        }
      }

      swap(dp, temp);
    }


    vector<mint> a(MAX-1);
    vector<mint> b(dp[C+P]);
    
    a[0] = 1;

    REP(i,MAX-1){
      FOR(j,1,MAX){
        if(i-j >= 0) a[i] += a[i-j] * dp[C+P][j];
      }
    }

    reverse(ALL(b));
    b.pop_back();
    
    KitamasaAlgorithm<mint> ka(MAX-1, a, b);
    
    map<LLI,mint> dp2;

    {
      LLI i = max<LLI>(0,N-MAX);

      auto t = ka.get(i);

      
      for(; i <= N; ++i){
        dp2[i] = ka.calc(t);
        
        t = ka.inc(t);
      }


      FORE(i,N+1,N+MAX){
        FOR(j,1,MAX){
          if(i-j >= 0 and i-j < N) dp2[i] += dp[C+P][j] * dp2[i-j];
        }
      }
    }

    
    
    mint ans = 0;

    FOR(i,N,N+MAX) ans += dp2[i];

    cout << ans << endl;
  }

  return 0;
}
0