結果

問題 No.1595 The Final Digit
ユーザー A0ikun_1818A0ikun_1818
提出日時 2021-07-13 14:25:17
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 3,378 bytes
コンパイル時間 3,422 ms
コンパイル使用メモリ 96,980 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-15 09:21:28
合計ジャッジ時間 2,797 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
using namespace std;
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <set>
#include <map>
#include <queue>
#include <cassert>
using ll = long long;
using ld = long double;
const ll MOD1 = 1000000007;
const ll MOD2 = 998244353;
const ll LIMIT1 = 200010;
const ll LIMIT2 = 500010;
const ll LIMIT3 = 1000010;
const int INF = ((1<<30)-1);
const ll LLINF = (1LL<<60);
const ld EPS = (1e-14);
using cv = const void;
using P = pair<ll,ll>;
#define rep(i,n) for((i)=0;(i)<(n);(i)++)
#define per(i,n) for((i)=(n)-1;(i)>=0;(i)--)
#define ALL(a) (a).begin(),(a).end()
#define ALL2(a, n) (a),(a)+(n)
template<class T,class C> T max(T a,C b){ return std::max(a, (T)b); }
template<class T,class C> T min(T a,C b){ return std::min(a, (T)b); }
template<class T,class C> bool chmax(T &a,const C b){ if(a<(T)b){ a=(T)b;return true; } return false; }
template<class T,class C> bool chmin(T &a,const C b){ if((T)b<a){ a=(T)b;return true; } return false; }
#define zt(a,b) (max((a),(b))-min((a),(b)))
#define setpre(n) fixed << setprecision(n)
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return (a/gcd(a,b))*b;}
#define YES(b) (b?"YES":"NO")
#define Yes(b) (b?"Yes":"No")
#define yes(b) (b?"yes":"no")
template<class T,class C> char gl(const T a,const C b){
  T bb = (T)b;
  if(a<bb) return '<';
  else if(a>bb) return '>';
  else return '=';
}
int dx[8]={1,0,-1,0,1,-1,-1,1},dy[8]={0,1,0,-1,1,1,-1,-1};
ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1){
      res *= a; if(mod>=1) res %= mod;
    }
    a *= a; if(mod>=1) a %= mod;
    n >>= 1;
  }
  return res;
}
void initialize(){
  
}
struct Doubling{//ダブリング計算(0-indexed)
  int n;//頂点数
  const ll logMaxCnt = 63;
  const ll MAX_CNT = (1LL<<60)-1;
  bool alreadyCalc = false;
  vector<vector<P>> d;//移動先,コスト
  Doubling(int n_) : d(logMaxCnt, vector<P>(n_)){
    n = n_;
  }
  void update_edge(int from, ll to, ll cost){//辺の設定
    if(from<0 || from>=n || to<0 || to>=n){
      cerr << "update_edge error: out of vertex" << '\n';
      return;
    }
    
    d[0][from].first = (ll)to;
    d[0][from].second = cost;
  }
  void calculate(){//ダブリング構築
    int i,k;
    for(k=0;k<logMaxCnt-1;k++){
      for(i=0;i<n;i++){
        d[k+1][i].second = d[k][i].second;
        d[k+1][i].second += d[k][d[k][i].first].second;
        
        d[k+1][i].first = d[k][d[k][i].first].first;
      }
    }
    alreadyCalc = true;
  }
  P query(ll from, ll cnt){//クエリの処理
    if(from<0 || from>=n || cnt<0) return P{-1,0};
    else if(cnt==0) return P{from,0};
    
    if(!alreadyCalc) calculate();//前計算がまだ
    
    ll to = from, cost = 0, k;
    for(k=0;cnt>0;k++){
      if(cnt & 1){
        cost += d[k][to].second;
        to = d[k][to].first;
      }
      cnt = cnt >> 1;
    }
    return P{to, cost};
  }
};
int main(void){
  initialize();
  ll n,m,i,j,k,result=0;
  string s;
  ll p,q,r;
  
  cin >> p >> q >> r >> k;
  p%=10;q%=10;r%=10;
  Doubling db(1000);
  for(i=0;i<1000;i++){
    int a = i/100;
    int b = i/10 - a*10;
    int c = i%10;
    db.update_edge(i, b*100+c*10+(a+b+c)%10, 1);
  }
  result = db.query(p*100+q*10+r, k-3).first;
  result %= 10;
  cout << result << endl;
  return 0;
}
0