結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
4,688 KB
testcase_01 AC 8 ms
4,640 KB
testcase_02 AC 8 ms
4,688 KB
testcase_03 AC 8 ms
4,696 KB
testcase_04 AC 8 ms
4,704 KB
testcase_05 AC 8 ms
4,708 KB
testcase_06 AC 9 ms
4,716 KB
testcase_07 AC 8 ms
4,736 KB
testcase_08 AC 8 ms
4,740 KB
testcase_09 AC 8 ms
4,748 KB
testcase_10 AC 8 ms
4,752 KB
testcase_11 AC 8 ms
4,760 KB
testcase_12 AC 8 ms
4,760 KB
testcase_13 AC 69 ms
8,928 KB
testcase_14 AC 69 ms
8,940 KB
testcase_15 AC 69 ms
8,936 KB
testcase_16 AC 68 ms
8,924 KB
testcase_17 AC 69 ms
8,928 KB
testcase_18 AC 69 ms
8,932 KB
testcase_19 AC 69 ms
8,928 KB
testcase_20 AC 68 ms
8,932 KB
testcase_21 AC 69 ms
8,936 KB
testcase_22 AC 68 ms
8,924 KB
testcase_23 AC 28 ms
6,076 KB
testcase_24 AC 28 ms
6,076 KB
testcase_25 AC 25 ms
6,076 KB
testcase_26 AC 16 ms
4,816 KB
testcase_27 AC 12 ms
6,256 KB
testcase_28 AC 65 ms
8,624 KB
権限があれば一括ダウンロードができます

ソースコード

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+1,j+1});
}
// 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