#include <iostream>
#include <algorithm>

std::vector<size_t> gaps = {1};

size_t insertion_sort(size_t a[], size_t n, size_t gap = 1){
  size_t count = 0;
  for(size_t i = gap; i < n; i++){
    size_t v = a[i];
    int j = i - gap;
    while(j >= 0 && a[j] > v){
      a[j+gap] = a[j];
      j -= gap;
      count++;
    }
    a[j+gap] = v;
  }
  return count;
}

void make_gap_table(size_t n){
  size_t h = 4;
  while(h < n){
    gaps.push_back(h);
    h = 3*h + 1;
  }
  std::reverse(gaps.begin(),gaps.end());
}

void shell_sort(size_t a[], size_t n){
  make_gap_table(n);
  size_t count = 0;
  for(auto const &v : gaps){
    count += insertion_sort(a,n,v);
  }

  std::cout << gaps.size() << '\n';

  for(int i=0;i<gaps.size()-1;i++){
    std::cout << gaps[i] << ' ';
  }
  std::cout << gaps[gaps.size()-1] << '\n';

  std::cout << count << '\n';
}

int main(){
  size_t n;
  size_t buf;
  std::vector<size_t> a;
  std::cin >> n;
  for(size_t i = 0; i < n; i++){
    std::cin >> buf;
    a.push_back(buf);
  }

  shell_sort(a.data(),a.size());
  for(auto &v : a){
    std::cout << v << '\n';
  }
}

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Shell Sort

Shell Sort is a generalization of Insertion Sort to arrange a list of $n$ elements $A$.

1 insertionSort(A, n, g)
2 for i = g to n-1
3 v = A[i]
4 j = i - g
5 while j >= 0 && A[j] > v
6 A[j+g] = A[j]
7 j = j - g
8 cnt++
9 A[j+g] = v
10
11 shellSort(A, n)
12 cnt = 0
13 m = ?
14 G[] = {?, ?,..., ?}
15 for i = 0 to m-1
16 insertionSort(A, n, G[i])

A function shellSort(A, n) performs a function insertionSort(A, n, g), which considers every $g$-th elements. Beginning with large values of $g$, it repeats the insertion sort with smaller $g$.

Your task is to complete the above program by filling ?. Write a program which reads an integer $n$ and a sequence $A$, and prints $m$, $G_i (i = 0, 1, ..., m − 1)$ in the pseudo code and the sequence $A$ in ascending order. The output of your program must meet the following requirements:

$1 \leq m \leq 100$

$0 \leq G_i \leq n$

cnt does not exceed $\lceil n^{1.5}\rceil$

Input

In the first line, an integer $n$ is given. In the following $n$ lines, $A_i (i=0,1,...,n-1)$ are given for each line.

Output

In the first line, print an integer $m$. In the second line, print $m$ integers $G_i (i=0,1,...,m-1)$ separated by single space character in a line.

In the third line, print cnt in a line. In the following $n$ lines, print $A_i (i=0,1,...,n-1)$ respectively.

This problem has multiple solutions and the judge will be performed by a special validator.

Constraints

$1 \leq n \leq 1,000,000$

$0 \leq A_i \leq 10^9$

Sample Input 1

5
5
1
4
3
2

Sample Output 1

2
4 1
3
1
2
3
4
5

Sample Input 2

3
3
2
1

Sample Output 2

1
1
3
1
2
3

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


#include <iostream>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;

int calcIndex(char* str){
    int index = 0, i = 0;
    bool flag = true;
    for(; str[i] == 'A' || str[i] == 'C' || str[i] == 'T' || str[i] == 'G'; i++){
        index += (str[i] == 'A' ? 0 : str[i] == 'C' ? 1 : str[i] == 'T' ? 2 : 3) * pow(4, i);
        if(str[i] != 'A') flag = false;
    }
    if(flag){
        index = 16777215 + i;
    }
    return index;
}

int main() {
    int n;
    scanf("%d", &n);

    bool* map = new bool[20000000];
    for(int i = 0; i < 20000000; i++){
        map[i] = false;
    }

    while(n--){
        char s[8], d[16];
        scanf("%s %s", s, d);

        if(s[0] == 'i'){
            map[calcIndex(d)] = true;
        }else if(map[calcIndex(d)]){
            printf("yes\n");
        }else{
            printf("no\n");
        }
    }

    return 0;
}

Search III

Your task is to write a program of a simple dictionary which implements the following instructions:

insert str: insert a string str in to the dictionary

find str: if the distionary contains str, then print 'yes', otherwise print 'no'

Input

In the first line n, the number of instructions is given. In the following n lines, n instructions are given in the above mentioned format.

Output

Print yes or no for each find instruction in a line.

Constraints

A string consists of 'A', 'C', 'G', or 'T'

1 ≤ length of a string ≤ 12

n ≤ 1000000

Sample Input 1

5
insert A
insert T
insert C
find G
find A

Sample Output 1

no
yes

Sample Input 2

13
insert AAA
insert AAC
insert AGA
insert AGG
insert TTT
find AAA
find CCC
find CCC
insert CCC
find CCC
insert T
find TTT
find T

Sample Output 2

yes
no
no
yes
yes
yes

Notes

Template in C


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
using namespace std;

class dictionary {

private:
	string* hashtable;
	long long table_size;
	int el_num;
	long long hash1(string);
	long long hash2(string);
	long long str_n(string);
public:
	dictionary(int n);
	void insert(string);
	bool find(string);
};
inline dictionary::dictionary(int n)
{
	hashtable= new string[n];
	if( !hashtable ){
		cout<<"Allocation Error"<<endl;
		abort();
	}

	table_size=n;
	el_num=0;
}
inline long long dictionary::hash1( string str )
{
	return str_n(str) % table_size;
}
inline long long dictionary::hash2( string str )
{
	return 1 + (str_n(str) % (table_size - 1));
}

inline long long dictionary::str_n( string str )
{

	long long str_n=0;

	for( unsigned int i=0; i<str.size(); i++ ){
		str_n = str_n * 5 + (long long)str[i] ; // str_n += (int)str[i] ?????????????????????????????????????????£???
	}

	return str_n;

/* <answer>
	long long str_n=0;
	for( unsigned int i=0, p=1; i<str.size(); i++,p*=5 ){
		switch(str[i]){
		case 'A':
			str_n += p*1;
			break;
		case 'C':
			str_n += p*2;
			break;
		case 'G':
			str_n += p*3;
			break;
		case 'T':
			str_n += p*4;
			break;
		}
	}
	return str_n;
</answer> */
}

inline void dictionary::insert( string str )
{
/*
	if( el_num >= table_size ){
		cout<<"????????\????????????????????????"<<endl;
		abort();
	}
	long long hash_v;
	do{
		hash_v = ( hash1(str) + hash2(str)*coltimes )%table_size ;

//		printf("I ( hash1, hash2, hash )(coltimes) = ( %d, %d, %d)(%d)\n", hash1(str), hash2(str), hash_v, coltimes );

	}while( hashtable[ hash_v ].size() && hashtable[ hash_v ]!= str, coltimes++ );
*/

	long long hash_v;

	for( int i=0; ; i++ ){
		hash_v= (hash1(str) + hash2(str)*i) % table_size;
		if( hashtable[ hash_v ] == str ){
			return;
		}else if( !hashtable[ hash_v ].size() ){
			hashtable[ hash_v ]=str;
			return;
		}
	}
}
inline bool dictionary::find( string str )
{

	long long hash_v;
	int coltimes=0;
	do{

		hash_v = ( hash1(str) + hash2(str)*coltimes )%table_size ;
//		printf("F ( hash1, hash2, hash )(coltimes) = ( %d, %d, %d)(%d)\n", hash1(str), hash2(str), hash_v, coltimes );

		if( hashtable[ hash_v ] == str ){
			return true;
		}

		coltimes++;

	}while( hashtable[ hash_v ].size() &&  coltimes <= table_size);

	return false;
}

int main()
{
	int n;
	scanf("%d", &n);
	char command[7], str[13];
	dictionary dic(1046527);

	for( int i=0; i<n; i++ ){
		scanf("%s %s", command, str);

		if( !strcmp(command, "insert") ){
			dic.insert(str);
		}else if ( !strcmp(command, "find") ){
			cout<<( dic.find(str)? "yes" : "no" )<<endl;
		}
	}
}


#include<iostream>
#include<cstdio>
#include<string>
#include<map>
using namespace std;

int main() {
  int n;
  char str[10], com[13];
  map<string, bool>  T;

  cin >> n;
  for ( int i = 0; i < n; i++ ) {
    scanf("%s%s", com, str);
    if ( com[0] == 'i' ) T[string(str)] = true;
    else {
      if ( T[string(str)] ) printf("yes\n");
      else printf("no\n");
    }
  }

  return 0;
}


/**
 *      purpose :
 *      author  : kyomukyomupurin
 *      created :
**/

// input/output
#include <fstream>
#include <iostream>
#include <sstream>
// container class
#include <array>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// math, algorithm
#include <algorithm>
#include <cmath>
#include <complex>
#include <numeric>
// etc
#include <bitset>
#include <cassert>
#include <cctype>
#include <cstring>
#include <chrono>
#include <iomanip>
#include <random>
#include <utility>
// using-directive
using namespace std;
// alias template
using int64 = long long;
using vi = vector<int>;
using vl = vector<int64>;
using pii = pair<int, int>;
using pii = pair<int, int>;
using pll = pair<int64, int64>;
// text macro replacement
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define print(x) cout << (x) << '\n'
#define debug(x) cerr << #x << ": " << (x) << '\n'
#define dbg(v) for (size_t _ = 0; _ < v.size(); ++_){cerr << #v << "[" << _ << "] : " << v[_] << '\n';}
// variadic template
template<typename T> inline void chmin(T &a, T b) {if (a > b) a = b; return;}
template<typename T> inline void chmax(T &a, T b) {if (a < b) a = b; return;}
// constant
const int INF = (1<<30) - 1;
const int64 INF64 = (1LL<<62) - 1;
const int MOD = 1000000007;

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n; cin >> n;
  set<string> st;
  for (int i = 0; i < n; ++i) {
    string op, k; cin >> op >> k;
    if (op == "insert") {
      st.insert(k);
    } else {
      if (st.find(k) != st.end()) {
        cout << "yes" << '\n';
      } else {
        cout << "no" << '\n';
      }
    }
  }

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int (i)=0;(i)<(n);i++)
#define INF 1001001001
#define LLINF 1001001001001001001
#define MOD 1000000007
#define FOUT(n, dist) cout<<fixed<<setprecision(n)<<(dist);
#define BR cout<<endl;
#define LEN(s) s.length();
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define ALL(v) v.begin(), v.end()
typedef long long ll;
const ll mod=1000000007;

ll powmod(ll x,ll y){
    ll res=1;
    for(ll i=0;i<y;i++){
        res=res*x%mod;
    }
    return res;
}

double expect(int x){
    double kitai,tem=0;
    for(int i=1;i<=x;i++){
        tem += i;
    }
    kitai = tem/x;
    cout << kitai << endl;
    return kitai;

}

int main(){
    int n;
    cin >> n;

    ll a[n];
    rep(i,n)cin >> a[i];

    ll sum = 0, ans = 0;

    for(int i = 0; i < n; i++){
        for(int j = i + 1; j < n; j++){
            sum += a[i] * a[j];
            sum = sum % mod;
        }
    }

    cout << sum << endl;

    return 0;
}

Score : 300 points

Problem Statement

Given are N integers A_1,\ldots,A_N.

Find the sum of A_i \times A_j over all pairs (i,j) such that 1\leq i < j \leq N, modulo (10^9+7).

Constraints

2 \leq N \leq 2\times 10^5

0 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print \sum_{i=1}^{N-1}\sum_{j=i+1}^{N} A_i A_j, modulo (10^9+7).

Sample Input 1

3
1 2 3

Sample Output 1

11

We have 1 \times 2 + 1 \times 3 + 2 \times 3 = 11.

Sample Input 2

4
141421356 17320508 22360679 244949

Sample Output 2

437235829


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

#define ll long long int
#define endl                 "\n"
#define space                " "
#define TLE_na_ho            ios_base::sync_with_stdio(false);cin.tie(NULL)
#define MAX_SIZE             1024
#define MOD                  1000000007
#define pb                   push_back
#define fi                   first
#define se                   second
#define rep(i,a,n)           for(int i=a;i<n;i++)
#define repe(i,a,n)          for(int i=a;i<=n;i++)
#define repr(i,a,n)          for(int i=a;i>=n;i--)
#define ain(arr,n)           for(int i1=0;i1<n;i1++ ) cin>>arr[i1]
#define aout(arr,n)          for(int i1=0;i1<n;i1++ ) cout<<arr[i1]<<space

