結果

問題 No.1106 🦉 何事もバランスが大事
ユーザー beet
提出日時 2020-07-03 22:39:47
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 1,612 bytes
コンパイル時間 4,116 ms
コンパイル使用メモリ 194,512 KB
最終ジャッジ日時 2025-01-11 14:56:29
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 77
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
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;}
using Int = long long;
const char newl = '\n';


template<typename F>
struct FixPoint : F{
  FixPoint(F&& f):F(forward<F>(f)){}
  template<typename... Args>
  decltype(auto) operator()(Args&&... args) const{
    return F::operator()(*this,forward<Args>(args)...);
  }
};
template<typename F>
inline decltype(auto) MFP(F&& f){
  return FixPoint<F>{forward<F>(f)};
}

//INSERT ABOVE HERE
const Int LOG = 40;
const Int OFS = 100;

// 0 same
// 1 less
// 2 greater
Int dp[3][2][LOG][OFS*2+1];

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n;
  cin>>n;
  n++;

  vector<Int> po(LOG);
  for(Int i=0;i<LOG;i++){
    po[i]=n%5;
    n/=5;
  }

  auto nx=[&](Int a,Int b,Int c)->Int{
    if(a==0 and b==c) return 0;
    if(b<c) return 1;
    if(b>c) return 2;
    assert(b==c);
    return a;
  };

  memset(dp,0,sizeof(dp));
  dp[0][0][0][OFS]=1;

  Int diff[5]={0,-1,-2,2,1};
  for(Int i=0;i+1<LOG;i++){
    for(Int s=-OFS;s<OFS;s++){
      for(Int a=0;a<3;a++){
        for(Int c=0;c<2;c++){
          if(dp[a][c][i][OFS+s]==0) continue;
          for(Int k=0;k<5;k++){
            dp[nx(a,k,po[i])][(k+c)>=3][i+1][OFS+s+diff[(k+c)%5]]+=dp[a][c][i][OFS+s];
            chmin(dp[nx(a,k,po[i])][(k+c)>=3][i+1][OFS+s+diff[(k+c)%5]],((Int)1e18)+10);
          }
        }
      }
    }
  }

  Int ans=0;
  ans+=dp[1][0][LOG-1][OFS];
  ans+=dp[1][1][LOG-1][OFS];
  ans--;
  cout<<ans<<newl;
  return 0;
}
0