結果

問題 No.1595 The Final Digit
ユーザー itsuteiitsutei
提出日時 2021-08-11 22:19:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,648 bytes
コンパイル時間 2,324 ms
コンパイル使用メモリ 213,268 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-26 00:16:14
合計ジャッジ時間 3,760 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;

template<typename T>struct Kitamasa{
  vector<T>b,c;
  int k;
  Kitamasa(vector<T>b,vector<T>c,int k):b(b),c(c),k(k){};
  
  void add(T &a,const T &b){
    a+=b;
    a%=10;
  }
  T prod(const T &a,const T &b){
    return (a*b)%10;
  }
  
  vector<T> next_v(const vector<T>&v){
    vector<T> ret(k);
    for(int i=0; i<k; i++){
      ret[i] = prod(v[k-1],c[i]);
      if(i) add(ret[i],v[i-1]);
    }
    return ret;
  }
  vector<T> double_v(const vector<T>&v){
    vector<T> ret(k);
    vector a(k,vector<T>(k));
    a[0] = v;
    for(int j=1; j<k; j++){
      a[j] = next_v(a[j-1]);
    }
    ret = vector<T>(k);
    for(int i=0; i<k; i++){
      for(int j=0; j<k; j++){
        add( ret[i], prod(a[0][j],a[j][i]) );
      }
    }
    return ret;
  }
  
  //n:0-indexed
  T calc(ll n){
    if(n<k) return b[n];
    ll m = n;
    vector<ll> v{n};
    while(m>k){
      if(m%2||m/2<=k) m--;
      else m/=2;
      
      v.push_back(m);
    }
    reverse(v.begin(),v.end());
    
    vector dp(2,vector<T>(k,0)); 
    int cur = 1,pre = 0;
    dp[pre] = c;
    
    m = v.size();
    for(int l=1; l<m; l++){
      if(v[l]-v[l-1]==1){      
        dp[cur] = next_v(dp[pre]);
      }
      else{
        dp[cur] = double_v(dp[pre]);
      }
      swap(pre,cur);
    }
    T ans{};
    for(int i=0; i<k; i++){
      add(ans, prod(dp[pre][i],b[i]) );
    }
    return ans;
  }
};

int main(){
  
  vector<int> b(3);
  ll k;
  for(auto &i:b){
    cin>>i;
    i %= 10;
  }
  cin>>k;
  vector<int> c(3,1);
  Kitamasa kita(b,c,3);
  cout<<kita.calc(k-1)<<endl;
  
}
0