int main()
{
    TLE_na_ho;
    ll n;
    cin>>n;
    ll a[n];
    ll b[n+1]={0};
    rep(i,0,n){
    	cin>>a[i];
    	b[i+1]+=b[i]+a[i];
    }
    ll ans=0;
    rep(i,0,n)
    {
    	ll sum=b[n]-b[i+1];
    	ans+=a[i]*sum;
    	ans%=MOD;
    }

    cout<<ans;

}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin>>n;

    long long arra[n+1],sum=0;

    int i=1,l=n;

    while(l--){
        cin>>arra[i];
        i++;
    }

    for(i=1;i<n;i++){
        for(int j=i+1;j<=n;j++){
            sum+=arra[i]*arra[j];
            sum%=1000000007;
        }
    }

    cout<<sum<<endl;

    return 0;
}


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

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize ("-ffloat-store")
#pragma GCC optimize ("-fno-defer-pop")

typedef long long int ll;
typedef long double ld;

ll mod = 1e9+7;

ll add(ll a, ll b){
	a = a%mod;
	b = b%mod;
	return (mod+(a+b)%mod)%mod;
}

ll mul(ll a, ll b){
	a = a%mod;
	b = b%mod;
	return (mod+(a*b)%mod)%mod;
}

int main(){

	std::ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	ll n;
	cin>>n;

	ll arr[n];
	ll su = 0;
	for(ll i=0;i<n;i++){
		cin>>arr[i];
		su += arr[i];
	}
	ll ans = 0;
	for(ll i=0;i<n;i++){
		su-=arr[i];
		ans = add(ans, mul(arr[i], su));
	}
	cout<<ans<<endl;
}


/*
	---------O7Oo7oO7O-------------
*/

#include <bits/stdc++.h>

#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define int long long
using namespace std;
#define all(v) v.begin(),v.end()

const int mod = 1000*1000*1000 +7;
int Bin_expo(int n, int b)
{
	int res = 1;
	while (b > 0)
	{
		if (b & 1)
		{
			res = (res * n) % mod;

		}
		n = (n * n)  % mod;

		b /= 2;

	}
	return res % mod;
}

int32_t main()
{
	IOS

	int n;
	cin>>n;
	vector<int> v(n);
	vector<int> suf(n,0),pref(n);
	int p = 0;
	for(int i=0;i<n;i++)
	{
		cin>>v[i];

		if(i==0)
			pref[i] = v[i];
		else
			pref[i] = pref[i-1] +v[i];

	}

	suf[n-1] = v[n-1];
	for(int i=n-2;i>=0;i--)
	{
		suf[i] = (suf[i+1] + v[i]) % mod;
	}

	for(int i=0;i<n-1;i++)
	{
		p += (v[i] * suf[i+1]) % mod;
		p%=mod;
		if(p<0)
			p+=mod;
	}

	cout<<p<<"\n";
}


#include <bits/stdc++.h>
#define rep(i,n) for(long long i=0; i<n; i++)
#define Rep(i,n) for(long long i=1; i<n; i++)
#define ll long long
#include <math.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;
#define PI acos(-1)
using P =pair<ll,ll>;

ll ketasu(ll a){
   ll num=1;
   while(a/10){
       num++;
       a/=10;
   }
   return num;
}

ll gcd(ll a,ll b){return b ? gcd(b,a%b) :a;}
ll lcm(ll a,ll b){return a*b/gcd(a,b);}

ll kosuu(ll a){
    ll sum=0;
    for(ll i=1;i*i<=a;i++){
        if(a%i==0){
            if(a!=1&&i*i!=a){
            sum+=2;
            }else{
             sum++;
            }
        }
    }
    return sum;
}

ll n;

    vector<pair<ll,ll>>p;
void fs(ll a){
    for(ll i=2;i*i<=n;i++){
        ll cnt=0;
        while(n%i==0){
            n/=i;
            cnt++;
        }
        p.emplace_back(i,cnt);
    }
    return;
}

ll di[]={1,0,-1,0};
ll dj[]={0,-1,0,1};
int main(){
   int n;
   cin>>n;
    long long int a[n],ans=0,i;
    int mod=1000000007;
    long long int sum=0;

    for( i = 0 ; i < n ; i++ ){
            cin>>a[i];
        sum = sum + a[i];
    }
    for( i = 0 ; i < n ; i++ ){
        sum = sum - a[i];

        ans+=(a[i]%mod * sum%mod)%mod;
    }
    cout<<ans%mod;

    return 0;
}


#include<bits/stdc++.h>

using namespace std;
const int mod = 1e9 + 7;

int main() {
    int n; cin >> n; int arr[n];
    for (int i = 0; i < n; i++) cin >> arr[i];
    long long int suffix[n]; suffix[n - 1] = arr[n - 1];
    for (int i = n - 2; i >= 0; i--) {
        suffix[i] = (suffix[i + 1] + arr[i]) % mod;
    }
    long long int ans = 0;
    for (int i = 0; i < n - 1; i++) {
        ans += (1LL * arr[i] * suffix[i + 1]) % mod;
    }
    cout << ans % mod;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long m=1000000007;
int main(){
   int n;
   cin>>n;
   vector<long long> a(n);
   for (int i = 0; i < n; i++)
   {
       cin>>a[i];
   }
   long long sum=0;
   for (int i = 0; i < n; i++)
   {
       for (int j = i+1; j < n; j++)
       {
           sum+=(a[i]*a[j])%m;
       }

   }
cout<<sum%m;

    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
using ll=long long;
#define rep(i,a,b) for(ll i=a;i<ll(b);i++)
#define repr(i,a,b) for(ll i=a;i>=ll(b);i--)
#define endl "\n"
#define ALL(x) x.begin(),x.end()
#define ALLR(x) x.rbegin(),x.rend()
#define INF 1e9
#define DEBUG(x) cout<<"debug: "<<x<<endl

const int MOD = 1000000007;

int n;
int main() {
    cin >> n;
    vector<ll> a(n), b(n+1, 0);
    rep(i, 0, n) cin >> a[i];
    rep(i, 0, n+1) b[i+1] = b[i] + a[i];

    ll ans = 0;
    rep(i, 0, n){
        ll ta = a[i];
        ll tb = b[n] - b[i+1]; tb %= MOD;
        ans += ta*tb;
        ans %= MOD;
    }

    cout << ans << endl;
    return 0;
}


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

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define fill fill_n
#define pf push_front
#define pb push_back
#define FOR(i,l,r) for(int i=l;i<r;i++)
#define ROF(i,r,l) for(int i=r;i>=l;i--)
#define comp(v) (v).begin(),(v).end()
#define ff first
#define ss second
#define infile freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
typedef vector<int> vi;
typedef vector<ll> vli;
typedef vector<pair<int,int>> vpi;

const int M = 1e9+7;
const int N = 2e5;
ll po(int,int);

void solve(){
    int n;cin>>n;

    ll a;

    ll s=0,ans=0;

    FOR(i,0,n){
        cin>>a;
        ans = (ans + (a*s)%M)%M;
        s = (s+a)%M;
    }
    cout<<ans<<"\n";

}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
    int t=1;
    //cin>>t;
    FOR(i,1,t+1){
        //cout<<"Case #"<<i<<": ";
        solve();
    }

	return 0;

}

ll po(int a,int b){
    if(b==0) return 1;
    ll ans=1;
    if(b%2==0){
        ans = po(a,b/2)%M;
        ans=(ans%M * ans%M)%M;
    }
    else{
        ans = po(a,(b-1)/2)%M;
        ans = (ans%M * ans%M * a)%M;
    }
    return ans%M;
}


#include <bits/stdc++.h>

//{ START
using namespace std;
#define int int64_t
#define rep(i, a, n) for (int i = (a); i < (n); ++i)
#define reps(i, a, n) for (int i = (n - 1); i > (a - 1); --i)
#define arep(i, x) for (auto &&i : (x))
#define irep(i, x) for (auto i = (x).begin(); i != (x).end(); ++i)
#define rirep(i, x) for (auto i = (x).rbegin(); i != (x).rend(); ++i)
//降順はgreater<T>()
#define all(x) (x).begin(), (x).end()
#define rv(s) reverse((s).begin(), (s).end())
// gcd lcmはそのままok
#define gcd(a, b) __gcd(a, b)
#define bits(n) (1LL << (n))
#define pcnt(x) __builtin_popcountll(x)
//配列内等要素削除
#define Unique(x) (x).erase(unique((x).begin(), (x).end()), (x).end())
#define Fixed(n) fixed << setprecision(n)
//総和
#define sowa(n) (((n) * ((n) + 1)) / 2)
#define updiv(a, b) ((a + b - 1) / b)
#define cauto const auto &
using P = pair<int, int>;
using Graph = vector<vector<P>>;
template <class T>  //昇順
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class T>  //降順
using max_heap = priority_queue<T>;
template <class A, class B>
using umap = unordered_map<A, B>;
template <class A>
using uset = unordered_set<A>;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {  //多次元初期化
  std::fill((T *)array, (T *)(array + N), val);
}
template <class A, class B>
bool chmax(A &a, const B &b) {  //最大値更新 返り値はbool
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class A, class B>
bool chmin(A &a, const B &b) {  //最小値更新 返り値はbool
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, 1, -1, -1};
constexpr int INF = 0x3f3f3f3f;
constexpr int LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int mod1 = 1e9 + 7;
constexpr int mod2 = 998244353;
//} END

/*
BIT(sz) : 長さszの0で初期化された配列を構築する
BIT(vs) : 配列vsで構築する
add(k,x) : 要素kに値xを加える
query(k) : 区間[0,k]の総和を求める
lower_bound(x) : 区間[0,k]の総和がx以上となる最小のkを返す
upper_bound(x) : 区間[0,k]の総和がxを上回る最小のkを返す
*/
template <typename T>
struct BIT {
  vector<T> data;

  BIT() = default;

  explicit BIT(size_t sz) : data(sz + 1, 0) {}

  explicit BIT(const vector<T> &vs) : data(vs.size() + 1, 0) {
    for (size_t i = 0; i < vs.size(); i++) data[i + 1] = vs[i];
    for (size_t i = 1; i < data.size(); i++) {
      size_t j = i + (i & -i);
      if (j < data.size()) data[j] += data[i];
    }
  }

  void add(int k, const T &x) {
    for (++k; k < (int)data.size(); k += k & -k) data[k] += x;
  }

  T query(int k) const {
    T ret = T();
    for (++k; k > 0; k -= k & -k) ret += data[k];
    return ret;
  }

  int lower_bound(T x) const {
    int i = 0;
    for (int k = 1 << (__lg(data.size() - 1) + 1); k > 0; k >>= 1) {
      if (i + k < data.size() && data[i + k] < x) {
        x -= data[i + k];
        i += k;
      }
    }
    return i;
  }

  int upper_bound(T x) const {
    int i = 0;
    for (int k = 1 << (__lg(data.size() - 1) + 1); k > 0; k >>= 1) {
      if (i + k < data.size() && data[i + k] <= x) {
        x -= data[i + k];
        i += k;
      }
    }
    return i;
  }
};

signed main(void) {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<int> x(n);
  arep(i, x) cin >> i;
  BIT<int> seg(x);
  int ans = 0;
  rep(i, 0, n - 1) ans =
      (ans + ((seg.query(n - 1) - seg.query(i)) % mod1) * x[i]) % mod1;
  cout << ans << '\n';
  return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

/*
ID: febrian3
LANG: C++11
PROB: task
*/

#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1)
#define IOS ios_base::sync_with_stdio(0); cin.tie(); cout.tie();
#define fi first
#define se second
#define pf push_front
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define debug(x) cerr<<#x<<": "<<(x)<<'\n'
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1e9+7;
const ll inf = 0x3f3f3f3f;
const ll nax = 0;
ll n;

int main(){
		/*freopen("filename.in","r",stdin);
		freopen("filename.out","w",stdout);*/
		IOS
		cin >> n;
		vector<ll>arr(n+1);
		for(int i=0;i<n;++i){
			cin >> arr[i];
		}
		vector<ll>pref(n, 0);
		for(int i=n-1;i>=0;--i){
			pref[i]=arr[i];
			if(i<n-1) pref[i]+=pref[i+1];
		}
		ll ans=0;
		for(int i=0;i<n-1;++i){
			ll cur=arr[i]*pref[i+1];
			ans=(ans+cur)%mod;
		}
		cout << ans << '\n';
	}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;

// find_by_order(k)  returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;

# define ll long long int
#define M 1000000007
#define pb push_back
#define ss second
#define ff first
#define inf 1000000000000000

													//nestedcode

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    ll n,i,ans = 0;
    cin>>n;
    ll a[n+5], dp[n+5];
    for(i=0;i<n;i++){
    	cin>>a[i];
    	if(i == 0)
    	dp[i] = a[i];
    	else
    	dp[i] = dp[i-1] + a[i];
	}
	for(i=1;i<n;i++){
		ans += (dp[i-1]*a[i])%M;
		ans %= M;
	}
	cout<<ans<<endl;
}


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

int main() {
  int n;
  cin >> n;
  vector<long long>a(n);
  long long sum1=0;
  for (int i=0;i<n;i++) {
    cin >> a.at(i);
    sum1+=a.at(i);
  }
  sum1%=1000000007;
  long long ans=0;
  for (int i=0;i<n-1;i++) {
    sum1-=a.at(i);
    sum1%=1000000007;
    ans+=a.at(i)*sum1;
    ans%=1000000007;
  }
  cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int MAX=1e9+7;
int main()
{
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    ll sum=0;
    for(int i=0;i<n-1;i++){
        for(int j=i+1;j<n;j++){
            ll product=((ll)(arr[i]%MAX)*(arr[j]%MAX))%MAX;
            sum=((sum%MAX)+(product%MAX))%MAX;
        }
    }
    cout<<sum<<"\n";
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
   int n;
   const int mod = 1000000007;
   cin >> n;
   long long  a[n];
   long long ans=0;
   for(int i = 0; i < n; ++i) {
     cin >> a[i];
   }
   for(int i = 0; i < n-1; ++i) {
     for(int j = i+1; j < n; ++j) {
       ans = (ans + (a[i] * a[j] % mod)) % mod;
     }
   }
   cout << ans << endl;
}


    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define pb push_back
    #define NUM 1000000007
    #define MAX 100001
    #define INF LLONG_MAX
    int main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
        ll n,tmp;
        cin>>n;
        vector<ll> v;
        for(int i=0;i<n;i++){
            cin>>tmp;
            v.pb(tmp);
        }
        vector<ll> prf(n+1);
        prf[0]=v[0];
        for(int i=1;i<n;i++){
            prf[i]=prf[i-1]+v[i];
        }
        ll ans = 0;
        for(int i=0;i<n-1;i++){
            ans=(ans+(v[i]*(prf[n-1]-prf[i])))%NUM;
        }
        cout<<ans<<endl;
        return 0;
    }


#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ll long long
#define ull unsigned long long
#define sii set <ll>
#define vi vector <ll>
#define popcount(x) __builtin_popcountll(x)
#define mii map <ll,ll>
#define vpi vector <pair <ll,ll> >
#define sz(c) (int)c.size()
#define fr first
#define ll long long
#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define sc second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define mem0(a) memset(a,0,sizeof(a))
#define rep(i,a,n) for(ll i=a ; i<n ; i++)
#define ld long double
#define rall(a) (a).rbegin(),(a).rend()
#define INF 1000000000
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")

ll power(ll b,ll e,ll m)
{
  if(b==0) return 0;
  if(e==0) return 1;
  if(e&1) return b*power(b*b%m,e/2,m)%m;
  return power(b*b%m,e/2,m);
}
ll power( ll b, ll e)
{
  if(b==0) return 0;
  if(e==0) return 1;
  if(e&1) return b*power(b*b,e/2);
  return power(b*b,e/2);
}
bool isPowerOfTwo(ll x)
{
    // x will check if x == 0 and !(x & (x - 1)) will check if x is a power of 2 or not
    return (x && !(x & (x - 1)));
}

 void solution(ll a[],ll n)
 {
 	ll sum=0;
 	rep(i,0,n)
 	sum=(sum%MOD+a[i]%MOD)%MOD;

 	ll square = ((sum%MOD)*(sum%MOD))%MOD;

 	ll sum2=0;

 	rep(i,0,n)
 	{
 		sum2=((sum2%MOD)+((a[i]%MOD)*(a[i]%MOD))%MOD)%MOD;
 	}

 	// cout<<square<<"\n";
 	// cout<<sum2<<"\n";

 	ll ans= ((square%MOD - sum2%MOD)+MOD)%MOD;

 	cout<<ans/2;
 }

void solve()
{
	ll n;
	cin>>n;
	ll a[n];
	rep(i,0,n) cin>>a[i];
	solution(a,n);
}

int main()
{
	// freopen("input.txt","r",stdin);
	// freopen("output.txt","w",stdout);
  fastio
  int tt=1;
  // cin>>tt;
  while(tt--)
  {
    solve();
    cout<<"\n";
  }
  return 0;
}


#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define ld long double
#define vi vector<int>
#define pb push_back
#define ff first
#define ss second
#define all(c) (c).begin(),(c).end()
#define sz(x) (int)(x).size()
#define EPS 0.000000001 //1e-9
#define pii pair<int,int>
using namespace std;
int32_t main()
{
    IOS;
    int n,s=0,p=1000000000+7;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            //int k=((a[i]%p)*(a[j]%p))%p;
            //int k=a[i]*a[j];

            s+=(((a[i]%p)*(a[j]%p))%p);
            s=s%p;
        }
    }
    cout << s << endl;
}


