結果

問題 No.1404 誕生日プレゼント
ユーザー 👑 Nachia
提出日時 2021-02-20 00:41:29
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 71 ms / 3,153 ms
コード長 2,135 bytes
コンパイル時間 1,994 ms
コンパイル使用メモリ 199,404 KB
最終ジャッジ日時 2025-01-19 02:20:41
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:36:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   36 |   scanf("%d%lld",&N,&P);
      |   ~~~~~^~~~~~~~~~~~~~~~
main.cpp:37:25: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   37 |   rep(i,N){ int a; scanf("%d",&a); A[i]=a; }
      |                    ~~~~~^~~~~~~~~
main.cpp:38:25: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   38 |   rep(i,N){ int a; scanf("%d",&a); B[i]=a; }
      |                    ~~~~~^~~~~~~~~

ソースコード

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