結果

問題 No.936 Are
ユーザー beetbeet
提出日時 2019-11-29 23:10:52
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 414 ms / 3,000 ms
コード長 4,497 bytes
コンパイル時間 5,483 ms
コンパイル使用メモリ 204,544 KB
実行使用メモリ 52,484 KB
最終ジャッジ日時 2023-08-13 07:35:52
合計ジャッジ時間 7,712 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
52,164 KB
testcase_01 AC 413 ms
52,292 KB
testcase_02 AC 412 ms
52,484 KB
testcase_03 AC 16 ms
52,152 KB
testcase_04 AC 18 ms
52,340 KB
testcase_05 AC 16 ms
52,232 KB
testcase_06 AC 17 ms
52,336 KB
testcase_07 AC 16 ms
52,176 KB
testcase_08 AC 16 ms
52,188 KB
testcase_09 AC 17 ms
52,184 KB
testcase_10 AC 17 ms
52,196 KB
testcase_11 AC 17 ms
52,160 KB
testcase_12 AC 15 ms
52,184 KB
testcase_13 AC 123 ms
52,224 KB
testcase_14 AC 337 ms
52,184 KB
testcase_15 AC 142 ms
52,152 KB
testcase_16 AC 248 ms
52,168 KB
testcase_17 AC 222 ms
52,180 KB
testcase_18 AC 99 ms
52,236 KB
testcase_19 AC 390 ms
52,196 KB
testcase_20 AC 392 ms
52,396 KB
testcase_21 AC 360 ms
52,472 KB
testcase_22 AC 218 ms
52,236 KB
testcase_23 AC 414 ms
52,156 KB
testcase_24 AC 413 ms
52,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


template<typename T,T MOD = 1000000007>
struct Mint{
  static constexpr T mod = MOD;
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}

  Mint pow(long long k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1) res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }

  static Mint add_identity(){return Mint(0);}
  static Mint mul_identity(){return Mint(1);}

  Mint inv(){return pow(MOD-2);}

  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}

  Mint operator+(Mint a) const{return Mint(v)+=a;}
  Mint operator-(Mint a) const{return Mint(v)-=a;}
  Mint operator*(Mint a) const{return Mint(v)*=a;}
  Mint operator/(Mint a) const{return Mint(v)/=a;}

  Mint operator-() const{return v?Mint(MOD-v):Mint(v);}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}
  bool operator <(const Mint a)const{return v <a.v;}

  static Mint comb(long long n,int k){
    Mint num(1),dom(1);
    for(int i=0;i<k;i++){
      num*=Mint(n-i);
      dom*=Mint(i+1);
    }
    return num/dom;
  }
};
template<typename T,T MOD> constexpr T Mint<T, MOD>::mod;
template<typename T,T MOD>
ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;}

//INSERT ABOVE HERE
using M = Mint<int>;
const int MAX = 20002;
M dp[5*5*5*5][MAX]={};
int win[5*5*5*5]={};

int calc(int a,int b,int c,int d){
  return (((a*5+b)*5+c)*5+d);
}

using P = pair<int, int>;
vector<P> vp[5*5];
void init(){
  for(int j=0;j<5*5*5*5;j++){
    int a=(j/125)%5;
    int b=(j/25)%5;
    int c=(j/5)%5;
    int d=(j/1)%5;

    if(a==0&&b==0) continue;
    if(c==0&&d==0) continue;

    for(int k=0;k<4;k++){
      int nc=c,nd=d;
      int add=(~k&1?a:b);
      if(add==0||(k/2?nc:nd)==0) continue;
      (k/2?nc:nd)+=add;
      (k/2?nc:nd)%=5;
      if(nc==0&&nd==0) win[j]=1;
    }
  }

  for(int j=0;j<5*5*5*5;j++){
    int a=(j/125)%5;
    int b=(j/25)%5;
    int c=(j/5)%5;
    int d=(j/1)%5;

    if(a==0&&b==0) continue;
    if(c==0&&d==0) continue;

    if((a+b)!=(c+d)&&(a+b)%5!=c+d) continue;
    if(P(a,b)==P(c,d)) continue;
    if(P(a,b)==P(d,c)) continue;

    vp[a*5+b].emplace_back(c,d);
    //cout<<a<<" "<<b<<":"<<c<<" "<<d<<endl;
  }
}


signed main(){
  init();

  int n,k;
  cin>>n>>k;
  int l1,r1,l2,r2;
  cin>>l1>>r1>>l2>>r2;

  dp[calc(l1,r1,l2,r2)][0]=1;

  M ans{0};
  for(int i=0;i<k;i++){
    for(int j=0;j<5*5*5*5;j++){
      int a=(j/125)%5;
      int b=(j/25)%5;
      int c=(j/5)%5;
      int d=(j/1)%5;
      //if(dp[j][i].v) cout<<a<<" "<<b<<" "<<c<<" "<<d<<":"<<dp[j][i]<<endl;

      if(a==0&&b==0) continue;
      if(c==0&&d==0) continue;

      if(~(i^n)&1){
        // First
        for(int k=0;k<4;k++){
          int nc=c,nd=d;
          int add=(~k&1?a:b);
          if(add==0||(k/2?nc:nd)==0) continue;
          (k/2?nc:nd)+=add;
          (k/2?nc:nd)%=5;
          dp[calc(a,b,nc,nd)][i+1]+=dp[j][i];
          if(nc==0&&nd==0) ans+=dp[j][i];
        }
        for(auto p:vp[a*5+b]){
          int na=p.first,nb=p.second;
          dp[calc(na,nb,c,d)][i+1]+=dp[j][i];
        }
      }else{
        // Second

        // Win
        if(win[calc(c,d,a,b)]) continue;

        int cnt=0;
        for(int k=0;k<4;k++){
          int na=a,nb=b;
          int add=(~k&1?c:d);
          if(add==0||(k/2?na:nb)==0) continue;
          (k/2?na:nb)+=add;
          (k/2?na:nb)%=5;
          if(!win[calc(na,nb,c,d)]) cnt++;
        }

        for(auto p:vp[c*5+d]){
          int nc=p.first,nd=p.second;
          if(!win[calc(a,b,nc,nd)]) cnt++;
        }

        for(int k=0;k<4;k++){
          int na=a,nb=b;
          int add=(~k&1?c:d);
          if(add==0||(k/2?na:nb)==0) continue;
          (k/2?na:nb)+=add;
          (k/2?na:nb)%=5;
          if(cnt&&win[calc(na,nb,c,d)]) continue;
          dp[calc(na,nb,c,d)][i+1]+=dp[j][i];
        }

        for(auto p:vp[c*5+d]){
          int nc=p.first,nd=p.second;
          if(cnt&&win[calc(a,b,nc,nd)]) continue;
          dp[calc(a,b,nc,nd)][i+1]+=dp[j][i];
        }
      }
    }
  }
  cout<<ans<<endl;
  return 0;
}
0