#include<bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; i++)
#define repo(i, n) for(int i=1; i<=n; i++)
#define INF 1001001001
#define INFll 100100100100100
// debug用
#define PrintVec(x) for (auto elementPrintVec: x) { cout << elementPrintVec << " "; } cout << endl;

using namespace std;
using ull = unsigned long long;
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const {
    return pow(mod-2);
  }
  mint& operator/=(const mint a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res/=a;
  }
};

int main(){
  int n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> s(n);
  rep(i, n){
    cin >> a[i];
    if(i==0){
      s[i]=a[i];
    } else {
      s[i]=a[i]+s[i-1];
    }
  }
  mint ans=0;
  repo(i, n-1){
    ans+=a[i]*s[i-1];
  }
  cout << ans.x << endl;
  return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;
#define ll long long
#define il inline
const int maxn=200010;
const ll mod=1e9+7;
ll a[maxn],s[maxn];
int n; ll ans=0;
int main(){
    scanf("%d",&n);
    for (int i=1;i<=n;++i){
        scanf("%lld",&a[i]);
        s[i]=s[i-1]+a[i];
    }
    for (int i=n;i>=2;--i){
        ans=(ans+s[i-1]*a[i])%mod;
    }
    printf("%lld",ans);
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using P=pair<ll,ll>;
template<class T> using V=vector<T>;
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
//const ll mod=998244353;
const ll mod=1000000007;
ll GCD(ll a,ll b) {return b ? GCD(b,a%b):a;}
ll LCM(ll c,ll d){return c/GCD(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
struct mint{
    using ull=unsigned long long int;
    ull v;
    mint(ll vv=0){s(vv%mod+mod);}
    mint& s(ull vv){
        v=vv<mod?vv:vv-mod;
        return *this;
    }
    //オーバーロード
    mint operator-()const{return mint()-*this;}//符号反転
    mint&operator+=(const mint&val){return s(v+val.v);}
    mint&operator-=(const mint&val){return s(v+mod-val.v);}
    mint&operator*=(const mint&val){
        v=ull(v)*val.v%mod;
        return *this;
    }
    mint&operator/=(const mint&val){return *this*=val.inv();}
    mint operator+(const mint&val){return mint(*this)+=val;}
    mint operator-(const mint&val){return mint(*this)-=val;}
    mint operator*(const mint&val){return mint(*this)*=val;}
    mint operator/(const mint&val){return mint(*this)/=val;}
    mint pow(ll n)const{
        mint res(1),x(*this);
        while(n){
            if(n&1)res*=x;
            x*=x;
            n>>=1;
        }
        return res;
    }
     mint inv()const{return pow(mod-2);}
    //拡張ユークリッドの互除法
    /* mint inv()const{
        int x,y;
        int g=extgcd(v,mod,x,y);
        assert(g==1);
        if(x<0)x+=mod;
        return mint(x);
    }*/
    friend ostream& operator<<(ostream&os,const mint&val){
        return os<<val.v;
    }//出力
    bool operator<(const mint&val)const{return v<val.v;}
    bool operator==(const mint&val)const{return v==val.v;}
    bool operator>(const mint&val)const{return v>val.v;}
};
int main(){
  int n;
  cin>>n;
  mint ans=0;
  V<ll> a(n);
  ll sum=0;
  for(int i=0;i<n;i++){
      cin>>a[i];
      sum+=a[i];
  }
  for(int i=0;i<n;i++){
      sum-=a[i];
      ans+=mint(sum)*mint(a[i]);
  }
  cout<<ans<<"\n";
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define pb push_back
#define MP(a, b) make_pair((a), (b))
template <class T> vector<T> make_vec(size_t a, T val) {
    return vector<T>(a, val);
}
template <class... Ts> auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec(ts...))>(a, make_vec(ts...));
}
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;
template <typename T> struct edge {
    int to;
    T cost;
    edge(int t, T c) : to(t), cost(c) {}
};
template <typename T> using WGraph = vector<vector<edge<T>>>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;

// 素数篩＋素因数分解
// 初期化O(N),素因数分解O(logN)
// たくさん素因数分解するときはこっち
#include <cassert>
struct prime_sieve {
    int N;
    vector<ll> sieve;
    prime_sieve(int n) : N(n + 1) {
        sieve.resize(N);
        iota(All(sieve), 0);
        init();
    }

    void init() {
        for(int i = 2; i * i <= N; i++) {
            if(sieve[i] < i)
                continue;
            for(int j = i * i; j < N; j += i) {
                if(sieve[j] == j)
                    sieve[j] = i;
            }
        }
    }

    map<ll, int> prime_factorize(ll n) {
        assert(n <= N);
        map<ll, int> ret;
        while(n > 1) {
            ret[sieve[n]]++;
            n = n / sieve[n];
        }
        return ret;
    }
};
int main() {
    int N;
    cin >> N;
    vector<ll> A(N);
    rep(i, N) { cin >> A[i]; }
    ll g = A[0];
    rep(i, N) { g = __gcd(g, A[i]); }
    if(g != 1) {
        cout << "not coprime" << endl;
    } else {
        prime_sieve sieve(1000001);
        set<int> se;
        bool ok = 1;
        rep(i, N) {
            auto m = sieve.prime_factorize(A[i]);
            for(auto p : m) {
                if(se.count(p.first))
                    ok = 0;
                se.insert(p.first);
            }
        }
        if(ok) {
            cout << "pairwise coprime" << endl;
        } else {
            cout << "setwise coprime" << endl;
        }
    }
}

Score : 500 points

Problem Statement

We have N integers. The i-th number is A_i.

\{A_i\} is said to be pairwise coprime when GCD(A_i,A_j)=1 holds for every pair (i, j) such that 1\leq i < j \leq N.

\{A_i\} is said to be setwise coprime when \{A_i\} is not pairwise coprime but GCD(A_1,\ldots,A_N)=1.

Determine if \{A_i\} is pairwise coprime, setwise coprime, or neither.

Here, GCD(\ldots) denotes greatest common divisor.

Constraints

2 \leq N \leq 10^6

1 \leq A_i\leq 10^6

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

If \{A_i\} is pairwise coprime, print pairwise coprime; if \{A_i\} is setwise coprime, print setwise coprime; if neither, print not coprime.

Sample Input 1

3
3 4 5

Sample Output 1

pairwise coprime

GCD(3,4)=GCD(3,5)=GCD(4,5)=1, so they are pairwise coprime.

Sample Input 2

3
6 10 15

Sample Output 2

setwise coprime

Since GCD(6,10)=2, they are not pairwise coprime. However, since GCD(6,10,15)=1, they are setwise coprime.

Sample Input 3

3
6 10 16

Sample Output 3

not coprime

GCD(6,10,16)=2, so they are neither pairwise coprime nor setwise coprime.


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define forin(in) for(int i=0; i<(int)in.size(); i++) cin>>in[i]
#define forout(out) for(int i=0; i<(int)out.size(); i++) cout<<out[i]<<endl
#define rep(i, n) for(int i=0; i<(n); i++)

bool pairwise = true;
vector<bool> seen(1e+6, false);

vector<pair<int, int> > prime_factorize(int n) {
    vector<pair<int, int> > res;
    for (int p = 2; p * p <= n; ++p) {
        if (n % p != 0) continue;
        int num = 0;
        while (n % p == 0) { ++num; n /= p; }
        if(seen[p - 1]) pairwise = false;
        res.push_back(make_pair(p, num)); seen[p - 1] = true;
    }
    if (n != 1) res.push_back(make_pair(n, 1));
  		if(seen[n-1]){
          pairwise=false;
        }
  		seen[n - 1] = true;
    return res;
}

int gcd(int a, int b) {
    if (b==0) return a;
    else return gcd(b, a%b);
}

int main(){
    int N; cin>> N;
    vector<int> A(N);
    bool setwise = true;

    cin>> A[0];
    int cur = A[0];

    for(int i = 1; i < N; i++){
        cin>> A[i];

        cur = gcd(cur, A[i]);
    }

    //setwise
    if(cur != 1) setwise = false;

    //pairwise
    rep(i, N){
        prime_factorize(A[i]);
    }

    //result
    if(pairwise){
        cout<< "pairwise coprime" << endl;
        return 0;
    }
    if(setwise){
        cout<< "setwise coprime" << endl;
        return 0;
    }
    cout<< "not coprime" << endl;
    return 0;
}


// https://atcoder.jp/contests/abc177/tasks/abc177_e

#include "algorithm"
#include "bitset"
#include "cmath"
#include "functional"
#include "iomanip"
#include "iostream"
#include "map"
#include "numeric"
#include "queue"
#include "string"
#include "unordered_set"
#include "vector"
#define rep(i, to) for (ll i = 0; i < (to); ++i)
#define rep1(i, to) for (ll i = 1; i <= (to); ++i)
#define repf(i, from, to) for (ll i = from; i < (to); ++i)
#define repr(i, from) for (ll i = from - 1; i >= 0; --i)
#define all(vec) vec.begin(), vec.end()
#define unless(cond) if (!(cond))
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
template <typename T>
using V = vector<T>;
using VL = V<ll>;
template <typename T>
using VV = V<V<T>>;
using VVL = VV<ll>;
template <typename T>
using VVV = VV<V<T>>;
template <typename T, typename U>
using P = pair<T, U>;
using PL = P<ll, ll>;
using VPL = V<PL>;
template <typename T>
using asc_pq = priority_queue<T, V<T>, greater<T>>;

template <typename T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T>
inline ll len(V<T> arr) {
  return arr.size();
}

struct exit_exception : public std::exception {
  const char* what() const throw() { return "Exited"; }
};

template <typename T>
void drop(T res) {
  cout << res << endl;
  throw exit_exception();
}

const ll INF = 1e18;

void solve();
void solve2();
void solve3();

#ifndef TEST
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  try {
    solve3();
  } catch (exit_exception& e) {
  }

  return 0;
}
#endif

string PC = "pairwise coprime";
string SC = "setwise coprime";
string NC = "not coprime";

VPL factorize(ll n) {
  VPL res;

  for (ll i = 2; i * i <= n; i++) {
    if (n % i != 0) continue;

    auto r = PL(i, 0);

    while (n % i == 0) {
      n /= i;
      r.second++;
    }

    res.push_back(r);
  }

  // sqrt(n) 以上の素因数は高々一つ
  if (n != 1) res.emplace_back(n, 1);

  return res;
}

void solve() {
  ll N;
  cin >> N;

  V<int> as(N);
  rep(i, N) cin >> as[i];

  auto is_pc = [&]() {
    unordered_set<int> facts;

    for (int a : as) {
      VPL fact = factorize(a);

      for (PL pa : fact) {
        auto [fact, _] = pa;

        if (facts.count(fact)) {
          return false;
        }

        facts.insert(fact);
      }
    }

    return true;
  };

  if (is_pc()) drop(PC);

  ll gcd_v = 0;
  rep(i, N) { gcd_v = gcd(gcd_v, as[i]); }

  if (gcd_v == 1) drop(SC);

  drop(NC);
}

void solve2() {
  ll N;
  cin >> N;

  V<int> as(N);
  rep(i, N) cin >> as[i];

  auto is_pc = [&]() {
    int a_max = *max_element(all(as));
    VL cs(a_max + 1);

    rep(i, N) cs[as[i]]++;

    repf(fact, 2, a_max + 1) {
      ll ct = 0;

      for (int num = fact; num <= a_max; num += fact) {
        ct += cs[num];
      }

      if (ct > 1) return false;
    }

    return true;
  };

  if (is_pc()) drop(PC);

  ll gcd_v = 0;
  rep(i, N) { gcd_v = gcd(gcd_v, as[i]); }

  if (gcd_v == 1) drop(SC);

  drop(NC);
}

void solve3() {
  ll N;
  cin >> N;

  V<int> as(N);
  rep(i, N) cin >> as[i];

  int a_max = *max_element(all(as));
  VL min_factor(a_max + 1);

  repf(fact, 2, a_max + 1) {
    if (min_factor[fact] > 0) continue;

    for (int num = fact; num <= a_max; num += fact) {
      if (min_factor[num] == 0) min_factor[num] = fact;
    }
  }

  auto is_pc = [&]() {
    unordered_set<int> facts;

    for (int a : as) {
      while (a > 1) {
        if (min_factor[a] == 0) {
          if (facts.count(a)) return false;
          facts.insert(a);
          break;
        }

        int f = min_factor[a];
        if (facts.count(f)) return false;
        facts.insert(f);
        while (a % f == 0) a /= f;
      }
    }

    return true;
  };

  if (is_pc()) drop(PC);

  ll gcd_v = 0;
  rep(i, N) { gcd_v = gcd(gcd_v, as[i]); }

  if (gcd_v == 1) drop(SC);

  drop(NC);
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1001001001;
const ll mod = 1000000007;
const int MAX = 1000100;
/**
 * caution : set MAX to the proper value
 * caution : don't forget to use Sieve
 **/
bool Is_prime[MAX];
int MinFactor[MAX];
// Eratosthenes's Sieve
// O(NloglogN)
void Sieve(int n = MAX){
    for (int i = 0; i < n; ++i) Is_prime[i] = true, MinFactor[i] = -1;
    Is_prime[0] = false; Is_prime[1] = false;
    MinFactor[0] = 0; MinFactor[1] = 1;
    for (int i = 2; i * i < n; ++i) {
        if (Is_prime[i]) {
            MinFactor[i] = i;
            for (int j = i * i; j < n; j += i) {
                Is_prime[j] = false;
                if (MinFactor[j] == -1) MinFactor[j] = i;
            }
        }
    }
}
// Factorization O(logN)
vector<pair<int, int>> prime_factor(int n){
    vector<pair<int,int> > res;
    while (n != 1) {
        int prime = MinFactor[n];
        int exp = 0;
        while (MinFactor[n] == prime) {
            ++exp;
            n /= prime;
        }
        res.push_back(make_pair(prime, exp));
    }
    return res;
}

int gcd(int a, int b){return (b == 0 ? a : gcd(b, a % b));}

int main(){
    int n; cin >> n;
    Sieve();
    vector<int> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    int full = a[0];
    for(int i = 1; i < n; i++){
        full = gcd(a[i], full);
    }
    bool one = true;
    vector<int> d(1000001, 0);
    for(int i = 0; i < n; i++){
        auto pf = prime_factor(a[i]);
        for(auto p : pf){
            d[p.first]++;
        }
    }
    if(*max_element(d.begin(), d.end()) >= 2) one = false;
    if(one) cout << "pairwise coprime" << endl;
    else if(full == 1) cout << "setwise coprime" << endl;
    else cout << "not coprime" << endl;
}


#include <bits/stdc++.h>

#ifndef M_PI
#define M_PI 3.14159265358979
#endif
#define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
typedef long double ld;
typedef pair<long double, long double> pld;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

const ll MAX_A = 1001000;

ll gcd(ll n, ll m) {
    if(n > m) {
        ll l = n;
        n = m;
        m = l;
    }
    if(n == 0) {
        return m;
    }
    while(1) {
        ll l = n;
        n = m % n;
        m = l;
        if(n == 0) {
            break;
        }
    }
    return m;
}

bool isprime(ll n) {
    if(n == 1) {
        return false;
    }
    if(n == 2) {
        return true;
    }
    ll sq = (ll)sqrt(double(n));
    for(ll i = 2; i <= sq; i++) {
        if(n % i == 0) {
            return false;
        }
    }
    return true;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll i, j, k, l;

    ll N;
    cin >> N;
    vll A(N);
    for(auto &e : A)
        cin >> e;
    // setwise
    ll g = A[0];
    for(i = 1; i < N; i++) {
        g = gcd(g, A[i]);
    }
    if(g > 1) {
        cout << "not coprime" << endl;
        return 0;
    }
    // pairwise
    // D[x]はxを割り切る最小の素数
    vll D(MAX_A, 0);
    for(i = 2; i < D.size(); i++) {
        if(D[i] == 0) {
            ll p = i;
            while(p < D.size()) {
                D[p] = i;
                p += i;
            }
        }
    }
    set<ll> pset;
    for(i = 0; i < N; i++) {
        ll a = A[i];
        if(a == 1)
            continue;
        set<ll> pset_sub;
        while(D[a] != a) {
            auto itr = pset.find(D[a]);
            if(itr != pset.end()) {
                cout << "setwise coprime" << endl;
                return 0;
            }
            pset_sub.insert(D[a]);
            a = a / D[a];
        }
        for(auto p : pset_sub) {
            pset.insert(p);
        }
    }

    cout << "pairwise coprime" << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define P pair<ll, ll>
#define Graph vector<vector<ll>>
#define fi first
#define se second
constexpr ll mod = 1000000007;
constexpr ll INF = (1ll << 60);
constexpr double pi = 3.14159265358979323846;
template <typename T>
inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}

ll gcd(ll a, ll b) {
    if (b == 0) {
        return a;
    } else {
        return gcd(b, a % b);
    }
}

vector<ll> sum;
void pre_process(vector<ll> &a, vector<ll> &s) {  //累積和のvector
    ll n = (ll)a.size();
    s.assign(n + 1, 0);
    s[0] = a[0];
    for (ll i = 0; i < n; i++) {
        s[i + 1] = gcd(s[i], a[i]);
    }
}

vector<ll> prime_num(5000010, 0);
vector<bool> prime(5000010, true);
bool ans = false;
void isprime(ll N) {
    if (N >= 0) prime[0] = false;
    if (N >= 1) prime[1] = false;
    for (ll i = 2; i * i <= N; i++) {
        if (!prime[i]) {
            continue;
        }

        ll cnt = 0;
        cnt += prime_num[i];

        for (ll j = i * i; j <= N; j += i) {
            prime[j] = false;
            cnt += prime_num[j];
        }

        // if (i == 2) cout << cnt << endl;
        if (cnt >= 2) {
            ans = true;
            break;
        }
    }
}

int main() {
    ll n;
    cin >> n;
    vector<ll> a(n);
    bool ans1 = false;
    ll number = 1;
    rep(i, n) {
        cin >> a[i];

        if (i == 0)
            number = a[i];
        else
            number = gcd(number, a[i]);

        prime_num[a[i]]++;
    }

    isprime(5000000);
    if (number == 1) {  // gcd=1;

        if (ans == true) {
            cout << "setwise coprime\n";
        } else {
            cout << "pairwise coprime\n";
        }
    } else {
        cout << "not coprime\n";
    }

    return 0;
}


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

const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  COMinit();
  long long int N;
  cin >> N;
  vector<long long int> A(N);
  for (int i = 0; i < N; i++) cin >> A.at(i);
  long long int G = A.at(0), S = 1, L = 1, MOD = 1000000007;
  for (int i = 0; i < N; i++) {
    G = __gcd(G, A.at(i));
    S = (S * A.at(i)) % MOD;
    L = (L * A.at(i) % MOD * inv[__gcd(L, A.at(i))]) % MOD;
  }
  if (G == 1 && S == L) cout << "pairwise coprime" << endl;
  else if (G == 1) cout << "setwise coprime" << endl;
  else cout << "not coprime" << endl;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;

#define REP(i, n, m) for(ll i=n; i<(ll)m; ++i)
#define IREP(i, n, m) for(ll i=n-1; i>=m; --i)
#define rep(i, n) REP(i, 0, n)
#define irep(i, n) IREP(i, n, 0)
#define all(v) v.begin(), v.end()
#define vprint(v) for(auto e:v){cout<<e<<" ";};cout<<endl;
#define vvprint(vv) for(auto v:vv)vprint(v);

ll gcd(ll n, ll m){
    while(true){
        if(n<m) swap(n, m);
        if(!m) break;
        n %= m;
    }
    return n;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(20);

    ll N;
    cin >> N;
    vll A(N);
    rep(i, N) cin >> A[i];

    vector<bool> prime(1000001, true);
    prime[0] = prime[1] = false;
    rep(i, 1000001){
        if(prime[i]){
            for(ll j=i+i; j<1000001; j+=i){
                prime[j] = false;
            }
        }
    }

    bool pc = true;
    vector<bool> appeared(1000001, false);
    rep(i, N){
        if(A[i]>=2 && appeared[A[i]]){
            pc = false;
            break;
        }
        appeared[A[i]] = true;
    }
    vll B;
    rep(i, N){
        if(!prime[i]) B.push_back(A[i]);
    }
    ll M = B.size();
    REP(i, 2, 500001){
        if(!prime[i]) continue;
        bool flag = false;
        bool valid = true;
        rep(j, M){
            if(B[j]%i==0){
                if(flag){
                    valid = false;
                    break;
                }
                else flag = true;
            }
        }
        if(!valid){
            pc = false;
            break;
        }
    }

    if(pc) cout << "pairwise coprime" << endl;
    else{
        ll g = A[0];
        REP(i, 1, N) g = gcd(g, A[i]);
        if(g==1) cout << "setwise coprime" << endl;
        else cout << "not coprime" << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define pb 		push_back
#define mp 		make_pair
#define pii 	pair<int,int>
#define vi 		vector<int>
#define vii 	vector<vector<int>>;
#define vpii 	vector<pii>
#define ar 		array
#define SZ(x) 	((int)(x.size()))
#define fi 		first
#define se 		second

#define TRvi(a,it)   for(auto (it)=(a).begin(),it!=(a).end();it++)
#define FORV (a,x)   for(auto (x):(a))
#define FOR(i,n)	 for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) 	for(int (i)=1;(i)<=(n);++(i))
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)

#define IN(x,y) 	((y).find((x))!=(y).end())
#define ALL(t) 		t.begin(),t.end()

#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define DBG cerr << "debug here" << endl;
#define DBGV(vari) cerr << #vari<< " = "<< (vari) <<endl;
#define all(x) (x).begin(), (x).end()
#define str(s) to_string(s);

typedef long long ll;

const int INF = 1e9;

const int MINV = 1;
const int MAXV = 10;
#define M (ll)1000000007

    long long mod(long long x){
        return (x%M);
    }
    long long add(long long a, long long b){
        return mod(mod(a)+mod(b));
    }
    long long mul(long long a, long long b){
        return mod(mod(a)*mod(b));
    }
    int temp[1000][1000]={-1};
    int gcd(int a, int b)
    {
        if (a == 0)
            return b;
        if(temp[a][b]!=-1)return temp[a][b];
        return temp[a][b]=gcd(b % a, a);
    }

    int findgcd(vi &arr)
{
    int n=arr.size();
    int result = arr[0];
    bool flag=false;
    for (int i = 1; i < n; i++)
    {

        result = gcd(arr[i], result);

        if(result == 1)
        {
           return 1;
        }
    }
    return result;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	ll n;
	cin>>n;
	vi A(n);for(auto &x:A)cin>>x;

	int i=findgcd(A);
	if(i!=1){
	    cout<<"not coprime"<<endl;

	}else{
	    for(int i=0;i<n-1;i++){
	        for(int j=i+1;j<n;j++){
	            if(gcd(A[i],A[j])!=1){
	                cout<<"setwise coprime"<<endl;
	                return 0;
	            }
	        }
	    }
	    cout<<"pairwise coprime"<<endl;

	}

  return 0;
}


/****************************************

			LostMartian

****************************************/
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <functional>
#include <utility>
#include <climits>
#include <numeric>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <cassert>
using namespace std;
typedef long long ll;

template <typename Arg1>
void pn(Arg1&& arg1) {
	cout << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void pn(Arg1&& arg1, Args&&... args) {
	cout << arg1 << " ";
	pn(args...);
}
template <typename Arg1>
void ps(Arg1&& arg1) {
	cout << arg1 << " ";
}
template <typename Arg1, typename... Args>
void ps(Arg1&& arg1, Args&&... args) {
	cout << arg1 << " ";
	ps(args...);
}
template <typename Arg1>
void read(Arg1&& arg1) {
	cin >> arg1;
}
template <typename Arg1, typename... Args>
void read(Arg1&& arg1, Args&&... args) {
	cin >> arg1;
	read(args...);
}

#define rep(i,m,n) for(ll i=m; i<n; i++)
#define repr(i,m,n) for(ll i=m; i>=n; i--)
#define vll vector<ll >
#define vvl vector<vector<ll > >
#define pi pair<ll, ll >
#define vpi vector<pi >
#define umap unordered_map<ll, ll >
#define pb push_back
#define mk make_pair
#define sz size()
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define test ll t; cin>>t; while(t--)
#define ini(a, x) memset(a, x, sizeof(a))
#define d(x) cout<<"Value of "#x" = "<<x<<"\n";
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define iAmSpeed ios_base::sync_with_stdio(false); cin.tie(NULL);
void LOCAL();
#define SZ 1000005
ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
bool coprime(ll a, ll b)
{
    return (gcd(a, b) == 1);
}
ll numOfPairs(ll arr[], ll n)
{

    ll count = 0;
    for (ll i = 0; i < n - 1; i++)
        for (ll j = i + 1; j < n; j++)
            if (coprime(arr[i], arr[j]))
                count++;

    return count;
}
ll findGCD(ll arr[], ll n)
{
    ll result = arr[0];
    for (ll i = 1; i < n; i++)
    {
        result = gcd(arr[i], result);

        if(result == 1)
        {
           return 1;
        }
    }
    return result;
}
int main()
{
	clock_t z = clock();
	//LOCAL();
	iAmSpeed

	ll n;
	cin>>n;
	ll ar[n];
	rep(i,0,n) {
		cin>>ar[i];
	}
	ll pairs=((n-1)*n)/2;
	if(numOfPairs(ar, n)!=pairs) {
		if(findGCD(ar, n)==1) {
			pn("setwise coprime");
			return 0;
		}
		pn("not coprime");
		return 0;
	}
	pn("pairwise coprime");

	debug("Total Time: %.7f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
	return 0;
}
void LOCAL() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}


/*-- ILSH  --*/
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pop pop_back
#define mp make_pair
#define vii vector < ll >
#define vll vector < ll >
#define dt cout<<"HERE\n";
#define pii pair < ll , ll >
#define pll pair < ll , ll >
#define vpi vector < pii >
#define vpl vector < pll >
#define fi first
#define se second
#define sz size
#define len length
const ll inf=1e18+1e17;
const ll moda =29;
const ll modb =34483;
//const ll mod = 998244353;

ll modInverse(ll a,ll m){ll m0=m;ll y=0,x=1;if(m == 1)return 0;while(a> 1){ll q=a/m;ll t=m;m=a%m,a=t;t=y;y=x-q*y;x=t;}if(x<0)x+=m0;return x;}
/*ll powm(ll a,ll b){a=a%mod;ll res=1;while(b){if(b&1)res=(res*a)%mod;a=(a*a)%mod;b>>=1;}return (res%mod);}*/
const ll N =1e6+6;
int arr[N];
void solve(){
	int n;
	cin>>n;
	memset( arr,0,sizeof ( arr));
	bool va=0;
	int gc=0;
	for ( int i=0;i<n;i++){
		int xx;
		cin>>xx;
		if ( gc==0)
			gc=xx;
		else
			gc=__gcd( gc,xx);
		/*if ( arr[xx])
			va=1;*/
		arr[xx]=1;
	}
	for ( int i=2;i<N;i++){
		int cnt=0;
		for ( int j=i;j<N;j+=i)
			cnt+=arr[j];
		if ( cnt>=2)
			va=1;
	}
	if ( n> 79000){
		va=1;
	}
	if ( va){
		if ( gc==1)
			cout<<"setwise coprime\n";
		else
			cout<<"not coprime\n";
	}
	else
		cout<<"pairwise coprime\n";

}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie( NULL);
	cout.tie(NULL);

	#ifndef ONLINE_JUDGE
		freopen ("INPUT.txt" , "r" , stdin);
		freopen ("OUTPUT.txt", "w" , stdout);
	#endif

	ll t=1;
	//cin>>t;
	for ( ll i=1;i<=t;i++){
		//cout<<"Case #"<<i<<": ";
    	solve();
    }
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>
#define rep(i, n)   for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

map<int, int>m;

void prime_fac(int x){
    if(x == 1)  return;
    for(int i = 2; x > 1; i++){
        if(x%i == 0){
            m[i]++;
            while(x%i == 0){
                x /= i;
            }
        }
    }
}

int main(){
    int n;  cin >> n;
    vector<int>a(n);
    rep(i, n)   cin >> a[i];
    rep(i, n)   prime_fac(a[i]);
    int flag = 0;
    for(int i = 2; i <= *max_element(a.begin(), a.end()); i++){
        flag = max(flag, m[i]);
    }
    if(flag == 1){
        puts("pairwise coprime");
    }else if(flag == n){
        puts("not coprime");
    }else{
        puts("setwise coprime");
    }
}


#include<algorithm>
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<cmath>
#include<list>
#include<set>
#include<map>

using namespace std;
long long MOD = 1000000007LL;
const double PI = 3.14159265358979323846;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define endl "\n"

template<class T>
bool prime_factorization(T n, map<int, int>& ret, map<int, int> &ret2) {
	bool flags = false;
	map<int, int> tmp;
	for (long long i = 2; i * i <= n; ++i) {
		while (n % i == 0) {
			if (ret[i] != 0)flags = true;
			tmp[i]++;
			n /= i;
		}
	}

	if (n != 1) {
		if (ret[n] != 0)flags = true;
		tmp[n] += 1;
	}

	for (auto x : tmp) {
		ret[x.first] += x.second;
		ret2[x.first]++;
	}

	return flags;
}

int main() {
	int N;
	cin >> N;
	vector<long long> A;
	map<int, int> mp;
	map<int, int> mp2;
	bool flags = false;
	map<int, int> _A;
	for (int i = 0; i < N; ++i) {
		int a;
		cin >> a;
		_A[a] = 1;
	}
	for (auto x : _A) {
		A.push_back(x.first);
	}

	sort(A.begin(), A.end());
	for (int i = 0; i < A.size(); ++i) {
		flags = max(flags, prime_factorization(A[i], mp, mp2));
	}

	if (flags) {
		bool flags2 = false;
		for (auto x : mp2) {
			//cout << x.first << " " << (x.second == N) << endl;
			if (x.second == A.size()) {
				flags2 = true;
				break;
			}
		}
		if (flags2) {
			cout << "not coprime" << endl;
		}
		else {
			cout << "setwise coprime" << endl;
		}
	}
	else {
		cout << "pairwise coprime" << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define all(x) x.begin(),x.end()
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
#define PI 3.1415926535897932384626
#define mod 1000000007
#define modd 998244353

void io() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
}

int main() {
	io();

	int n;
	cin>>n;
	ll a;
	cin>>a;
	ll lcm = a, gcd = a, pro = a;
	for(int i=1;i<n;i++) {
		cin>>a;
		pro*=a;
		gcd = __gcd(gcd,a);
		lcm = lcm*a/__gcd(lcm,a);
	}
	if(lcm == pro) cout<<"pairwise coprime";
	else if(gcd == 1) cout<<"setwise coprime";
	else cout<<"not coprime";
	return 0;
}


#include <bits/stdc++.h>

signed main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    int n;
    std::cin >> n;

    std::vector<int> a(n);
    for (int &i : a) std::cin >> i;

    int gcd = 0;
    for (int i : a) gcd = std::gcd(gcd, i);

    if (gcd != 1) {
        std::cout << "not coprime";
        return 0;
    }

    int mulOf2 = 0;
    for (int i : a) mulOf2 += (i % 2 == 0);

    if (mulOf2 > 1) {
        std::cout << "setwise coprime";
        return 0;
    }

    for (int i = 3; i <= (int) 1e6; i += 2) {
        int mulOfi = 0;
        for (int x : a) mulOfi += (x % i == 0);
        if (mulOfi > 1) {
            std::cout << "setwise coprime";
            return 0;
        }
    }

    std::cout << "pairwise coprime";

    return 0;
}


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

template <typename T>
vector<pair<T, int>> prime_factorization(T val) {
  vector<pair<T, int>> res;
  for (T i = 2; i * i <= val; ++i) {
    if (val % i != 0) continue;
    int exponent = 0;
    while (val % i == 0) {
      ++exponent;
      val /= i;
    }
    res.emplace_back(i, exponent);
  }
  if (val != 1) res.emplace_back(val, 1);
  return res;
}

vector<bool> sieve_of_eratosthenes(int val) {
  vector<bool> res(val + 1, true);
  res[0] = false;
  if (val >= 1) res[1] = false;
  for (int i = 2; i * i <= val; ++i) {
    if (res[i]) {
      for (int j = i * i; j <= val; j += i) res[j] = false;
    }
  }
  return res;
}

int main() {
  int n; cin >> n;
  vector<int> a;
  REP(i, n) {
    int ai; cin >> ai;
    if (ai > 1) a.emplace_back(ai);
  }
  n = a.size();
  if (n <= 78498) {
    vector p(1000001, false);
    bool is_p = true;
    REP(i, n) {
      for (auto [k, v] : prime_factorization(a[i])) {
        if (p[k]) {
          is_p = false;
          break;
        }
        p[k] = true;
      }
      if (!is_p) break;
    }
    if (is_p) {
      cout << "pairwise coprime\n";
      return 0;
    }
  }
  int g = a[0];
  FOR(i, 1, n) g = gcd(g,  a[i]);
  cout << (g == 1 ? "setwise coprime\n" : "not coprime\n");
  return 0;
}


#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <cassert>
#include <random>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
typedef long long int ll;
const ll INF = 10000000000;
const double PI = acos(-1);
const ll mod = 1000000007;

typedef pair<int, int> P;
vector<int> h_idx;
vector<int> w_idx;

int main()
{
    int H, W, M;
    cin >> H >> W >> M;
    set<P> s;
    vector<int> row(H);
    vector<int> col(W);
    rep(i, M) {
        int h, w;
        cin >> h >> w;
        h--;
        w--;
        row[h]++;
        col[w]++;
        s.insert(make_pair(h, w));
    }

    int max_row = 0;
    int max_col = 0;
    rep(i, H) max_row = max(max_row, row[i]);
    rep(i, W) max_col = max(max_col, col[i]);

    rep(i, H) {
        if (row[i] == max_row) h_idx.push_back(i);
    }
    rep(i, W) {
        if (col[i] == max_col) w_idx.push_back(i);
    }

    int ans = max_row + max_col;
    for (auto i : h_idx) {
        for (auto j : w_idx) {
            if (s.count(make_pair(i, j)) == 0) {
                cout << ans << endl;
                return 0;
            }
        }
    }

    ans--;
    cout << ans << endl;
    return 0;
}

Score : 500 points

Problem Statement

We have a two-dimensional grid with H \times W squares. There are M targets to destroy in this grid - the position of the i-th target is \left(h_i, w_i \right).

Takahashi will choose one square in this grid, place a bomb there, and ignite it. The bomb will destroy all targets that are in the row or the column where the bomb is placed. It is possible to place the bomb at a square with a target.

Takahashi is trying to maximize the number of targets to destroy. Find the maximum number of targets that can be destroyed.

Constraints

All values in input are integers.

1 \leq H, W \leq 3 \times 10^5

1 \leq M \leq \min\left(H\times W, 3 \times 10^5\right)

1 \leq h_i \leq H

1 \leq w_i \leq W

\left(h_i, w_i\right) \neq \left(h_j, w_j\right) \left(i \neq j\right)

Input

Input is given from Standard Input in the following format:

H W M
h_1 w_1
\vdots
h_M w_M

Output

Print the answer.

Sample Input 1

2 3 3
2 2
1 1
1 3

Sample Output 1

3

We can destroy all the targets by placing the bomb at \left(1, 2\right).

Sample Input 2

3 3 4
3 3
3 1
1 1
1 2

Sample Output 2

3

Sample Input 3

5 5 10
2 5
4 3
2 3
5 5
2 2
5 4
5 3
5 1
3 5
1 4

Sample Output 3

6


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define MOD ll(1e9+7)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cerr<<#x<<": "<<x<<endl

int main() {
    ll H, W, m;
    cin >> H >> W >> m;

    vector<ll> h(m), hcnt(H, 0);
    vector<ll> w(m), wcnt(W, 0);
    for(int i = 0; i < m; i++){
        cin >> h[i] >> w[i];
        h[i]--; w[i]--;
        hcnt[h[i]]++; wcnt[w[i]]++;
    }
    ll hmax = 0;
    ll wmax = 0;
    for(int i = 0; i < H; i++){
        hmax = max(hmax, hcnt[i]);
    }
    for(int i = 0; i < W; i++){
        wmax = max(wmax, wcnt[i]);
    }
    vector<ll> hmax_index;
    vector<ll> wmax_index;
    for(int i = 0; i < H; i++){
        if(hmax == hcnt[i]){hmax_index.push_back(i);}
    }
    for(int i = 0; i < W; i++){
        if(wmax == wcnt[i]){wmax_index.push_back(i);}
    }
    ll hcandidate_num = hmax_index.size();
    ll wcandidate_num = wmax_index.size();

    if(hcandidate_num * wcandidate_num > m){
        cout << hmax + wmax << endl;
    }else{
        set<pll> st;
        for(int i = 0; i < m; i++){
            st.insert({h[i],w[i]});
        }
        bool is_cross = true;
        for(int i = 0; i < hcandidate_num; i++){
            for(int j = 0; j < wcandidate_num; j++){
                if(st.find({hmax_index[i],wmax_index[j]}) == st.end()){
                    is_cross = false;
                }
            }
        }
        if(is_cross){
            cout << hmax + wmax - 1 << endl;
        }else{
            cout << hmax + wmax << endl;
        }
    }

    return 0;
}


#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#define F first
#define S second
#define vec vector
#define pb push_back
#define pll pair<ll,ll>
#define pdd pair<ld, ld>
#define umap unordered_map
#define uset unordered_set
#define pii pair<int, int>
#define pnn pair<Node*, Node*>
#define all(m) m.begin(), m.end()
#define uid uniform_int_distribution
#define init(m, x) memset(m, x, sizeof(m));
#define FILE ifstream in("input.txt");ofstream out("output.txt");
#define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);
using namespace std;
typedef string str;
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

int main() {
    fast;
    int a,b,k; cin>>a>>b>>k;
    pii m[k];
    int rw[a], cl[b];
    uset<int> srw[a];
    init(rw, 0); init(cl, 0);
    for(int q=0; q<k; q++){
        int x,y; cin>>x>>y; x--, y--;
        m[q] = {x, y};
        rw[x]++;
        srw[x].insert(y);
        cl[y]++;
    }
    int mxr = *max_element(rw, rw+a);
    int mxc = *max_element(cl, cl+a);
    int o = mxr+mxc-1;
    vec<int> big;
    for(int q=0; q<b; q++){
        if(cl[q]==mxc) big.pb(q);
    }
    for(int q=0; q<a; q++){
        if(rw[q]!=mxr) continue;
        for(int i : big){
            if(!srw[q].count(i)){
                cout<<o+1;
                return 0;
            }
        }
    }
    cout<<o;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i=0; i < n; i++)
#define repd(i, n) for (int i = n-1; i > -1; i--)
#define repran(i, a,b) for (int i = a; i<b;i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> P;
int main()
{
    int h, w, m;
    cin >> h >> w >> m;
    vector<vector<int>> g(h, vector<int>(w, 0));
    vector<int> hs(h, 0), vs(w, 0);
    rep(i, m){
        int a, b;
        cin >> a >> b;
        --a;--b;
        g[a][b] =1;
    }
    rep(i, h){
        rep(j, w){
            hs[i] += g[i][j];
            vs[j] += g[i][j];
        }
    }

    int ans = 0;
    int tmp = 0;
    rep(i, h) rep(j, w){
        tmp = vs[j]+hs[i];
        if (g[i][j] == 1) tmp -=1;
        ans = max(ans, tmp);
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;
template <typename T> using vec = std::vector<T>;

int main() {
    ll H, W, M;
    cin >> H >> W >> M;

    ll maxh = 0;
    ll maxw = 0;
    set<pair<int, int>> st;

    vec<ll>  hb(H), wb(W);
    rep(i, M) {
        int h,w;
        cin >> h >> w;
        --h, --w;
        ++hb[h], ++wb[w];
        maxh = max(maxh, hb[h]);
        maxw = max(maxw, wb[w]);
        st.insert({h, w});
    }
    vec<ll> maxhs, maxws;
    rep(i, H) if (hb[i] == maxh) maxhs.push_back(i);
    rep(i, W) if (wb[i] == maxw) maxws.push_back(i);
    for (auto e : maxhs) {
        for (auto j : maxws) {
            if (st.count({e, j}) == 0) {
                cout << maxh + maxw << endl;
                return 0;
            }
        }
    }
    cout << maxh + maxw - 1 << endl;
}


#include <iostream>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <cctype>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <stdlib.h>
#include <tuple>
#include <iomanip>
#include <iterator>
#include <stdio.h>
#include <fstream>
#include <time.h>
#include <bit>
#include <bitset>
#include <cstdint>
using namespace std;
const double PI = 3.14159265358979323846;
const long long int MOD = 1000000000 + 7;
using ll = long long;

struct UnionFind {
    vector<int> parent;

    UnionFind(int N) : parent(N) {
        for (int i = 0; i < N; i++) {
            parent[i] = -1;
        }
    }

    int root(int i) {
        if (parent[i] < 0) {
            return i;
        }
        return (parent[i] = root(parent[i]));
    }

    bool unite(int from, int to) {
        int rx = root(from);
        int ry = root(to);
        if (rx != ry) {
            parent[ry] += parent[rx];
            parent[rx] = ry;

            return true;
        }
        else {
            return false;
        }
    }

    bool same(int x, int y) {
        return root(x) == root(y);
    }

    int treeSize(int x) {

        return -parent[root(x)];
    }
};

long long int modpow(long long int base, long long int pow, long long int mod) {
    if (pow == 1) {
        return base;
    }
    else if (pow == 0) {
        return 1;
    }

    if (pow % 2 == 0) {
        auto temp = modpow(base, pow / 2, mod);
        return (temp * temp) % mod;
    }
    else {
        return (base * modpow(base, pow - 1, mod)) % mod;
    }
}

long long int moddiv(long long int X, long long int Y, long long int mod) {
    auto fermatDiv = modpow(Y, mod - 2, mod);

    return (X * fermatDiv) % mod;
}

long long modCombination(long long left, long long right, long long int mod) {
    long long answer = 1;
    if (left > right) {
        for (long long i = 0; i < right; i++) {
            answer = (answer * (left - i)) % mod;
            answer = moddiv(answer, (i + 1), mod);
        }
    }
    return answer;
}

bool IsPrime(long long N) {
    if (N == 1) {
        return false;
    }
    for (long long i = 2; i * i <= N; i++) {
        if (N % i == 0) {
            return false;
        }
    }

    return true;
}

vector<pair<long long, long long> > prime_factorize(long long N) {
    vector<pair<long long, long long> > res;
    for (long long a = 2; a * a <= N; ++a) {
        if (N % a != 0) continue;
        long long ex = 0; // 指数

        // 割れる限り割り続ける
        while (N % a == 0) {
            ++ex;
            N /= a;

        }

        // その結果を push
        res.push_back({ a, ex });
    }

    // 最後に残った数について
    if (N != 1) res.push_back({ N, 1 });
    return res;
}

vector<long long> enum_divisors(long long N) {
    vector<long long> res;
    for (long long i = 1; i * i <= N; ++i) {
        if (N % i == 0) {
            res.push_back(i);
            // 重複しないならば i の相方である N/i も push
            if (N / i != i) res.push_back(N / i);
        }
    }
    // 小さい順に並び替える
    sort(res.begin(), res.end());
    return res;
}

long long gcd(long long a, long long b) {
    if (b > a) {
        long long temp = b;
        b = a;
        a = temp;
    }
    //cout << "a:" << a << "b:" << b << endl;
    long long c = a % b;
    if (c == 0) {
        return b;
    }
    else {
        return gcd(b, c);
    }
}

int main() {

    /*ll H, W;
    cin >> H >> W;
    ll Ch, Cw;
    cin >> Ch >> Cw;
    Ch--;
    Cw--;
    ll Dh, Dw;
    cin >> Dh >> Dw;
    Dh--;
    Dw--;

    vector<vector<char>> s(H, vector<char>(W));
    UnionFind unionFind(H * W);
    for (ll i = 0; i < H; i++) {
        for (ll j = 0; j < W; j++) {
            char c;
            cin >> c;

            s[i][j] = c;
            if (j > 0) {
                if (s[i][j - 1] == '.') {
                    unionFind.unite(i * W + j, i * W + j - 1);
                }
            }
            if (i > 0) {
                if (s[i - 1][j] == '.') {
                    unionFind.unite(i * W + j, (i - 1) * W + j);
                }
            }
        }
    }

    if (unionFind.same(Ch * W + Cw, Dh * W + Dw)) {
        cout << 0 << endl;
        return 0;
    }

    vector<int> unionNodeTable;

    for (ll i = 0; i < H; i++) {
        for (ll j = 0; j < W; j++) {

            if (s[i][j] == '.' &&
                find(unionNodeTable.begin(), unionNodeTable.end(), unionFind.root(i * W + j)) == unionNodeTable.end()) {
                unionNodeTable.push_back(unionFind.root(i * W + j));
            }
        }
    }

    vector<vector<bool>> graph(unionNodeTable.size(), vector<bool>(unionNodeTable.size()));

    for (ll i = 0; i < H; i++) {
        for (ll j = 0; j < W; j++) {

            if (s[i][j] == '.') {
                for (ll di = -2; di <= 2; di++) {
                    for (ll dj = -2; dj <= 2; dj++) {
                        ll y = i + di, x = j + dj;

                        if (y < 0 || y >= H || x < 0 || x >= W) {
                            continue;
                        }

                        if (s[y][x] == '.') {
                            int root1 = unionFind.root(i * W + j);
                            int root2 = unionFind.root(y * W + x);

                            auto itr1 = find(unionNodeTable.begin(), unionNodeTable.end(), root1);
                            int index1 = distance(unionNodeTable.begin(), itr1);

                            auto itr2 = find(unionNodeTable.begin(), unionNodeTable.end(), root2);
                            int index2 = distance(unionNodeTable.begin(), itr2);

                            graph[index1][index2] = true;
                            graph[index2][index1] = true;
                        }
                    }
                }
            }
        }
    }

    vector<int> dist(unionNodeTable.size(), -1);
    queue<int> que;

    int startRoot = unionFind.root(Ch * W + Cw);
    auto startItr = find(unionNodeTable.begin(), unionNodeTable.end(), startRoot);
    int startIndex = distance(unionNodeTable.begin(), startItr);
    que.push(startIndex);
    dist[startIndex] = 0;

    while (!que.empty()) {
        int v = que.front();
        que.pop();

        for (int i = 0; i < unionNodeTable.size(); i++) {
            if (graph[v][i]) {
                if (dist[i] != -1) {
                    continue;
                }

                dist[i] = dist[v] + 1;
                que.push(i);
            }
        }
    }

    int goalRoot = unionFind.root(Dh * W + Dw);
    auto goalItr = find(unionNodeTable.begin(), unionNodeTable.end(), goalRoot);
    int goalIndex = distance(unionNodeTable.begin(), goalItr);

    cout << dist[goalIndex] << endl;*/

    ll H, W, M;
    cin >> H >> W >> M;

    vector<ll> hCounts(H + 1, 0);
    vector<ll> wCounts(W + 1, 0);

    vector<vector<bool>> field(H + 1, vector<bool>(W + 1, false));
    vector<pair<ll, ll>> items;
    for (ll i = 0; i < M; i++) {
        ll h, w;
        cin >> h >> w;
        hCounts[h]++;
        wCounts[w]++;

        field[h][w] = true;

        items.emplace_back(h, w);
    }

    ll hMaxIndex = 0;
    ll hMax = 0;
    vector<ll> hMaxIndices;
    for (ll i = 0; i <= H; i++) {
        if (hCounts[i] > hMax) {
            hMaxIndex = i;
            hMax = hCounts[i];
            hMaxIndices = vector<ll>();
            hMaxIndices.push_back(i);
        }
        else if (hCounts[i] == hMax) {
            hMaxIndices.push_back(i);
        }
    }

    ll wMaxIndex = 0;
    ll wMax = 0;
    vector<ll> wMaxIndices;
    for (ll i = 0; i <= W; i++) {
        if (wCounts[i] > wMax) {
            wMaxIndex = i;
            wMax = wCounts[i];
            wMaxIndices = vector<ll>();
            wMaxIndices.push_back(i);
        }
        else if (wCounts[i] == wMax) {
            wMaxIndices.push_back(i);
        }
    }

    ll crossItemsCount = 0;
    for (auto p : items) {
        auto hItr = binary_search(hMaxIndices.begin(), hMaxIndices.end(), p.first);
        auto wItr = binary_search(wMaxIndices.begin(), wMaxIndices.end(), p.second);
        if (hItr && wItr) {
            crossItemsCount++;
        }
    }

    ll crossCount = hMaxIndices.size();
    crossCount *= wMaxIndices.size();
    if (crossItemsCount == crossCount) {
        cout << hMax + wMax - 1 << endl;
    }
    else {
        cout << hMax + wMax << endl;
    }

    return 0;
}


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

typedef long long int ll;
typedef unsigned long long int ull;

typedef vector <int> vi;
typedef vector <ll> vll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

typedef vector < pii > vpii;
typedef vector < pll > vpll;

typedef vector <string> vs;
typedef vector < vi > vvi;
typedef vector < vll > vvll;

#define fl(i, a, b)    for (int i(a); i <= (b); i ++)
#define rep(i, n)        fl (i, 1, (n))
#define loop(i, n)        fl (i, 0, (n) - 1)
#define rfl(i, a, b)    for (int i(a); i >= (b); i --)
#define rrep(i, n)        rfl (i, (n), 1)

#define all(v)     (v).begin(), (v).end()
#define srt(v)    sort (all (v))

#define pb push_back
#define mp make_pair

#define sz(x) ((int) (x).size())
#define fill (x, y)    memset(x, y, sizeof(x))
#define clr(a)    fill(a, 0)

#define PI 3.14159265358979323

const int Maxn = 1e5 + 1, Mod = 1e9 + 7;
#define trace1(x1)                    cerr << #x1 << ": " << x1 << endl;
#define trace2(x1, x2)                cerr << #x1 << ": " << x1 << " | " << #x2 << ": " << x2 << endl;
#define trace3(x1, x2, x3)            cerr << #x1 << ": " << x1 << " | " << #x2 << ": " << x2 << " | " << #x3 << ": " << x3 << endl;

#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)

const ll MOD = 1000000007LL;
const ll MAX = 100010LL;

// #define endl '\n'

// template <typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b);}
// template <typename T> T power(T x, T y, ll m = MOD) { T ans = 1; x %= m; while (y > 0) { if (y & 1ll) ans = (ans * x) % m; y >>= 1ll; x = (x * x)%m; } return ans%m;}

// template<typename T, typename T1>
// T mod(T x, T1 p) {
//     x %= p;
//     if (x < 0)
//         x += p;
//     return x;
// }

// int inv[Maxn], ifact[Maxn];
// template<typename T>
// T inverse(T x, T p)
// {
//     x = mod(x, p);
//     if (x == 1)
//         return x;
//     return mod((1LL * (-p / x) * (inv[p % x] % p)) , p);
//     // Since inverse of p % x is already calculated.
// }

// ifact[0] = 1;
// for(int i = 1; i < Maxn; i++)
// {
//     inv[i] = inverse(i, Mod);
//     ifact[i] = (1LL * ifact[i - 1] * inv[i]) % Mod;
// }

// int fact[Maxn];
// fact[0] = 1;
// for(int i = 1; i < Maxn; i++)
// {
//     fact[i] = 1LL * fact[i - 1] * i % Mod;
// }

// int n, t[4*MAXN];
// void build(int a[], int v, int tl, int tr) {
//     if (tl == tr) {
//         t[v] = a[tl];
//     } else {
//         int tm = (tl + tr) / 2;
//         build(a, v*2, tl, tm);
//         build(a, v*2+1, tm+1, tr);
//         t[v] = t[v*2] + t[v*2+1];
//     }
// }
// int sum(int v, int tl, int tr, int l, int r) {
//     if (l > r)
//         return 0;
//     if (l == tl && r == tr) {
//         return t[v];
//     }
//     int tm = (tl + tr) / 2;
//     return sum(v*2, tl, tm, l, min(r, tm))
//            + sum(v*2+1, tm+1, tr, max(l, tm+1), r);
// }
// void update(int v, int tl, int tr, int pos, int new_val) {
//     if (tl == tr) {
//         t[v] = new_val;
//     } else {
//         int tm = (tl + tr) / 2;
//         if (pos <= tm)
//             update(v*2, tl, tm, pos, new_val);
//         else
//             update(v*2+1, tm+1, tr, pos, new_val);
//         t[v] = t[v*2] + t[v*2+1];
//     }
// }

// int dif(string a, string b)
// {
// 	int cnt = 0;
// 	for(int i = 0 ; i < a.length() ; i++)
// 	{
// 		if(a[i]!=b[i])
// 			cnt++;
// 		if(cnt>1)
// 			return 0;
// 	}
// 	return 1;
// }

// void rv1(vector<int> &vec, int n) {
// 	vec = vector<int> (n);
// 	fl(i, 0, n-1) cin >> vec[i];
// 	return;
// }

// void pv1(vector<int> &vec) {
// 	int n = vec.size();
// 	if (n == 0) return;
// 	loop (i, n) cout << vec[i] << " ";
// 	return;
// }

// void rv2(vector<vector<int>> &vec, int n, int m) {
// 	vec = vector<vector<int>> (n, vector<int> (m));
// 	fl(i, 0, n-1) {
// 		fl(j, 0, m-1) cin >> vec[i][j];
// 	}
// 	return;
// }

// void pv2(vector<vector<int>> &vec) {
// 	int n = vec.size();
// 	if (n == 0) return;
// 	int m = vec[0].size();
// 	if (m == 0) return;
// 	loop (i, n) {
// 		loop (j, m) cout << vec[i][j] << " ";
// 		cout << endl;
// 	}
// 	return;
// }

int main() {
	FAST_IO;

	int T = 1;

	for (int t = 1; t <= T; t++) {
		int n, m, k;
		cin >> n >> m >> k;
		vpii row(n+1, mp(0, 0));
		vpii col(m+1, mp(0, 0));
		fl (i, 1, n) row[i].second = i;
		fl (i, 1, m) col[i].second = i;
		int x, y;
		set<pii> bs;
		fl (i, 1, k) {
			cin >> x >> y;
			bs.insert(mp(x, y));
			row[x].first++;
			col[y].first++;
		}
		sort(row.begin(), row.end());
		sort(col.begin(), col.end());
		int ans;
		int pr = row[n].first, pc = col[m].first;
		ans = pr + pc - 1;
		int i = n, j = m;
		bool f = false;
		while (i >= 1) {
			if (row[i].first != pr) break;
			j = m;
			while (j >= 1) {
				if (col[i].first != pc) break;
				if (bs.find(mp(row[i].second, col[i].second)) == bs.end()) {
					ans++;
					f = true;
					break;
				}
				j--;
			}
			if (f) break;
			i--;
		}
		cout << ans << endl;
	}

	return 0;
}


//(・ω・)
#include <stdio.h>
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, atoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <iomanip> //setprecision
#include <limits>
#include <sstream> //istringstream
#include <stdlib.h>
#include <list>
#include <iterator>//std::advance()
using namespace std;
int main(){
    int H,W,m;
    cin >> H >> W >> m;
    vector<int> h(m);
    vector<int> w(m);
    vector<vector<int>> b(H+1,vector<int>(W+1,0));
    for(int i=0; i<m; i++){
        cin >> h[i] >> w[i];
        b[h[i]][w[i]]++;
    }
    sort(h.begin(),h.end());
    sort(w.begin(),w.end());
    vector<pair<int,int>> hcount;
    vector<pair<int,int>> wcount;
    int hc=1,wc=1;
    for(int i=0; i<m-1; i++){
        if(w[i]==w[i+1]) wc++;
        else{
            wcount.push_back(make_pair(wc,w[i]));
            wc=1;
        }
        if(h[i]==h[i+1]) hc++;
        else{
            hcount.push_back(make_pair(hc,h[i]));
            hc=1;
        }
    }
    wcount.push_back(make_pair(wc,w[m-1]));
    hcount.push_back(make_pair(hc,h[m-1]));

    sort(hcount.begin(),hcount.end(),greater<pair<int,int>>());
    sort(wcount.begin(),wcount.end(),greater<pair<int,int>>());
    int max=0;
    for(int i=0; i<hcount.size(); i++){
        for(int j=0; j<wcount.size(); j++){
            int x;
            x=hcount[i].first+wcount[j].first-b[hcount[i].second][wcount[j].second];
            if(x>max) max=x;
        }
    }
    cout << max << endl;

}


#include <bits/stdc++.h>
#define ll long long int
#define endl '\n'
#define INF 1000000000
#define MOD 1000000007
#define MAX 200001
#define pb push_back
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int h,w,m,x,y;
    cin>>h>>w>>m;
    map<int,int> a,b;
    vector<set<int>> v1(h+1);
    h=0;w=0;
    for(int i=0;i<m;i++)
    {
    	cin>>x>>y;
    	v1[x].insert(y);
    	a[x]++;
    	h=max(h,a[x]);
    	b[y]++;
    	w=max(w,b[y]);
    }
    x=h;y=w;
    vector<int> c1,c2;
    for(auto it=a.begin();it!=a.end();it++)
    {
    	if(it->second==x)
    	{
    		c1.pb(it->first);
    	}
    }
    for(auto it=b.begin();it!=b.end();it++)
    {
    	if(it->second==y)
    	{
    		c2.pb(it->first);
    	}
    }
    x=0;
    for(int i=0;i<c1.size();i++)
    {
    	for(int j=0;j<c2.size();j++)
    	{
    		if(v1[c1[i]].find(c2[j])==v1[c1[i]].end())
    		{
    			y=a[c1[i]]+b[c2[j]];
    		}
    		else
    		{
    			y=a[c1[i]]+b[c2[j]]-1;
    		}
    		x=max(x,y);
    	}
    }
    cout<<x<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL)
#define ll long long
#define fm_t  ll t;cin>>t;while(t--)
#define fr(i,a,b) for( ll i=a;i<b;i++)
#define fr1(i,n,a) for( ll i=n-1;i>=a;i--)
#define sz(x) (ll)(x).size()
#define all(v)  (v).begin(), (v).end()
#define Sort(x) sort(x.begin(),x.end())
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
const ll mod = 998244353;
#define Mod 1000000007

#define inf 1e18
#define ld long double
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvl vector< vector<ll> >
#define vvi vector< vector<int> >
#define vlp vector< pair<ll, ll> >
#define vllp vector<pair<ll, ll>, ll >
#define pi pair<ll, ll>
#define ppi pair<pair<ll, ll>, ll>
#define ump unordered_map<ll,ll>
#define pri priority_queue< pair<ll,ll> , vector< pair<ll,ll> >, greater< pair<ll,ll> > >
void ingraph(vl graph[], ll m){ll x, y;fr(i,0,m){cin>>x>>y;x--, y--;graph[x].pb(y);graph[y].pb(x);}}

#define max3(a, b, c)   max(max(a, b), c)
#define min3(a, b, c)   min(min(a, b), c)

//setprecision(10)
//greater<int>()
ll gcd(ll a, ll b){if(b==0)return a;return gcd(b, a%b);}
ll lcm(ll a, ll b){return a*b/gcd(a, b);}

ll modmulti(ll a, ll b){
   return ((a%mod)*1ll*(b%mod))%mod;
}
ll modadd(ll a, ll b){
   ll asdfgh =  ((a%mod)+(b%mod)+mod+mod)%mod;
   asdfgh = (asdfgh+mod)%mod;
   return asdfgh;
}
ll modpower(ll a, ll n){
   if(n==0) return 1;
   if(n==1) return a%mod;
   ll b = modpower(a,n/2);
   b = modmulti(b,b);
   if(n%2==0) return b;
   return modmulti(a,b);
}
ll modinv(ll a){
    return modpower(a,mod-2);
}

int main()
{

  ll h,w,m;
  cin>>h>>w>>m;
  map<ll,ll>r,c;
  map<pair<ll,ll>,ll>mp;

  ll mx1=0,mx2=0;
  fr(i,0,m)
  {
    ll a,b;
    cin>>a>>b;
    r[a]++;
    c[b]++;
    mp[{a,b}]=1;

  }
ll ans=0;
  for(int i=1;i<=h;i++)
  {
    for(int j=1;j<=w;j++)
    {
      ans=max(ans,r[i]+c[j]-mp[{i,j}]);
    }
  }
  //cout<<c[1]<<endl;
  cout<<ans<<endl;

//cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl;
return 0;
}


#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for(int i=0;i<n;++i)
#define rrep(i, n) for(int i=n;i>=0;--i)
const int inf=1e9+7;
const ll mod=1e9+7;
const ll mod1=998244353;
const ll big=1e18;
const double PI=2*asin(1);

int main() {
  int N;
  cin>>N;
  for(int i=0;i<=N;++i) {
    if(i*1000>=N) {
      cout<<i*1000-N<<endl;
      return 0;
    }
  }
}

Score : 100 points

Problem Statement

We will buy a product for N yen (the currency of Japan) at a shop.

If we use only 1000-yen bills to pay the price, how much change will we receive?

Assume we use the minimum number of bills required.

Constraints

1 \leq N \leq 10000

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the amount of change as an integer.

Sample Input 1

1900

Sample Output 1

100

We will use two 1000-yen bills to pay the price and receive 100 yen in change.

Sample Input 2

3000

Sample Output 2

0

We can pay the exact price.


#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  int d = (n+999)/1000;
  cout<<(d*1000)-n;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define rep_start(a,i,n) for(ll i=a;i<=n;i++)
#define rev(i,n) for(ll i=n-1;i>=0;i--)
#define rev1(i,n) for(ll i=n;i>0;i--)
#define sz(a) int((a).size())
#define all(c) c.begin(),c.end()
#define tr(c,i) for(typeof(c).begin() i+=c.begin; i!=c.end(); i++)
#define present(c,x) (c.find(x)!=c.end())
#define cpresent(c,x) (find(all(c),x)!=c.end())
#define pb push_back
#define F first
#define S second
#define INF LONG_MAX
#define mod 1000000007
#define print1(a) cout<<a<<"\n";
#define print2(a,b) cout<<a<<" "<<b<<"\n";
#define print3(a,b,c) cout<<a<<" "<<b<<" "<<c<<"\n";
#define mt make_tuple
#define eb emplace_back

typedef long long int ll;
typedef vector<ll> vi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
typedef tuple<int,int,int> State;

void solve(){
    int n;cin>>n;
    if(n%1000==0) cout<<0;
    else cout<<1000-n%1000;
}

signed main() {
    // your code goes here
    IOS
    ll t=1;
    //cin>>t;
    rep1(i,t){
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define vii vector<int>
#define vcc vector<char>
#define pll pair<long long, long long>
#define mem memset
#define sof sizeof
#define co1 __builtin_popcountll
#define PB push_back
#define UB upper_bound
#define LB lower_bound
#define MP make_pair
#define TS to_string
#define F first
#define S second
#define endl '\n'
using namespace std;

long long __lcm(long long a, long long b){
    ll gd = __gcd(a,b);
    ll xx = (a*b)/gd;
    return xx;
}

long long bigmod(long long a, long long b, long long M){
    if(b==0) return 1%M;
    ll x = bigmod(a,b/2,M);
    x=(x*x)%M;
    if(b%2==1) x=(x*a)%M;
    return x;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long a,b,c,d,e,f,i,j,k,m,n,o,x,y;
    cin>>n;
    if(n%1000==0) cout<<0;
    else cout<<1000-(n%1000);
}


#include <bits/stdc++.h>

using namespace std;

int main(void)
{
    int N;
    cin >> N;
    cout << ((N-1 / 1000) + 1) * 1000 - N << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<double,int> pdi;
typedef pair<double,double> pdd;
typedef pair<double,ll> pdl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<pdd> vdd;
typedef vector<pdi> vdi;
typedef vector<pdl> vdl;
#define fi first
#define se second
const ll INFLL=LLONG_MAX;
const int INF=INT_MAX;
const ll MAXLL=0x3f3f3f3f3f3f3f3f;
const int MAX=0x3f3f3f3f;
#define eb emplace_back
#define emp emplace
#define mp(a,b) make_pair(a,b)
template<class T> using min_heap=priority_queue<T,vector<T>,greater<T> >;
template<class T>
void sort(vector<T>& v){
	sort(v.begin(),v.end());
}
template <class T, class U>
void sort(vector<T>& v,U func){
	sort(v.begin(),v.end(),func);
}
template<class T>
void rsort(vector<T>& v){
	sort(v.rbegin(),v.rend());
}
template <class T>
int lb_index(vector<T>& v,T k){
	return lower_bound(v.begin(),v.end(),k)-v.begin();
}
template <class T>
int ub_index(vector<T>& v,T k){
	return upper_bound(v.begin(),v.end(),k)-v.begin();
}
template<class T>
bool is_sorted(vector<T>& v){
	return is_sorted(v.begin(),v.end());
}
template<class T>
bool sorted(vector<T>& v){
	return is_sorted(v);
}

void solve(){
	int n;
	cin>>n;
	cout<<1000-(n%1000);
}

int main(){
	ios::sync_with_stdio(0);
	int numberofsubtestcases=1;
//	cin>>numberofsubtestcases;
	while(numberofsubtestcases--){
		solve();
	}
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define INF 0x3f3f3f3f
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
const ll mod=1000000007;
const double PI = acos(-1.0);
const double epsilon = PI / 180.0;//角度转弧度
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
const int N = 1e4+10, M = N * 2;

int main() {
	int n;
	cin>>n;
	cout<<n - n % 1000;
	return 0;
}


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

typedef long long ll;
const int INF = 1e9;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	// int t;
	// cin>>t;
	// while(t--){
		int n;
		cin>>n;
		n %= 1000;
		if (n == 0){
			cout<<0<<'\n';
		}else{
			cout<<1000-n<<'\n';
		}
	// }
	return 0;
}


#include<stdio.h>

int main(void)
{
  int N;
  printf("%d\n",N%1000);
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,c,d;
    cin>>n;
    if(n%1000==0)
    {
        cout<<0<<endl;
    }
    else
    {
        c=n/1000;
        d=((c+1)*1000)-n;
        cout<<d<<endl;
    }
}


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

int main() {
    int n;
    cin >> n;
    int k=(n-1)/1000+1;
    cout << 1000*k-n << endl;
}


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

int main()
{
    int n, i;
    cin >> n;

    for (i = 1; i * 1000 < n; i++)
    {
    }
    int ans = i * 1000 - n;

    cout << ans << endl;
}


#include <algorithm>
#include <bitset>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <cctype>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define ArraySizeOf(array)     (sizeof(array) / sizeof(array[0]))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define int long long
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define _GLIBCXX_DEBUG
int INF = 1e9+7;
unsigned NthDayOfWeekToDay(unsigned n, unsigned dow, unsigned dow1)
{
unsigned day;
if(dow < dow1) dow += 7;
day = dow - dow1;
day += 7 * n - 6;
return day;
}
unsigned DayToWeekNumber(unsigned day)
{
  return (day - 1) / 7 + 1;
}
unsigned AnotherDayOfWeek(unsigned day, unsigned day0, unsigned dow0)
{
  return (dow0 + 35 + day - day0) % 7;
}
using namespace std;
signed main(){
  int N;
  cin>>N;
  cout<<(N%1000==0? 0:1000-(N%1000))<<endl;
}


#include <bits/stdc++.h>
#include <stdio.h>
#define ll long long
#define fr(i,p,n) for(int i=p;i<n;i++)
#define fr1(i,p,n) for(int i=p;i>=n;i--)
#define FAST ios::sync_with_stdio(0);
#define sz(v) (ll)v.size()
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define PI 3.14159265358979323846
#define mod 1000000007
#define mod1 998244353
#define precise cout << std::setprecision(12) << std::fixed;
#define endl "\n"
using namespace std;

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    FAST
    int t=1;
    //cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int ans=n%1000;
        cout<<1000-ans<<endl;
    }
    return 0;
}


#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;

constexpr char newl = '\n';
constexpr double eps = 1e-10;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,b) FOR(i,0,b)
#define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--)
#define RF0(i,b) RFO(i,0,b)
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << '\n';
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()

template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
template<class T> void Print(vector<T> v) {
  F0R(i, v.size()) {
    cout << v[i] << ' ';
  }
  cout << newl;
}

#if 1

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  cin >> N;
  int a = (N + 999) / 1000 * 1000;
  cout << (a - N);
}
#endif