結果

問題 No.1097 Remainder Operation
ユーザー cureskolcureskol
提出日時 2022-11-12 15:51:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 212 ms / 2,000 ms
コード長 1,695 bytes
コンパイル時間 2,474 ms
コンパイル使用メモリ 206,188 KB
実行使用メモリ 67,252 KB
最終ジャッジ日時 2023-10-12 07:01:29
合計ジャッジ時間 6,279 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 12 ms
9,636 KB
testcase_08 AC 12 ms
9,648 KB
testcase_09 AC 11 ms
9,588 KB
testcase_10 AC 12 ms
9,580 KB
testcase_11 AC 12 ms
9,672 KB
testcase_12 AC 129 ms
67,192 KB
testcase_13 AC 150 ms
67,136 KB
testcase_14 AC 159 ms
67,180 KB
testcase_15 AC 166 ms
67,252 KB
testcase_16 AC 139 ms
67,180 KB
testcase_17 AC 102 ms
67,140 KB
testcase_18 AC 94 ms
67,240 KB
testcase_19 AC 119 ms
67,196 KB
testcase_20 AC 118 ms
67,140 KB
testcase_21 AC 212 ms
67,180 KB
testcase_22 AC 204 ms
67,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<typename Monoid,int LOG>
class Doubling{
  using X=typename Monoid::value_type;
  int n;
  bool is_prepared;
  
  using P=pair<int,X>;
  static constexpr P unit={-1,Monoid::unit()};
  vector<vector<P>> DP;

  P k_move(const P&a,int k){
    if(a.first==-1)return a;
    const auto [now,val]=a;
    const auto [nxt,cost]=DP[k][now];
    return {nxt,Monoid::op(val,cost)};
  }

  void build(){
    is_prepared=true;
    for(int k=0;k<LOG-1;k++)
      for(int v=0;v<n;v++)
        DP[k+1][v]=k_move(DP[k][v],k);
  }
public:
  Doubling(int n):n(n),is_prepared(false){
    DP.assign(LOG, vector<P>(n,unit));
  }

  void add(int from,int to,X x){
    assert(!is_prepared);
    assert(-1<=to and to<n);
    DP[0][from]={to,x};
  }

  // [終点,値]
  P calc(int s,long long step){
    assert(step<=(1LL<<LOG));
    if(!is_prepared)build();

    P res{s,Monoid::unit()};
    for(int k=0;step;k++,step>>=1)
      if(step&1)res=k_move(res,k);
    return res;
  }
};

template<typename X>
struct Group_Add {
  using value_type = X;
  static constexpr X op(const X &x, const X &y) noexcept { return x + y; }
  static constexpr X inverse(const X &x) noexcept { return -x; }
  static constexpr X power(const X &x, long long n) noexcept { return X(n) * x; }
  static constexpr X unit() { return X(0); }
  static constexpr bool commute = true;
};
using GA=Group_Add<long long>;

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  
  int n;cin>>n;
  Doubling<GA,40> db(n);
  for(int i=0;i<n;i++){
    int a;cin>>a;
    db.add(i,(i+a)%n,a);
  }
  int q;cin>>q;
  while(q--){
    long long k;cin>>k;
    cout<< db.calc(0,k).second <<"\n";
  }
}
0