結果

問題 No.983 Convolution
ユーザー LayCurseLayCurse
提出日時 2020-02-11 14:14:11
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 4,206 bytes
コンパイル時間 2,619 ms
コンパイル使用メモリ 215,048 KB
実行使用メモリ 12,728 KB
最終ジャッジ日時 2024-04-08 14:54:11
合計ジャッジ時間 4,494 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,624 KB
testcase_01 AC 3 ms
9,672 KB
testcase_02 AC 3 ms
9,672 KB
testcase_03 AC 23 ms
12,104 KB
testcase_04 AC 18 ms
12,104 KB
testcase_05 AC 16 ms
11,976 KB
testcase_06 AC 25 ms
12,268 KB
testcase_07 AC 25 ms
12,268 KB
testcase_08 AC 7 ms
11,720 KB
testcase_09 AC 10 ms
11,848 KB
testcase_10 AC 5 ms
11,720 KB
testcase_11 AC 7 ms
11,720 KB
testcase_12 AC 29 ms
12,728 KB
testcase_13 AC 9 ms
11,848 KB
testcase_14 AC 11 ms
11,848 KB
testcase_15 AC 20 ms
12,104 KB
testcase_16 AC 16 ms
11,976 KB
testcase_17 AC 24 ms
12,356 KB
testcase_18 AC 6 ms
11,720 KB
testcase_19 AC 16 ms
11,976 KB
testcase_20 AC 28 ms
12,712 KB
testcase_21 AC 20 ms
12,104 KB
testcase_22 AC 7 ms
11,720 KB
testcase_23 AC 17 ms
10,696 KB
testcase_24 AC 5 ms
8,136 KB
testcase_25 AC 5 ms
8,008 KB
testcase_26 AC 11 ms
9,160 KB
testcase_27 AC 9 ms
8,776 KB
testcase_28 AC 16 ms
11,976 KB
testcase_29 AC 31 ms
12,640 KB
testcase_30 AC 17 ms
11,976 KB
testcase_31 AC 10 ms
11,848 KB
testcase_32 AC 18 ms
11,976 KB
testcase_33 AC 2 ms
7,624 KB
testcase_34 AC 3 ms
9,672 KB
testcase_35 AC 3 ms
9,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void rd(long long &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void wt_L(char a){
  putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    putchar_unlocked('-');
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    putchar_unlocked('-');
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
template<class T, class U> inline T GCD_L(T a, U b){
  T r;
  while(b){
    r=a;
    a=b;
    b=r%a;
  }
  return a;
}
template<class T, class S> void ZetaTransform_L(int N, T A[], S res[] = NULL){
  int i;
  int j;
  if(res==NULL){
    for(i=0;(1<<i)<N;i++){
      for(j=(0);j<(N);j++){
        if(j&1<<i){
          A[j] += A[j^(1<<i)];
        }
      }
    }
  }
  else{
    for(j=(0);j<(N);j++){
      res[j] = A[j];
    }
    for(i=0;(1<<i)<N;i++){
      for(j=(0);j<(N);j++){
        if(j&1<<i){
          res[j] += res[j^(1<<i)];
        }
      }
    }
  }
}
template<class T> void MoebiusTransform(int N, T A[], T res[] = NULL){
  int i;
  int j;
  if(res==NULL){
    for(i=0;(1<<i)<N;i++){
      for(j=(0);j<(N);j++){
        if(j&1<<i){
          A[j] -= A[j^(1<<i)];
        }
      }
    }
  }
  else{
    for(j=(0);j<(N);j++){
      res[j] = A[j];
    }
    for(i=0;(1<<i)<N;i++){
      for(j=(0);j<(N);j++){
        if(j&1<<i){
          res[j] -= res[j^(1<<i)];
        }
      }
    }
  }
}
inline void wt_L(__int128_t x){
  int s=0;
  int m=0;
  char f[40];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    putchar_unlocked('-');
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
int N;
long long A[200000];
__int128_t b[200000];
__int128_t b2[200000];
int c[200000];
int c2[200000];
int main(){
  int i;
  __int128_t g;
  rd(N);
  {
    int cTE1_r3A;
    for(cTE1_r3A=(0);cTE1_r3A<(N);cTE1_r3A++){
      rd(A[cTE1_r3A]);
    }
  }
  for(i=(0);i<(N);i++){
    if(A[i]==-1){
      c[i]++;
    }
    else{
      b[i] = A[i];
    }
  }
  ZetaTransform_L(N,b,b2);
  ZetaTransform_L(N,c,c2);
  for(i=(0);i<(N);i++){
    b2[i] *= b2[i];
  }
  for(i=(0);i<(N);i++){
    c2[i] *= c2[i];
  }
  MoebiusTransform(N,b2,b);
  MoebiusTransform(N,c2,c);
  {
    int a2conNHc;
    __int128_t hCmBdyQB;
    if(N==0){
      hCmBdyQB = 0;
    }
    else{
      hCmBdyQB = b[0];
      for(a2conNHc=(1);a2conNHc<(N);a2conNHc++){
        hCmBdyQB = GCD_L(hCmBdyQB, b[a2conNHc]);
      }
    }
    g =hCmBdyQB;
  }
  if(g==0){
    g = -1;
  }
  wt_L(g);
  wt_L('\n');
  return 0;
}
// cLay varsion 20200119-1

// --- original code ---
// inline void wt_L(__int128_t x){
//   int s=0, m=0;
//   char f[40];
//   if(x<0) m=1, x=-x;
//   while(x) f[s++]=x%10, x/=10;
//   if(!s) f[s++]=0;
//   if(m) putchar_unlocked('-');
//   while(s--) putchar_unlocked(f[s]+'0');
// }
// 
// int N; ll A[2d5];
// 
// __int128_t b[2d5], b2[2d5];
// int c[2d5], c2[2d5];
// {
//   __int128_t g;
// 
//   rd(N,A(N));
//   rep(i,N) if[A[i]==-1, c[i]++, b[i] = A[i]];
// 
//   ZetaTransform(N,b,b2);
//   ZetaTransform(N,c,c2);
//   rep(i,N) b2[i] *= b2[i];
//   rep(i,N) c2[i] *= c2[i];
//   MoebiusTransform(N,b2,b);
//   MoebiusTransform(N,c2,c);
// 
// //  rep(i,N) if(c[i]) b[i] = 0;
//   g = gcd(b(N));
//   if(g==0) g = -1;
//   wt(g);
// }
0