結果

問題 No.619 CardShuffle
ユーザー DAyamaCTFDAyamaCTF
提出日時 2018-06-11 19:02:11
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,088 bytes
コンパイル時間 2,020 ms
コンパイル使用メモリ 183,252 KB
実行使用メモリ 87,660 KB
最終ジャッジ日時 2023-09-13 03:15:43
合計ジャッジ時間 60,411 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 AC 2,644 ms
87,484 KB
testcase_27 AC 2,633 ms
87,324 KB
testcase_28 AC 2,619 ms
87,268 KB
testcase_29 AC 2,629 ms
87,424 KB
testcase_30 AC 2,630 ms
87,276 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 TLE -
testcase_33 TLE -
testcase_34 AC 2,393 ms
87,364 KB
testcase_35 AC 555 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007LL

typedef vector<ll> vec;
typedef vector<vec> mat;

mat operator*(const mat& a,const mat& b){
  mat res(4,vec(4,0));
  const ll modl=8*mod*mod;
  rep(i,4){
    rep(k,4){
      rep(j,4){
        res[i][j]+=a[i][k]*b[k][j];
        if(res[i][j]>=modl)res[i][j]-=modl;
      }
    }
    rep(j,4)res[i][j]%=mod;
  }
  return res;
}

vector<mat> dat;
int sz;
mat e;

void init(int n_){
  sz=1;
  while(sz<n_) sz*=2;
  dat.resize(2*sz-1);
  for(int i=0;i<2*sz-1;i++){
    dat[i]=mat(4,vec(4,0));
    rep(j,4)dat[i][j][j]=1;
  }
  e=mat(4,vec(4,0));
  rep(i,4)e[i][i]=1;
}

void update(int k,mat a){
  k+=sz-1;
  dat[k]=a;
  while(k>0){
    k=(k-1)/2;
    dat[k]=dat[k*2+2]*dat[k*2+1];
  }
}

mat query(int a,int b,int k=0,int l=0,int r=sz){
  if(r<=a||b<=l)return e;
  if(a<=l&&r<=b)return dat[k];
  else{
    return query(a,b,k*2+2,(l+r)/2,r)*query(a,b,k*2+1,l,(l+r)/2);
  }
}


int n,q;
vector<char> cs;

ll pls[4][4]={
  {1, 0, 1, 0},
  {0, 0, 0, 1},
  {0, 0, 0, 0},
  {0, 0, 0, 1},
};

ll mul[4][4]={
  {1, 0, 0, 0},
  {0, 0, 1, 0},
  {0, 0, 0, 0},
  {0, 0, 0, 1},
};

ll num[4][4]={
  {1, 0, 0, 0},
  {0, 1, 0, 0},
  {0, 0, 10, 0},
  {0, 0, 0, 1},
};

int main(){
  scanf("%d",&n);
  init(n);
  rep(i,n){
    char c;
    cin>>c;
    mat m(4,vec(4,0));
    cs.push_back(c);
    if(isdigit(cs[i])){
      rep(j,4)rep(k,4)m[j][k]=num[j][k];
      m[2][1]=cs[i]-'0';
    }else if(cs[i]=='+'){
      rep(j,4)rep(k,4)m[j][k]=pls[j][k];
    }else if(cs[i]=='*'){
      rep(j,4)rep(k,4)m[j][k]=mul[j][k];
    }
    update(i,m);
  }
  mat mpls(4,vec(4,0));
  rep(i,4)rep(j,4)mpls[i][j]=pls[i][j];

  scanf("%d",&q);
  while(q--){
    char typ;
    cin>>typ;
    if(typ=='?'){
      int l,r;
      scanf("%d%d",&l,&r);
      l--;
      mat m=query(l,r);
      m=mpls*m;
      cout<<(m[0][1]+m[0][3])%mod<<endl;
    }else{
      int a,b;
      scanf("%d%d",&a,&b);
      a--;b--;
      mat m1(4,vec(4)),m2(4,vec(4));
      if(isdigit(cs[a])){
        rep(j,4)rep(k,4)m1[j][k]=num[j][k];
        m1[2][1]=cs[a]-'0';
      }else if(cs[a]=='+'){
        rep(j,4)rep(k,4)m1[j][k]=pls[j][k];
      }else if(cs[a]=='*'){
        rep(j,4)rep(k,4)m1[j][k]=mul[j][k];
      }

      if(isdigit(cs[b])){
        rep(j,4)rep(k,4)m2[j][k]=num[j][k];
        m2[2][1]=cs[b]-'0';
      }else if(cs[b]=='+'){
        rep(j,4)rep(k,4)m2[j][k]=pls[j][k];
      }else if(cs[b]=='*'){
        rep(j,4)rep(k,4)m2[j][k]=mul[j][k];
      }
      swap(cs[a],cs[b]);
      update(a,m2); update(b,m1);
    }
  }
  return 0;
}
0