結果

問題 No.1404 誕生日プレゼント
ユーザー 👑 NachiaNachia
提出日時 2021-02-20 00:40:20
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,131 bytes
コンパイル時間 2,135 ms
コンパイル使用メモリ 208,920 KB
実行使用メモリ 8,940 KB
最終ジャッジ日時 2023-10-17 04:08:30
合計ジャッジ時間 29,242 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 29 ms
6,076 KB
testcase_24 AC 28 ms
6,076 KB
testcase_25 AC 25 ms
6,076 KB
testcase_26 WA -
testcase_27 AC 12 ms
6,256 KB
testcase_28 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using LL=long long;
using ULL=unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

const int M=223577;

struct Query{ int i,j; };

int N;
LL P;
LL A[100000];
LL B[100000];
int I[M];
vector<Query> ans;

bool test_AeqB(){ rep(i,N) if(A[i]!=B[i]) return false; return true; }

// A[i]+=A[j]
void op(int i,int j){
  A[i]+=A[j]; if(A[i]>=P) A[i]-=P;
  ans.push_back({i,j});
}
// A[mem]!=0
// takes 40 moves
void make_k(int dst,int mem,LL k){
  LL t = (k+(P-A[dst])*(1ll<<18))%P *I[A[mem]]%P;
  for(int d=17; d>=0; d--){
    op(dst,dst);
    if(t&(1ll<<d)) op(dst,mem);
  }
}

int main(){
  scanf("%d%lld",&N,&P);
  rep(i,N){ int a; scanf("%d",&a); A[i]=a; }
  rep(i,N){ int a; scanf("%d",&a); B[i]=a; }

  I[0]=I[1]=1; for(int i=2; i<P; i++) I[i]=P-(LL)P/i*I[P%i]%P;
  
  {
    LL sumA=0; rep(i,N) sumA+=A[i];
    LL sumB=0; rep(i,N) sumB+=B[i];
    if(sumA==0 && sumB==0){ printf("0\n"); return 0; }
    if(sumA==0 && sumB!=0){ printf("-1\n"); return 0; }
    if(sumA!=0 && sumB==0){ printf("-1\n"); return 0; }
  }

  if(N==1){
    rep(i,P-1){ if(A[0]==B[0]) break; op(0,0); }
    if(!test_AeqB()){ printf("-1\n"); return 0; }
  }
  else{
    {
      int p=-1;
      rep(i,N) if(A[i]!=0) p=i;
      if(A[0]==0) op(0,p);
      if(A[1]==0) op(1,0);
    }
    int sqrtP=500;
    vector<vector<int>> V0(sqrtP);
    vector<vector<int>> V1(sqrtP);
    for(int i=2; i<N; i++){
      int t=(B[i]+P-A[i])%P;
      V1[t/sqrtP].push_back(i);
      V0[t%sqrtP].push_back(i);
    }
    make_k(0,1,1);
    make_k(1,0,1);
    rep(p,sqrtP-1){
      for(int j:V0[p+1]) op(j,0);
      op(0,1);
    }
    make_k(0,1,sqrtP);
    make_k(1,0,sqrtP);
    rep(p,sqrtP-1){
      for(int j:V1[p+1]) op(j,0);
      op(0,1);
    }
    make_k(0,1,1);
    int src=0; rep(i,N) if(B[i]!=0) src=i;
    if(src==0) make_k(src,1,B[src]); else make_k(src,0,B[src]);
    if(src!=0) make_k(0,src,B[0]);
    if(src!=1) make_k(1,src,B[1]);
  }

  assert(ans.size()<P);
  assert(test_AeqB());
  printf("%d\n",(int)ans.size()+1);
  rep(i,ans.size()) printf("1 %d %d\n",ans[i].i,ans[i].j);
  printf("2\n");
  return 0;
}

0