#include <stdio.h>

int cnt = 0;

void Insert(int A[],int n,int g)
{
  int i,j,v;

  for(i = g;i < n;i++)
  {
    v = A[i];
    j = i - g;
    while(j >= 0 && A[j] > v)
    {
      A[j + g] = A[j];
      j = j - g;
      cnt++;
    }
    A[j + g] = v;
  }
}

void Shell(int A[],int n)
{
  int i,m,h,G[100];

  for(i = 0,h = 1;i < n;i++)
  {
    if(h > n) break;
    G[i] = h;
    h = 3 * h + 1;
  }
  m = i;

  printf("%d\n",m);
  for(i = m - 1;i >= 0;i--)
  {
    printf("%d",G[i]);
    if(i) printf(" ");
  }
  printf("\n");

  for(i = m - 1;i >= 0;i--) Insert(A,n,G[i]);
}

int main()
{
  int i,n,A[1000000];

  scanf("%d",&n);
  for(i = 0;i < n;i++) scanf("%d",&A[i]);

  Shell(A,n);

  printf("%d\n",cnt);
  for(i = 0;i < n;i++)
  {
    printf("%d\n",A[i]);
  }

  return 0;
}

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 <stdio.h>

#define ARRAY_INIT 0
#define DIV_INIT 0
#define LOOP_INIT 0
#define G_MAX 12

int main( void )
{
	unsigned long ulW_Dtc_cnt;
	int *aulW_Dtestcase;
	unsigned long ulW_Cloop_cnt;
	unsigned long ulW_Cloop_sub_cnt;
	unsigned long ulW_Csort_cnt = DIV_INIT;
	unsigned long ulW_Dm;
	unsigned long aulW_DG[ G_MAX ];
	unsigned long ulW_Dv;
	signed long ulW_Dk;

	scanf( "%d", &ulW_Dtc_cnt );
	aulW_Dtestcase = ( int * ) malloc ( sizeof ( int ) * ulW_Dtc_cnt );
	for( ulW_Cloop_cnt = LOOP_INIT; ulW_Cloop_cnt < ulW_Dtc_cnt; ulW_Cloop_cnt++ ){
		scanf( "%d", &aulW_Dtestcase[ ulW_Cloop_cnt ] );
	}

	ulW_Dm = 1;
	aulW_DG[ 0 ] = 1;
	while( 3 * aulW_DG[ ulW_Dm - 1 ] + 1 < ulW_Dtc_cnt ){
		aulW_DG[ ulW_Dm ] = 3 * aulW_DG[ ulW_Dm - 1 ] + 1;
		ulW_Dm++;
	}

	for( ulW_Cloop_cnt = LOOP_INIT; ulW_Cloop_cnt < ulW_Dm / 2; ulW_Cloop_cnt++ ){
		ulW_Dv = aulW_DG[ ulW_Cloop_cnt ];
		aulW_DG[ ulW_Cloop_cnt ] = aulW_DG[ ulW_Dm - 1 - ulW_Cloop_cnt ];
		aulW_DG[ ulW_Dm - 1 - ulW_Cloop_cnt ] = ulW_Dv;
	}

	printf( "%d\n", ulW_Dm );
	for( ulW_Cloop_cnt = LOOP_INIT; ulW_Cloop_cnt < ulW_Dm; ulW_Cloop_cnt++ ){
		if( ulW_Cloop_cnt == ulW_Dm - 1 ){
			printf( "%d\n", aulW_DG[ ulW_Cloop_cnt ] );
		}
		else{
			printf( "%d ", aulW_DG[ ulW_Cloop_cnt ] );
		}
	}

	for( ulW_Cloop_cnt = LOOP_INIT; ulW_Cloop_cnt < ulW_Dm; ulW_Cloop_cnt++){
		for( ulW_Cloop_sub_cnt = aulW_DG[ ulW_Cloop_cnt ]; ulW_Cloop_sub_cnt < ulW_Dtc_cnt; ulW_Cloop_sub_cnt++ ){
			ulW_Dv = aulW_Dtestcase[ ulW_Cloop_sub_cnt ];
			ulW_Dk = ulW_Cloop_sub_cnt - aulW_DG[ ulW_Cloop_cnt ];
			while( ( ulW_Dk >= 0 ) && ( aulW_Dtestcase[ ulW_Dk ] > ulW_Dv ) ){
				aulW_Dtestcase[ ulW_Dk + aulW_DG[ ulW_Cloop_cnt ] ] = aulW_Dtestcase[ ulW_Dk ];
				ulW_Dk -= aulW_DG[ ulW_Cloop_cnt ];
				ulW_Csort_cnt++;
			}
			aulW_Dtestcase[ ulW_Dk + aulW_DG[ ulW_Cloop_cnt ] ] = ulW_Dv;
		}
	}

	printf( "%d\n", ulW_Csort_cnt );
	for( ulW_Cloop_cnt = LOOP_INIT; ulW_Cloop_cnt < ulW_Dtc_cnt; ulW_Cloop_cnt++ ){
		printf( "%d\n", aulW_Dtestcase[ ulW_Cloop_cnt ] );
	}

	return 0;
}


#include <stdio.h>
#include <stdlib.h>

unsigned gaps[] = {1,8,23,77,281,1073,4193,16577,65921,262913,1050113};
// generated from pow(4,i)+3*pow(2,i-1)+1 (prefixed with 1)

unsigned insertion_sort(unsigned a[], unsigned n, unsigned gap){
  unsigned i,count = 0;
  for(i = gap; i < n; i++){
    unsigned 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 shell_sort(unsigned a[], unsigned n){
  int i;
  unsigned gap_num = 0;
  while(gaps[++gap_num] < n);
  gap_num--;

  unsigned count = 0;
  for(i = gap_num; i >= 0; i--) count += insertion_sort(a,n,gaps[i]);

  printf("%u\n",(gap_num+1));

  for(i = gap_num; i; i--) printf("%u ",gaps[i]);
  printf("%u\n",gaps[0]);

  printf("%u\n",count);
}

int main(){
  unsigned n,i;
  scanf("%u",&n);
  unsigned* a = (unsigned*)malloc(sizeof(unsigned)*n);
  for(i = 0; i < n; i++) scanf("%u",&a[i]);
  shell_sort(a,n);
  for(i = 0; i < n; i++) printf("%u\n",a[i]);
  free(a);
  return 0;
}


#include<stdio.h>

void insertionSort(int *,int ,int);
void shellSort(int *,int);
int cnt;
int main()
{
  int n,i,A[1000000];

  scanf("%d",&n);

  for(i=0;i<n;i++)
    {
      scanf("%d",&A[i]);
    }
  shellSort(A,n);
  for(i=0;i<n;i++)
    {
      printf("%d\n",A[i]);
    }

  return 0;
}
  void insertionSort(int *A,int n,int g)
  {
    int i,j,v;
    for(i=g;i<n;i++)
      {
	v=A[i];
	j=i-g;
	while(j>=0 && A[j]>v)
	  {
	    A[j+g]=A[j];
	    j=j-g;
	    cnt++;
	  }
	A[j+g]=v;
      }

      }

  void shellSort(int *A,int n)
  {
    int m=0,G[n],i;
    G[0]=1;

    for(i=0;;i++)
      {
	if(G[i]>n)break;
	G[i+1]=3*G[i]+1;
	m++;
      }
    printf("%d\n",m);
        for(i=m-1;i>=0;i--)
      {
	printf("%d ",G[i]);
	insertionSort(A,n,G[i]);
      }
    printf("\n%d\n",cnt);

  }


#include <stdio.h>
#define N 1000005
#define SIZE 14

void insertionSort(int n,int g);
void shellSort(int n);

int A[N];
int G[SIZE] = {1,4,13,40,121,364,1093,3280,9841,29524,88573,265720,797161,2391484};
int cnt=0,m;

int main(){
  int i,j,n;
  scanf("%d",&n);
  for(i=0; i<n; i++){
    scanf("%d",&A[i]);
  }

  shellSort(n);

  printf("%d\n",m);
  for(i=m-1; i>=0; i--){
    printf("%d",G[i]);
    if(i) printf(" ");
  }
  printf("\n");
  printf("%d\n",cnt);
  for(i=0; i<n; i++) printf("%d\n",A[i]);

  return 0;

}

void insertionSort(int n,int g){
  int v,i,j;
  for(i=g; i<n; i++){
    v = A[i];
    j = i - g;
    while(j >= 0 && A[j] > v){
      A[j+g] = A[j];
      j -= g;
      cnt++;
    }
    A[j+g] = v;
  }
}

void shellSort(int n){
  int i,h;
  for(h=1; ; ){
    if(h > n) break;
    h = 3*h +1;
  }
  for(i=0; i<SIZE; i++){
    if(h == G[i]) m = i;
  }
  for(i=m-1; i>=0; i--){
    insertionSort(n,G[i]);
  }
}


#include <stdio.h>          // printf(), scanf()
#define MAX_N 1000000
#define MAX_G 1000

int count;
int G[MAX_G];
int ix;

void
insertionSort(int a[MAX_N], int n, int g)
{
	int v;
	int i, j;

	for (i = g; i < n; ++i)
	{
		v = a[i];
		for (j = i - g; j >= 0 && v < a[j]; j -= g)
		{
			a[j + g] = a[j];
			count++;
		}

		a[j + g] = v;
	}
}

void
shellsort(int a[MAX_N], int n)
{
	int h;
	int i;

	ix = 0;
	for (h = 1; h <= n;)
	{
		G[ix++] = h;
		h = 3 * h + 1;
	}

	for (i = ix - 1; i >= 0; --i)
		insertionSort(a, n, G[i]);
}

int
main(int argc, char** argv)
{
	int a[MAX_N];
	int n;
	int i;

	scanf("%d", &n);
	for (i = 0; i < n; ++i)
		scanf("%d", &a[i]);

	count = 0;
	shellsort(a, n);

	printf("%d\n", ix);
	for (i = ix - 1; i >= 0; --i)
		printf("%d%c", G[i], (i != 0) ? ' ' : '\n');

	printf("%d\n", count);
	for (i = 0; i < n; ++i)
		printf("%d\n", a[i]);

	return 0;
}


#include <stdio.h>
int n, A[1000000], cnt;
void insertionSort(int g) {
  int v;
  int i, j;
  for(i = g; i < n; i++) {
    v = A[i];
    j = i-g;
    while(j >= 0 && A[j] > v) {
      A[j+g] = A[j];
      j -= g;
      cnt++;
    }
    A[j+g] = v;
  }
}
void shellSort() {
  int G[100], m = 0, g = 0;
  int i;
  while(3*g+1 <= n) {
    g = 3*g+1;
    G[m++] = g;
  }
  printf("%d\n", m);
  for(i = m-1; i >= 0; i--) {
    if(i < m-1) printf(" ");
    printf("%d", G[i]);
    insertionSort(G[i]);
  }
  puts("");
  printf("%d\n", cnt);
  for(i = 0; i < n; i++) printf("%d\n", A[i]);
}
int main() {
  int i;
  scanf("%d", &n);
  for(i = 0; i < n; i++) scanf("%d", A+i);
  shellSort();
  return 0;
}


#include <stdio.h>
#include <stdlib.h>

void insertionSort(int *,int,int);
void shellSort(int *,int);

int cnt;

int main(int argc, char const *argv[]) {

  int num;
  int i;
  int line[1000000];

  scanf("%d",&num);

  for( i = 0 ; i < num ; i++ ){
    scanf("%d",&line[i]);
  }

  shellSort(line,num);

  for( i = 0 ; i < num ; i++ ){
    printf("%d\n",line[i]);
  }

  return 0;
}

void insertionSort(int *A, int n, int g){

  int i,j;
  int v;

  for( i = g ; i < n ; i++ ){

    v = A[i];
    j = i - g;

    while( j >= 0 && A[j] > v ){

      A[j+g] = A[j];
      j -= g;
      cnt++;

    }

    A[j+g] = v;

  }

}

void shellSort(int *A, int n){

  int i;
  int G[4] = {1,4,13,40,121,364,1093,3280,9841,29524,88573,265720,797161,2391584};
  int m = 4;
  int count = 0;

  for( i = m-1 ; i >= 0 ; i-- ){
    insertionSort(A,n,G[i]);
  }

  for( i = m-1 ; i >= 0 ; i-- ){
    if( n >= G[i] ){
      count++;
    }
  }

  m = count;

  printf("%d\n",m);

  for( i = m-1 ; i >= 0 ; i-- ){
    if( n >= G[i] ){
      printf("%d",G[i]);
      if( i ){
        printf(" ");
      }
    }
  }

  printf("\n%d\n",cnt);

}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int count=0;

void select_sort(int *A,int n,int g){
  int i,j;
  int key;
  for(i=g;i<n;i++){
    j=i;
    key=A[i];
    while(0 <= j-g){
      if(A[j-g] <= key) break;
      A[j]=A[j-g];
      j-=g;
      count+=1;
    }
   A[j]=key;
 }
}

int main(){
  int n;
  int i,j;
  int *A;
  int G[20];

  scanf("%d",&n);
  A=malloc(n*sizeof(int));
  for(i=0;i<n;i++) scanf("%d",&A[i]);

  int m=0;
  G[0]=0;
  while(1){
    if(n < 3*G[m]+1) break;
    G[m+1]=3*G[m]+1;
    m+=1;
 }

 for(i=m;i>0;i--) select_sort(A,n,G[i]);

  printf("%d\n",m);
  for(i=m;i>=1;i--) printf("%d ",G[i]);
  printf("\n");
  printf("%d\n",count);

  for(i=0;i<n;i++) printf("%d\n",A[i]);
  return 0;
}


#include<stdio.h>
void insertionSort(int *a,int n,int g);
int count = 0;

int main(){
  int a[1100000],g[110]={};
  int n,m=1;
  int i,j,x;

  scanf("%d",&n);
  for(i=0;i<n;i++) scanf("%d",&a[i]);
  x=n;
  while(x/2!=0){
    x/=2;
    m++;
  }
  g[0]=n;
  for(i=1;i<m;i++){
    if(g[i-1]/2!=0)  g[i]=g[i-1]/2;
    else g[i]=1;
  }
  for(i=0;i<m;i++) insertionSort(a,n,g[i]);
  printf("%d\n",m);
  for(i=0;i<m;i++){
    printf("%d",g[i]);
    if(i!=m-1) printf(" ");
    else printf("\n");
  }
  printf("%d\n",count);
  for(i=0;i<n;i++) printf("%d\n",a[i]);

    return 0;
}

void insertionSort(int *a,int n,int g){
  int v;
  int i,j;

  for(i=g;i<n;i++){
    v=a[i];
    j=i-g;
    while(j>=0 && a[j]>v){
      a[j+g] = a[j];
      j = j-g;
      count++;
    }
    a[j+g] = v;
  }
}


#include<stdio.h>
#include<stdlib.h>

int insertionSort(int *A,int n,int g){
  int v, i, j, cnt=0;
  for(i=g; i<=n-1; i++){
    v = A[i];
    j = i - g;
    while(j>=0 && A[j]>v){
      A[j+g] = A[j];
      j = j-g;
      cnt++;
    }
    A[j+g] = v;
  }
  return cnt;
}

int main(){

  int *A, N, i, *G, cnt, m;

  scanf("%d",&N);
  A = (int *)malloc(sizeof(int)*N);
  for(i=0; i<=N-1; i++){
    scanf("%d",&A[i]);
  }

  //shellSort(A, n)
  cnt = 0;
  m = N/2;
  if(m > 100) m = 100;
  else if(0 >= m) m = 1;
  G = (int *)malloc(sizeof(int)*m);
  for(i=0; i<=m-1; i++){
    G[i] = m-i;
  }

  for(i=0; i<=m-1; i++){
    cnt += insertionSort(A, N, G[i]);
  }

  printf("%d\n",m);
  printf("%d",G[0]);
  for(i=1; i<=m-1; i++){
  printf(" %d",G[i]);
  }
  printf("\n");
  printf("%d\n",cnt);
  for(i=0; i<=N-1; i++){
    printf("%d\n",A[i]);
  }

  return 0;
}


#include<stdio.h>
#include<stdlib.h>
#define N 1000000
int cnt=0,n,l,A[N],*S;

void insertionSort(int A[], int n, int m){
int i,v,j;
  for(i=m;i<n;i++){
    v=A[i];
    j=i-m;
    while(j>=0&&A[j]>v){
      A[j+m]=A[j];
      j-=m;
      cnt++;
    }
    A[j+m]=v;
  }
}

void Sort(int A[],int n){
  int i=0,a;

S=(int *)malloc(n * sizeof(int));

for(a=1; ; ){
if(a>n) break;
S[i]=a;
i++;
  a=3*a+1;
}
l=i;
  for(i=l-1;i>=0;i--){
    insertionSort(A,n,S[i]);
  }
}

int main(){
int i;
scanf("%d",&n);
for(i=0;i<n;i++) scanf("%d",&A[i]);
cnt=0;

Sort(A,n);

printf("%d\n",l);

for(i=l-1;i>=0;i--){
printf("%d",S[i]);
if(i) printf(" ");
}
printf("\n");
printf("%d\n",cnt);
for(i=0;i<n;i++)printf("%d\n",A[i]);

 free(S);
  return 0;
}


#include<stdio.h>

int insertionsort(int a[], int n, int g, int cnt)
{
	int v, j, i;
	for(i=g; i<n; i++){
		v = a[i];
		j = i-g;
		while (j>=0 && a[j]>v){
			a[j+g] = a[j];
			j=j-g;
			cnt++;
		}
		a[j+g] = v;
	}
	return cnt;
}

void shellsort(int a[], int n)
{
	int i;
	int cnt = 0;
	int g[n];
	int m = 0;

	for(int h=1; ;){
		if(h > n) break;
		g[m] = h;
		h = 3*h+1;
		m++;
	}

	printf("%d\n", m);

	for(i=m-1; i>=0; i--){
		if(i != m-1) printf(" ");
		printf("%d", g[i]);
	}
	printf("\n");

	for(i=m-1; i>=0; i--){
		cnt = insertionsort(a, n, g[i],cnt);
	}
	printf("%d\n", cnt);
	for(i=0; i<n; i++){
		printf("%d\n", a[i]);
	}
}

int main(void)
{
	int n;
	scanf("%d", &n);
	int a[n];
	int i;
	for(i=0; i<n; i++){
		scanf("%d", &a[i]);
	}

	shellsort(a, n);

	return 0;
}


#include<stdio.h>

int cnt;

void insertionSort(int *, int, int);
void shellSort(int *, int);

int main()
{
	int i, n;
	scanf("%d", &n);
	int A[n];
	for(i=0; i<n; i++) scanf("%d", &A[i]);
	shellSort(A, n);
	printf("%d\n", cnt);
	for(i=0; i<n; i++) printf("%d\n", A[i]);
	return 0;
}

void insertionSort(int A[], int n, int g)
{
	int i, j, v;
	for(i=g; i<n; i++){
		v=A[i];
		j=i-g;
		while(j>=0 && A[j]>v){
              A[j+g]=A[j];
              j=j-g;
              cnt++;
		}
		A[j+g]=v;
	}
}

void shellSort(int A[], int n) //一定の間隔 g だけ離れた要素のみを対象とした挿入ソートである insertionSort(A, n, g) を、最初は大きい値から g を狭めながら繰り返します。
{
	int i, m;
	int G[100];
	G[0]=1;
	cnt=0;
	for(m=1; G[m-1]*3+1<=n; m++){ //G[m]がnより小さい間
		G[m]=G[m-1]*3+1;
	}
	printf("%d\n", m);
	for(i=m-1; i>=0; i--) {
		if(i==0) printf("%d\n", G[i]);
		else printf("%d ", G[i]);
		insertionSort(A, n, G[i]);
  }
}


#include <stdio.h>

int insertionSort(int *A, int n, int g);
void shellSort(int *A, int n);

int main()
{
    int i,n;
    scanf("%d%*c",&n);
    int *A;
    A = malloc(n*sizeof(int));
    for (i=0;i<n;i++)
    {
        scanf("%d%*c",&A[i]);
    }
    shellSort(A,n);
    for (i=0;i<n;i++)
    {
        printf("%d\n",A[i]);
    }

    return 0;
}

int insertionSort(int *A, int n, int g)
{
    int i,j,tmp,cnt=0;
    for(i=g;i<n;i++)
    {
        tmp = A[i];
        j = i-g;
        while (j>=0 && A[j]>tmp)
        {
            A[j+g] = A[j];
            j = j-g;
            cnt++;
        }
        A[j+g] = tmp;
    }
    return cnt;
}

void shellSort(int *A,int n)
{
    int ans[102] = {1};
    int i,j,m=1,cnt=0;
    int h=1;
    for (i=0;i<((int)(n*0.333333)+1);i++)
    {
        h = 3*h+1; // h=1,4,13,40,121,364...
        if (h<=n && m<100)
        {
            ans[m] = h;
            m+=1;
        }
        else break;
    }
    printf("%d\n",m);
    int *G;
    G = malloc((m)*sizeof(int));
    for (i=0;i<m;i++)
    {
        G[i] = ans[m-1-i];
        printf("%d",G[i]);
        if (i<m-1) printf(" ");
    }
    printf("\n");
    for (i=0;i<m;i++)
    {
        cnt += insertionSort(A,n,G[i]);
    }
    printf("%d\n",cnt);
    free(G);
    return; //G 넣은뒤 m,cnt넣어놓기.
}


#include<stdio.h>
#include<string.h>
char dic[1000000][12];
int main(){
  int n;
  int top=0;
  char buf[12];
  int i,j;

  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%s",buf);

    if(strcmp(buf,"insert")==0){
      scanf("%s",dic[top]);
      top++;
    }
    else if(strcmp(buf,"find")==0){
      scanf("%s",buf);

      for(j=0;j<top;j++){
        if(strcmp(buf,dic[j])==0){
          printf("yes\n");
          break;
        }
      }
      if(j==top)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 <stdio.h>
#include <string.h>

#define N 1000000
#define L 13

int getChar(char);
long long getkey(char *);
int h1(int);
int h2(int);
int find(char *);
int insert(char *);

char HT[N][L];

int main(){
  int i, num, h;
  char str[L],com[10];
  for(i=0;i<N;i++) HT[i][0] = '\0';
  scanf("%d",&num);
  for(i=0;i<num;i++) {
    scanf("%s %s",com,str);

  if(com[0]=='i') insert(str);
  else {
    if(find(str)==1) printf("yes\n");
    else printf("no\n");
  }
}
  return 0;
}

int getChar(char c){
  if(c=='A') return 1;
  else if(c=='C') return 2;
  else if(c=='G') return 3;
  else if(c=='T') return 4;
  else return 0;
}

long long getkey(char str[]){
  long long sum = 0, p = 1, i;
  for(i=0;i<strlen(str);i++) {
    sum += p*(getChar(str[i]));
    p*=5;
  }
  return sum;
}

int h1(int key){
  return key%N;
}
int h2(int key){
  return 1+(key%(N-1));
}

int find(char str[]){
  long long key, i, h;
  key = getkey(str); /*文字列を数値に変換*/
  while(1){
    h = (h1(key)+i*h2(key))%N;
    if(strcmp(HT[h],str)==0) return 1;
    else if(strlen(HT[h])==0) return 0;
  }
  return 0;
}

int insert(char str[]){
  long long key, i, h;
  key = getkey(str); /*文字列を数値に変換*/
  while(1){
    h=(h1(key)+i*h2(key))%N;
   if(strlen(HT[h])==0)break;
   i++;
 }

 strcpy(HT[h],str);
 return 0;
}


#include <stdio.h>
#include <string.h>

char dict[30600000];

char word[26];

long encode(const char *key){
  int n=strlen(key),i;
  long sum=0;
  long keta=1;
  for(i=0;i<n;i++){
    int keyi=key[i];
    sum = sum + keta * word[keyi];
    //    if (key[i]=='A')      sum = sum + keta;
    //    else if (key[i]=='C') sum = sum + (keta << 1);
    //    else if (key[i]=='G') sum = sum + (keta << 1) + keta;
    //    else if (key[i]=='T') sum = sum + (keta << 2);
    keta=keta*5;
  }
  return (sum);
}

void insert(const char *key2) {
  long key=encode(key2);
  long keta=key/8;
  dict[keta]=(dict[keta]|(1<<(key%8)));
}

int find_h(const char *key2) {
  long key=encode(key2);
  return (dict[key/8] & 1<<(key%8));
}

int main(void) {
  long n,i;
  word['A'] = 1;
  word['C'] = 2;
  word['G'] = 3;
  word['T'] = 4;
  scanf("%ld\n",&n);
  for(i=0;i<n;i++){
    char cmd[8], str[14]; scanf("%s %s\n",cmd, str);
    if(cmd[0]=='i') {
      insert(str);
    }
    else {
      if(find_h(str)) printf("yes\n");
      else printf("no\n");
    }
  }
  return 0;
}


#include <stdio.h>
#include <string.h>

#define N 1000000
#define L 14

char A[N][L];

int getChar(char c);
long long getKey(char str[]);
int h1(int key){ return key%N; }
int h2(int key){ return 1+(key%(N-1)); }
int find(char str[]);
int insert(char str[]);

int main(){
  int i, n;
  char str[L], cmp[9];

  for(i = 0; i < N; i++){
    A[i][0] = '\0';
  }
  scanf("%d", &n);
  for(i = 0; i < n; i++){
    scanf("%s %s", cmp, str);
    if(cmp[0] == 'i'){
      insert(str);
    } else {
      if(find(str)){
        printf("yes\n");
      } else {
        printf("no\n");
      }
    }
  }
  return 0;
}

int getChar(char c){
  if( c == 'A') return 1;
  else if( c == 'C') return 2;
  else if( c == 'G') return 3;
  else if( c == 'T') return 4;
  else return 0;
}

long long getKey(char str[]){
  long long sum = 0, p = 1, i;
  for (i = 0; i < strlen(str); i++){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int find(char str[]){
  int i, h;

  h = (h1(getKey(str)) + i * h2(getKey(str))) % N;
  for(i = 0; i < 14; i++){
    h = (h1(getKey(str)) + i * h2(getKey(str))) % N;
    if(strcmp(A[h], str) == 0) return 1;
  }
  return 0;
}

int insert(char str[]){
  int i;
  if(find(str)) return 0;
  for(i = 0; i < 14; i++){
    if(A[ (h1(getKey(str)) + i * h2(getKey(str))) % N ][0] == '\0'){
      strcpy(A[ (h1(getKey(str)) + i * h2(getKey(str))) % N ], str);
      return 1;
    }
  }
  return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define M 1046527
#define NIL (-1)
#define L 14

typedef struct cc {
  char com[9];
  char str[L];
} C;

char H[M][L];

int getChar(char ch){
  if(ch == 'A') return 1;
  else if (ch == 'C') return 2;
  else if (ch == 'G') return 3;
  else if (ch == 'T') return 4;
  else return 0;
}

long long getKey(char str[]){
  long long sum = 0, p = 1, i;
  for(i=0;i<strlen(str);i++){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key){ return key % M;}
int h2(int key){ return 1 + (key % (M-1));}

int find(char str[]){
  long long key, i, h;
  key = getKey(str);
  for(i=0;;i++){
    h = (h1(key) + i * h2(key)) % M;
    if(strcmp(H[h],str) == 0) return 1;
    else if(strlen(H[h]) == 0) return 0;
  }
  return 0;
}

int insert(char str[]){
  long long key, i, h;
  key = getKey(str);
  for(i=0;;i++){
    h = (h1(key) + i * h2(key)) % M;
    if(strcmp(H[h], str) == 0) return 1;
    else if(strlen(H[h]) == 0){
      strcpy(H[h], str);
      return 0;
    }
  }
  return 0;
}

int main(){
  int i, n, h;
  char str[L], com[9];
  for(i=0;i<M;i++) H[i][0] = '\0';
  scanf("%d", &n);
  C *com_line = (C *)malloc(sizeof(C)*n);
  for(i=0;i<n;i++) scanf("%s %s", com_line[i].com, com_line[i].str);
  for(i=0;i<n;i++){
    if(com_line[i].com[0] == 'i'){
      insert(com_line[i].str);
    } else {
      if(find(com_line[i].str)){
        printf("yes\n");
      } else {
        printf("no\n");
      }
    }
  }
  return 0;
}


#include<stdio.h>
#include<string.h>
#define N_MAX 1000005
#define str_MAX 12

char data [N_MAX][str_MAX];
int sum=0;

void insert(char s[str_MAX])
{
	strcpy(data[sum], s);
	sum++;
}

void find(char s[str_MAX])
{
	int i;
	int flag=0;
	for(i=0; i<sum; i++){
			if(strcmp(data[i], s) == 0){
					flag=1;
					break;
			}
	}

	if(flag == 1){
		printf("yes\n");
	}
	else{
		printf("no\n");
	}
}

int main()
{
	int n;
	int i;
	char cm [100], s[str_MAX];

	scanf("%d", &n);

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

		if(strcmp(cm, "insert") == 0) insert(s);
		if(strcmp(cm, "find") == 0) find(s);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
#define N_MAX 1000005
#define LENGS 12
#define NIL -1

char hash[N_MAX][LENGS];

int getint(char c)
{
  switch (c){
  case 'A':
    return 1;
    break;
  case 'C':
    return 2;
    break;
  case 'G':
    return 3;
    break;
  case 'T':
    return 4;
    break;
  }
}

int getkey(char data[LENGS])
{
  long long sum=0, p=1;
  long long i;

  for(i=0; i<strlen(data); i++){
    sum+=p*getint(data[i]);
    p*=5;
  }
  return sum;
}

int h1(int key)
{
  return key % N_MAX;
}

int h2(int key)
{
  return 1+(key & (N_MAX-1));
}

void insert(char data[LENGS])
{
  long long h,key;
  long long i=0;

  key = getkey(data);

  while(1){
    //    printf("inert\n");
    h = (h1(key) + i*h2(key)) % N_MAX;
    if(strcmp(hash[h], data) == 0){
      break;
    }
    else if(strlen(hash[h]) == 0){
      strcpy(hash[h], data);
      break;
    }
    i++;
  }
}

int find(char data[LENGS])
{
  long long key, h;
  long i=0;

  key = getkey(data);

  while(1){
    //  printf("find");
    h = (h1(key) + i*h2(key)) % N_MAX;
    if(strcmp(hash[h], data) == 0){
      return 1;
    }
    else if(strlen(hash[h]) == 0){
      return 0;
    }
    i++;
  }
  return 0;
}

int main()
{
  int i;
  int n;
  char data[LENGS], cmnd[10];
  int ans[N_MAX], count=0;

  scanf("%d", &n);
  for(i=0; i<N_MAX; i++){
    hash[i][0] = '\0';
  }

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

    if(strcmp(cmnd, "insert") == 0){
      scanf("%s", data);
      insert(data);
    }

    else if(strcmp(cmnd, "find") == 0){
      scanf("%s", data);
      if(find(data)) printf("yes\n");
      else printf("no\n");
    }
  }

  /* for(i=0; i<count; i++){
    if(ans[i] == 1) printf("yes\n");
    else printf("no\n");
    }*/

  return 0;
}


#include<stdio.h>
#include<string.h>

#define M 1046527
#define L 14

char H[M][L];

int getChar(char ch){
  if ( ch == 'A') return 1;
  else if ( ch == 'C') return 2;
  else if ( ch == 'G') return 3;
  else if ( ch == 'T') return 4;
}

long long getKey(char str[]){
  long long sum = 0, p = 1, i;
  for ( i = 0; i < strlen(str); i++ ){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key){ return key % M/* your task */; }
int h2(int key){ return 1 + (key % (M -1))/* your task */; }

int find(char str[]){
  long long key,i,h;

  key = getKey(str);
  for(i=0;;i++){
    h=(h1(key)+i*h2(key))%M;
    if(strcmp(H[h],str)==0)return 1;
    else if(strlen(H[h])==0)return 0;
  }
}

int insert(char str[]){
  long long key,i,h;

  key = getKey(str);
  for(i=0;;i++){
    h=(h1(key)+i*h2(key))%M;
    if(strcmp(H[h],str)==0)return 1;
    else if(strlen(H[h])==0){
      strcpy(H[h],str);
      return 0;
    }
  }
}

int main(){
    int i, n, h;
    char str[L], com[9];
    for ( i = 0; i < M; i++ ) H[i][0] = '\0';

    scanf("%d", &n);

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

	if ( com[0] == 'i' ){
	    insert(str);
	} else {
	    if (find(str)){
		printf("yes\n");
	    } else {
		printf("no\n");
	    }
	}
    }

    return 0;
}


#include<stdio.h>
#include<string.h>

int search(char[] , int );

char inputdata[3000000][15];
int answer[3000000];

int main(){

  int datasum,i;
  char command[10];
  char finddata[20];
  int inputdatasum;
  int answer[2000000];
  int findsum;

  inputdatasum = 0;
  findsum = 0;

  scanf("%d",&datasum);

  for(i = 0; i < datasum; i++){

    scanf("%s",command);

    if((strcmp(command,"insert"))== 0){

      scanf("%s",inputdata[inputdatasum]);

      inputdatasum += 1;

    }

    if( (strcmp(command,"find")) == 0){

      scanf("%s",finddata);

      answer[findsum] = search(finddata,inputdatasum);

      findsum = findsum + 1;

      }

  }

  for(i = 0; i < findsum; i++){

    if(answer[i] == 1){

      printf("yes\n");

    }

    else{

      printf("no\n");

    }

  }

   return 0;
}

int search(char searchdata[20], int inputsumdata){

  int i,flag;

  flag = 0;

  for(i = 0; i < inputsumdata; i++){

    if((strcmp(searchdata,inputdata[i])) == 0){

      flag = 1;

      return 1;

      break;

    }
  }

    return 0;

}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define N 500000

typedef struct h{
    char data[13];
struct h *next;
}H;

H table[N];

void init() {
    int i;

    for (i = 0; i < N; i++) {
        table[i].next = NULL;
        *table[i].data = NULL;
    }
}

int hash(char *x) {
    int sum = 0;
    while (*x)
        sum += *x++;
    sum = (sum*sum) / 2 ;
    return (sum+(*--x))%N;
}

int find(char *x) {
    H *p;
    int h;

    h = hash(x);
    for (p = table[h].next; p != NULL; p = p->next)
        if (strcmp(p->data,x) == 0)
            return 1;

    return 0;
}

void insert(char *x) {
    H *p;
    H *New;
    int h;

    if (find(x))
        return;

    if ((New = (H *)malloc(sizeof(H))) == NULL)
        exit(1);

    h = hash(x);
    strcpy(New->data, x);
    p = table[h].next;
    table[h].next = New;
    New->next = p;
}

/*void Delete(char *x) {
    H *p;
    H *q;
    int h;

    h = hash(x);

    for (p = table[h].next, q = &table[h]; p != NULL; q = p, p = p->next)
        if (strcmp(p->data, x) == 0) {
            q->next = p->next;
            break;
        }

    if (p != NULL)
        free(p);
}*/

int main(void) {
    int i, j, n;
    char com[7], data[13];

    init();

    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        scanf("%s %s", com, data);

        if (com[0] == 'i')
            insert(data);
        else if (com[0] == 'f')
            if (find(data))
                printf("yes\n");
            else
                printf("no\n");
        /*else
            Delete(data);*/
    }

    return 0;
}


#include <stdio.h>
#include <string.h>

#define MAX 300000000

void insert(char []);
int find(char []);
int getNum(char []);

int hash[MAX];

int main(){
  int i, n;
  char str[15], cmd[10];

  for(i=0; i<MAX; i++) hash[i] = 0;
  scanf("%d", &n);
  for(i=0; i<n; i++){
    scanf("%s%s", cmd, str);
    if(cmd[0] == 'i') insert(str);
    else printf(find(str)?"yes\n":"no\n");
  }

  return 0;
}

void insert(char str[]){
  int num;
  num = getNum(str);
  hash[num] = 1;
}

int find(char str[]){
  int num;
  num = getNum(str);
  if(hash[num] == 1) return 1;
  return 0;
}

int getNum(char str[]){
  int i, sum = 0, code, x = 1;
  for(i=0; i<strlen(str); i++){
    switch(str[i]){
    case 'A':
      code = 1;
      break;
    case 'C':
      code = 2;
      break;
    case 'G':
      code = 3;
      break;
    case 'T':
      code = 4;
      break;
    }
    sum += code * x;
    x *= 5;
  }
  return sum;
}


#define M 8000
#define L 14

char H[M][L]; /* Hash Table */
int getChar(char ch){
  if ( ch == 'A') return 1;
  else if ( ch == 'C') return 2;
  else if ( ch == 'G') return 3;
  else if ( ch == 'T') return 4;
}

/* convert a string into an integer value */
long long getKey(char str[]){
  long long sum = 0, p = 1, i;
  for ( i = 0; i < strlen(str); i++ ){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key){ return key % M; }
int h2(int key){ return 1+ (key % M); }

int find(char str[]){
  int i;
  for(i=0;i<M;i++){
    if(strcmp(str,H[i])==0)  return 1;
  }
    return 0;
}
void insert(char str[]){
  long long key = 0;
  int h_1,h_2,i=0;
  key = getKey(str);
  h_1 = h1(key);
  h_2 = h2(key);
  if(find(str) == 0) strcpy(H[h_1],str);
  else {
    i++;
    h_1 = (h_1+ i * h_2) % M;
    strcpy(H[h_1],str);
  }
}

int main(){
  int i,h,n;
    char str[L], com[9];
    for ( i = 0; i < M; i++ ) H[i][0] = '\0';

    scanf("%d", &n);

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

	if ( com[0] == 'i' ){
	    insert(str);
	}
	else {
	    if (find(str)==1){
		printf("yes\n");
	    } else {
		printf("no\n");
	    }
	}
    }

    return 0;
}


#include<stdio.h>
#include<string.h>

#define M 10000/* your task*/
#define L 14

char H[M][L]; /* Hash Table */

int getChar(char ch){
  if ( ch == 'A') return 1;
  else if ( ch == 'C') return 2;
  else if ( ch == 'G') return 3;
  else if ( ch == 'T') return 4;
}

/* convert a string into an integer value */
long long getKey(char str[]){
  long long sum = 0, p = 1, i;
  for ( i = 0; i < strlen(str); i++ ){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key){ return /* your task */key%M; }
int h2(int key){ return /* your task */1+key%(M-1); }

int find(char str[]){
  /* your task */
  int i,x;
  for(i=0;i<M;i++){
    x = strcmp(H[i],str);
    if(x == 0) break;
  }
  return x;
}

void insert(char str[]){
  /* your task */
  long long key;
  int x,y,z,i=1;

  key = getKey(str);
  x = h1((int)key);
  if(H[x][0] != '\0'){
    y = h2((int)key);
    z = x;
    while(H[z][0] != '\0'){
      z = (x+i*y)%M;
      i++;
    }
    strcpy(H[z],str);
  }
  else strcpy(H[x],str);
}

int main(){
   int i, n, h;
    char str[L], com[9];
    for ( i = 0; i < M; i++ ) H[i][0] = '\0';

    scanf("%d", &n);

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

	if ( com[0] == 'i' ){

	    insert(str);
	} else {
	    if (find(str) == 0){
		printf("yes\n");
	    } else {
		printf("no\n");
	    }
	}
    }

    return 0;
}


#include<stdio.h>
#include<string.h>

#define M 100000000/* your task*/
#define L 12

char H[M][L]; /* Hash Table */
int c=0;

int find(char str[]){
  int i;
  for(i=0;i<c;i++){
    if(strcmp(str,H[i])==0)  return 1;
  }
    return 0;
}

int insert(char str[]){

  strcpy(H[c],str);
  c++;
  return 1;
}

int main(){
    int i, n, h;
    char str[L], com[9];

    scanf("%d", &n);

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

	if ( com[0] == 'i' ){
	    insert(str);
	} else {
	    if (find(str)==1){
		printf("yes\n");
	    } else {
		printf("no\n");
	    }
	}
    }

    return 0;
}


#include<stdio.h>
#include<string.h>

#define M 1046527
#define NIL (-1)
#define L 14

char H[M][L];

int getChar(char ch){
  if(ch=='A') return 1;
  else if(ch=='C') return 2;
  else if(ch =='G') return 3;
  else if(ch=='T') return 4;
  else return 0;
}

long long getKey(char str[]){
  long long sum=0,p=1,i;
  for(i=0;i<strlen(str);i++){
    sum+= p*(getChar(str[i]));
    p*=5;
  }
  return sum;
}

int h1(int key){return key % M;}
int h2(int key){return 1+(key%(M-1));}

int find(char str[]){
  long long key,i,h;
  key=getKey(str);
  for(i=0;;i++){
    h=(h1(key)+i*h2(key))%M;
    if(strcmp(H[h],str)==0) return 1;
    else if(strlen(H[h])==0) return 0;
  }
  return 0;
}

int insert(char str[]){
  long long key,i,h;
  key=getKey(str);
  for(i=0;;i++){
    h=(h1(key)+i*h2(key))%M;
    if(strcmp(H[h],str)==0) return 1;
    else if(strlen(H[h])==0){
      strcpy(H[h],str);
      return 0;
    }
  }
  return 0;
}

int main(){
  int i,n,h;
  char str[L],com[9];
  for(i=0;i<M;i++) H[i][0]='\0';
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%s %s",com,str);

    if(com[0]=='i'){
      insert(str);
    }else{
      if(find(str)){
	printf("yes\n");
      }else{
	printf("no\n");
      }
    }
  }
  return 0;
}


#include<stdio.h>
#include<string.h>

#define M 1000000
#define L 14

char H[M][L];

int getChar(char ch){
  if ( ch == 'A') return 1;
  else if ( ch == 'C') return 2;
  else if ( ch == 'G') return 3;
  else if ( ch == 'T') return 4;
}

long long getKey(char str[]){
  long long sum = 0, p = 1, i;
  for ( i = 0; i < strlen(str); i++ ){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key){ return key % M;}
int h2(int key){ return 1 + (key % (M - 1));}

int find(char str[]){
    /* your task */

  long long key, i, h;

  key = getKey(str);

  for ( i = 0;; i++){
    h = (h1(key) + i * h2(key)) % M;
    if( strcmp(H[h],str) == 0 ){
    return 1;

    }
    else if ( strlen(H[h]) == 0 ) {

     return 0;
    }
  }
  return 0;
}

int insert(char str[]){
  long long key, i, h;
  key = getKey(str);

  for ( i = 0; ; i++ ){
    h = (h1(key) + i * h2(key)) % M;
    if( strcmp(H[h],str) == 0 ) {
      return 1;
    }
    else if ( strlen(H[h]) == 0 ){

      strcpy(H[h], str);
      return 0;
    }
  }
  return 0;
}

int main(){
    int i, n, h;
    char str[L], com[9];
    for ( i = 0; i < M; i++ ) H[i][0] = '\0';

    scanf("%d", &n);

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

	if ( com[0] == 'i' ){
	    insert(str);
	} else {
	    if (find(str)){
		printf("yes\n");
	    } else {
		printf("no\n");
	    }
	}
    }
    return 0;
}


#include<stdio.h>
#include<string.h>

#define M 1000003
#define L 14

char H[M][L]; /* Hash Table */

int getChar(char ch){
  if ( ch == 'A') return 1;
  else if ( ch == 'C') return 2;
  else if ( ch == 'G') return 3;
  else if ( ch == 'T') return 4;
}

/* convert a string into an integer value */
long long getKey(char str[]){
  long long sum = 0, p = 1, i;
  for ( i = 0; i < strlen(str); i++ ){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key){ return key % M; }
int h2(int key){ return 1 + (key % (M-1)); }

int find(char str[]){
  int key = getKey(str), j, i = 0;
  for(;;){
    j = (h1(key) + i * h2(key)) % M;
    if(strcmp(H[j], str) == 0)
      return 1;
    else if(H[j][0] == '\0' || i >= M)
      return 0;
    else i++;
  }
}

void insert(char str[]){
  int i = 0, j, key = getKey(str);
  do {
    j = (h1(key) + i * h2(key)) % M;
    i++;
  } while (H[j][0] != '\0');
  for(i = 0; i < strlen(str); i++) H[j][i] = str[i];
}

int main(){
    int i, n, h;
    char str[L], com[9];
    for ( i = 0; i < M; i++ ) H[i][0] = '\0';

    scanf("%d", &n);

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

	if ( com[0] == 'i' ){
	    insert(str);
	} else {
	    if (find(str)){
		printf("yes\n");
	    } else {
		printf("no\n");
	    }
	}
    }

    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char const *argv[]) {
	int i, j, n, index = 0, foundFlag;
	char **a;
	scanf("%d", &n);
	a = malloc(sizeof(char *) * n);
	for (i = 0; i < n; i++) {
		a[i] = malloc(sizeof(char) * 13);
	}
	char command[7], arg[13];
	for (i = 0; i < n; i++) {
		foundFlag = 0;
		scanf("%s %s", command, arg);
		if (strcmp(command, "insert") == 0) {
			strcpy(a[index++], arg);
		} else if (strcmp(command, "remove") == 0) {
			for (j = 0; j < index; j++) {
				if (strcmp(a[j], arg) == 0) {
					strcpy(a[j], "\0");
				}
			}
		} else if (strcmp(command, "find") == 0) {
			for (j = 0; j < index; j++) {
				if (strcmp(a[j], arg) == 0) {
					foundFlag = 1;
				}
			}
			if (foundFlag) {
				printf("yes\n");
			} else {
				printf("no\n");
			}
		} else if (strcmp(command, "show") == 0) {
			for (j = 0; j < index; j++) {
				printf("%d %s\n", j, a[j]);
			}
		}
	}
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N_MAX 1000003
#define LENGTH 16

int get_key(char entry[LENGTH]){
	int times = 1, value, key, i;

	key = 0;
	for(i=0; i<strlen(entry); ++i){
		if      ('A' == entry[i]){
			key += 1*times;
		}else if('C' == entry[i]){
			key += 2*times;
		}else if('T' == entry[i]){
			key += 3*times;
		}else if('G' == entry[i]){
			key += 4*times;
		}
		times *= 5;
	}
	return key;
}
int hash1(int key){
	return key % N_MAX;
}
int hash2(int key){
	return 1 + (key % (N_MAX - 1));
}
int hash(int key, int times){
	return (hash1(key) + times*hash2(key)) % N_MAX;
}
int insert(char (*dictionary)[LENGTH], char entry[LENGTH]){
	int entry_key, i = 0, hash_value;

	entry_key = get_key(entry);
	while(1){
		hash_value = hash(entry_key, i);
		if(0 == strcmp(dictionary[hash_value], entry)){
			return 1;
		}else if(0 == strlen(dictionary[hash_value])){
			strcpy(dictionary[hash_value], entry);
			return 0;
		}
		++i;
	}
	return 0;
}
int search(char (*dictionary)[LENGTH], char entry[LENGTH]){
	int entry_key, i=0, hash_value;

	entry_key = get_key(entry);
	while(1){
		hash_value = hash(entry_key, i);
		if(0 == strcmp(dictionary[hash_value], entry)){
			return 1;
		}else if(0 == strlen(dictionary[hash_value])){
			return 0;
		}
		++i;
	}
	return 0;
}

int main(void){
	int n, i;
	char query[8], entry[LENGTH];
	char (*dictionary)[LENGTH];

	dictionary = malloc(sizeof(char)*N_MAX*LENGTH);
	for(i=0; i<N_MAX; ++i)
		dictionary[i][0] = '\0';
	scanf("%d", &n);
	for(i=0; i<n; ++i){
		scanf("%s %s", query, entry);
		if(0 == strcmp(query, "insert")){
			insert(dictionary, entry);
		}else if(0 == strcmp(query, "find")){
			if(search(dictionary, entry)){
				printf("yes\n");
			}else{
				printf("no\n");
			}
		}else{
			continue;
		}
	}
	free(dictionary);
	return 0;
}


/* Dictionary */
#include <stdio.h>
#include <string.h>

#define M 1046527
#define NIL (-1)
#define L 14

char H[M][L];

int getChar(char ch) {
    if(ch == 'A') return 1;
    else if(ch == 'C') return 2;
    else if(ch == 'G') return 3;
    else if(ch == 'T') return 4;
    else return 0;
}

long long getKey(char str[]) {
    long long sum=0, p=1, i;
    for(i=0; i < strlen(str); i++){
        sum += p*(getChar(str[i]));
        p *= 5;
    }
    return sum;
}

int h1(int key){ return key % M; }
int h2(int key){ return 1 + (key * (M - 1));}

int find(char str[]) {
    long long key, i, h;
    key = getKey(str);
    for(i=0;; i++){
        h = (h1(key) + i * h2(key)) % M;
        if(strcmp(H[h], str) == 0) return 1;
        else if(strlen(H[h]) == 0) return 0;
    }
    return 0;
}

int insert(char str[]) {
    long long key, i, h;
    key = getKey(str);
    for(i=0;; i++){
        h = (h1(key) + i * h2(key)) % M;
        if(strcmp(H[h], str) == 0) return 1;
        else if(strlen(H[h]) == 0) {
            strcpy(H[h], str);
            return 0;
        }
    }
    return 0;
}

int main() {
    int i, n, h;
    char str[L], com[9];
    for(i=0; i<M; i++){ H[i][0] = '\0'; }
    scanf("%d", &n);
    for(i=0; i<n; i++){
        scanf("%s %s", com, str);

        if(com[0] == 'i') {
            insert(str);
        } else {
            if(find(str)) {
                printf("yes\n");
            } else {
                printf("no\n");
            }
        }
    }
    return 0;
}


#include <stdio.h>
#include <string.h>

#define M 1046527
#define NIL (-1)
#define L 14

char H[M][L];

int getChar(char ch)
{
    if (ch == 'A') return 1;
    else if (ch == 'C') return 2;
    else if (ch == 'G') return 3;
    else if (ch == 'T') return 4;
    else return 0;
}

long long getKey(char str[]) {
    long long sum = 0, p = 1, i;
    for (int i = 0; i < strlen(str); i++) {
        sum += p*getChar(str[i]);
        p *= 5;
    }
    return sum;
}

int h1(int key)
{
    return key % M;
}

int h2(int key)
{
    return 1 + (key % (M - 1));
}

int find(char str[])
{
    long long key, i, h;
    key = getKey(str);
    for (i = 0; ; i++) {
        h = (h1(key) + i * h2(key)) % M;
        if (strcmp(H[h], str) == 0) {
            return 1;
        } else if (strlen(H[h]) == 0) {
            return 0;
        }
    }
    return 0;
}

int insert(char str[])
{
    long long key, i, h;
    key = getKey(str);
    for (i = 0; ; i++) {
        h = (h1(key) + i * h2(key)) % M;
        if (strcmp(H[h], str) == 0) {
            return 1;
        } else if (strlen(H[h]) == 0) {
            strcpy(H[h], str);
            return 0;
        }
    }
    return 0;
}

int main(int argc, char const *argv[])
{
    int i, n, h;
    char str[L], com[9];
    for (int i = 0; i < M; i++) {
        H[i][0] = '\0';
    }
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        scanf("%s %s", com, str);

        if (com[0] == 'i') {
            insert(str);
        } else {
            if (find(str)) {
                printf("yes\n");
            } else {
                printf("no\n");
            }
        }
    }

    return 0;
}


#include <stdio.h>
#include <string.h>

#define M 1000001
#define NIL -1

int T[M];
char Q[M];
int tail = 0;

int h1(int k)
{
	return k % M;
}

int h2(int k)
{
	return (1 + (k % (M-1)));
}

int h(int k, int i)
{
	return (h1(k) + i * h2(k)) % M;
}

int insert(int key)
{
	int i = 0, p;
	while (1) {
		if (i >= M)
			return NIL;
		p = h(key, i++);
		if (NIL == T[p]) {
			T[p] = key;
			return p;
		}
	}
}

int find(int key)
{
	int i = 0, p;
	while (1) {
		if (i >= M)
			return NIL;
		p = h(key, i++);
		if (key == T[p]) {
			return p;
		} else if (NIL == T[p]) {
			return NIL;
		}
	}
}

int get_char(char ch)
{
	switch (ch) {
	case 'A': return 1;
	case 'C': return 2;
	case 'G': return 3;
	case 'T': return 4;
	default: return 0;
	}
}

int get_key(char *cargs)
{
	int key = 0, p = 1;
	while (*cargs != 0) {
		key += p * get_char(*cargs);
		p *= 5;
		cargs++;
	}
	return key;
}

int main(int argc, char *argv[])
{
	int i, n, key;
	char cmd[10], cargs[12];
	memset(T, NIL, sizeof(T));

	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		scanf("%s %s", cmd, cargs);
		key = get_key(cargs);

		if (!strcmp(cmd, "insert")) {
			insert(key);
		} else if (!strcmp(cmd, "find")) {
			if (NIL != find(key)) {
				//printf("yes\n");
				Q[tail++] = 1;
			} else {
				//printf("no\n");
				Q[tail++] = 0;
			}
		}
	}

#if 1
	for (i = 0; i < tail; i++) {
		if (Q[i]) {
			printf("yes\n");
		} else {
			printf("no\n");
		}
	}
#endif

	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int find(char *,int);
char t[100000][12],q[100000][12],m[100000][12];
int main() {
	int n,i,j=0;
	char s[12],f[12];
	scanf("%d",&n);

	for(i=0;i<n;i++) {

scanf("%s%s",t[i],q[i]);
}
	for(i=0;i<n;i++) {

if(strcmp(t[i],"insert")==0) {
strcpy(m[j],q[i]); j++;
}
else {
	if(find(q[i],j)==1) printf("yes\n");
	else printf("no\n");
	}
	}
	return 0;
	}

	int find(char *p,int j) {
		int i;
		for(i=0;i<j;i++) {
			if(strcmp(m[i],p)==0) return 1;
		}
		return 0;
	}


#include<stdio.h>
#include<string.h>
char ha[1<<21],k[256];
int main(int argc,char *argv[]){
  int i,flg;
  int val,s,t;
  char c;
  k['T'] = 1;
  k['G'] = 2;
  k['C'] = 3;
  k['A'] = 4;

  scanf("%d", &val);
  getchar();
  for(i=0;i<val;i++){
    s = 0;
    t = 1;
    flg = ( c = getchar() ) == 'i';
    while ( ( c = getchar() ) != ' ' );
    while ( ( c = k[getchar()] ) != 0 ) {
      s += c * t;
      t *= 4;
    }
    if (flg) ha[s] = 1; else puts( ha[s] ? "yes" : "no" );
  }
  return 0;
}


#include <stdio.h>
#include <string.h>

const int N = 16777217;

int n;
char data[16777217];

int conv(char c){
  switch(c){
  case 'A':
    return 1;
    break;
  case 'C':
    return 2;
    break;
  case 'G':
    return 3;
    break;
  case 'T':
    return 4;
    break;
  }
}

int hash(char str[10]){
  int res = 0, size = strlen(str), r = 1, i;
  for(i=size-1;i>=0;i--){
    res += r * conv(str[i]);
    r *= 4;
  }
  return res;
}

void insertElm(int d){
  data[d] = 1;
}

char findElm(int d){
  return data[d];
}

main(){
  char ope[13], str[13];
  int i;
  memset(data, 0, 16777217);
  scanf("%d", &n);
  for(i=0;i<n;i++){
    scanf("%s", ope);
    scanf("%s", str);
    if(!strcmp(ope, "insert")) insertElm(hash(str));
    else printf("%s\n", (findElm(hash(str)) ? "yes" : "no"));
  }
  return 0;
}


#include<stdio.h>
#include<string.h>

#define M 1000000/* your task*/
#define NIL (-1)
#define L 14

char H[M][L]; /* Hash Table */

int getChar(char ch){
  if ( ch == 'A') return 1;
  else if ( ch == 'C') return 2;
  else if ( ch == 'G') return 3;
  else if ( ch == 'T') return 4;
}

/* convert a string into an integer value */
long long getKey(char str[]){
  long long sum = 0, p = 1, i;
  for ( i = 0; i < strlen(str); i++ ){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key){ return key % 13; }
int h2(int key){ return 1 + (key % 11); }

int find(char str[]){
  int x = h1(getKey(str));
  int y = h2(getKey(str));
  int i;
  for(i=x;i<M;i+=((x+i*y)%13)){
    if(strcmp(H[i],str) == 0) return 1;
    if(strcmp(H[i],"\0") == 0) break;

  }
  return 0;
}

int insert(char str[]){
  int i;
  int x = h1(getKey(str));
  int y = h2(getKey(str));

  for(i=x;i<M;i+=((x+i*y)%13)){
    if(strcmp(H[i],"\0") == 0){
      strcpy(H[i],str);
      return 1;
    }
  }
  return NIL;
}

int main(){
  int i, n, h;
  char str[L], com[9];
  for ( i = 0; i < M; i++ ) H[i][0] = '\0';
    scanf("%d", &n);

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

    if ( com[0] == 'i' ){
      insert(str);
    } else {
      if (find(str)){
        printf("yes\n");
      } else {
        printf("no\n");
      }
    }
  }

  return 0;
}


#include<stdio.h>
#include<string.h>

#define M 1000007/* your task*/
#define NIL (-1)
#define L 14

char H[M][L]; /* Hash Table */

int getChar(char ch){
  if ( ch == 'A') return 1;
  else if ( ch == 'C') return 2;
  else if ( ch == 'G') return 3;
  else if ( ch == 'T') return 4;
  return -1;
}

/* convert a string into an integer value */
long long getKey(char str[]){
  long long sum = 0, p = 1, i;
  for ( i = 0; i < strlen(str); i++ ){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key){ return key%M; }
int h2(int key){ return key%L; }

int find(char str[]){
  int key = (int)(getKey(str)&0x7fffffff);
  return (H[h1(key)][h2(key)]=='\0'?0:1);
}

int insert(char str[]){
  int key = (int)(getKey(str)&0x7fffffff);
  H[h1(key)][h2(key)] = 't';
  return 0;
}

int main(){
    int i, n, h;
    char str[L], com[9];
    for ( i = 0; i < M; i++ ) H[i][0] = '\0';

    scanf("%d", &n);

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

	if ( com[0] == 'i' ){
	    insert(str);
	} else {
	    if (find(str)){
		printf("yes\n");
	    } else {
		printf("no\n");
	    }
	}
    }

    return 0;
}


#include<stdio.h>
#include<string.h>

#define M (244140625/8+1)
#define NIL (-1)
#define L 14

unsigned char H[M]; /* Hash Table */

int getChar(char ch){
  if ( ch == 'A') return 1;
  else if ( ch == 'C') return 2;
  else if ( ch == 'G') return 3;
  else if ( ch == 'T') return 4;
}

/* convert a string into an integer value */
long long getKey(char str[]){
  long long sum = 0, p = 1, i;
  for ( i = 0; i < strlen(str); i++ ){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key){ return 0/* your task */; }
int h2(int key){ return 0/* your task */; }

int find(char str[]){
	long long t = getKey(str);
	return H[t/8] & (1<<t%8);
}

int insert(char str[]){
	long long t = getKey(str);
	H[t/8] |= (1<<t%8);
}

int main(){
    int i, n, h;
    char str[L], com[9];
    for ( i = 0; i < M; i++ ) H[i] = 0;

    scanf("%d", &n);

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

	if ( com[0] == 'i' ){
	    insert(str);
	} else {
	    if (find(str)){
		printf("yes\n");
	    } else {
		printf("no\n");
	    }
	}
    }

    return 0;
}


#include <stdio.h>
#include <string.h>

#define MAX 16777215

int Hash[MAX];
char str[13];

int getChar(char);
int getKey();
int find();
void insert();

int main(){
  int n;
  char com[7];

  scanf("%d" ,&n);
  while(n--){
    scanf("%s %s" ,com ,str);

    if(com[0] == 'i'){
      insert();
    }else{
      if(find()){
	puts("yes");
      }else{
	puts("no");
      }
    }
  }

  return 0;
}

int getChar(char ch){
  if(ch == 'A'){
    return 1;
  }else if(ch == 'C'){
    return 2;
  }else if(ch == 'G'){
    return 3;
  }
  return 4;
}

int getKey(){
  int sum = 0, p = 1, i;

  for(i = 0 ; i < strlen(str) ; i++){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int find(){
  int key = getKey();

  if(Hash[key]){
    return 1;
  }

  return 0;
}

void insert(){
  Hash[getKey()] = 1;
}


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>

#define M 100000
#define NIL (-1)
#define L 14

char H[M][L]; /* Hash Table */

int getChar(char ch){
	if (ch == 'A') return 1;
	else if (ch == 'C') return 2;
	else if (ch == 'G') return 3;
	else if (ch == 'T') return 4;
}

/* convert a string into an integer value */
long long getKey(char str[]){
	long long sum = 0, p = 1, i;
	for (i = 0; i < strlen(str); i++){
		sum += p*(getChar(str[i]));
		p *= 5;
	}
	return sum;
}

int h1(int key){ return key%M; }
int h2(int key){ return 1 + key % (M - 1); }

int find(char str[]){
	long long ii = getKey(str);
	int i;
	int hh1 = h1(ii);
	int hh2 = h2(ii);
	for (i = 0; i<M; i++){
		if (H[(hh1 + i*hh2) % M][0] == '\n'){}
		else if (strcmp(H[(hh1 + i*hh2) % M], str) == 0){ return 1; }
	}
	return 0;
	/* your task */
	/*
	01. i = 0
		02. repeat j = h(k, i)
		03. if T[j] == k
		04. return j
		05. else
		06. i = i + 1
		07. until T[j] == NIL or i == m
		08. return NIL
	*/
}

int insert(char str[]){
	long long ii = getKey(str);
	int hh1 = h1(ii);
	int hh2 = h2(ii);
	int i;
	for (i = 0; i<M; i++){
		if (H[(hh1 + i*hh2) % M][0]== '\0'){
			strcpy(H[(hh1 + i*hh2)%M], str);
			break;
		}
	}
	return 0;
	/* your task */

	/*
	int i = 0;
	do{
	  j = h(k, i)
	    if( T[j] == NIL){
	      T[j] = k;
	      return j;
	    }else{
	      int i = i + 1;
	    }
	} while (i == m);
	*/
}

int main(){
	int i, n, h;
	char str[L], com[9];
	for (i = 0; i < M; i++) H[i][0] = '\0';

	scanf("%d", &n);

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

		if (com[0] == 'i'){
			insert(str);
		}
		else {
			if (find(str)){
				printf("yes\n");
			}
			else {
				printf("no\n");
			}
		}
	}

	return 0;
}


#include<stdio.h>
#include<string.h>

#define M 333331
#define NIL (-1)
#define L 14

char H[M][L]; /* Hash Table */

int getChar(char ch){
  if ( ch == 'A') return 1;
  else if ( ch == 'C') return 2;
  else if ( ch == 'G') return 3;
  else if ( ch == 'T') return 4;
}

/* convert a string into an integer value */
long long getKey(char str[]){
  long long sum = 0, p = 1, i;
  for ( i = 0; i < strlen(str); i++ ){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key){
  return key%M;
}
int h2(int key){
  return 1+(key%(M-1));
}

int find(char str[]){
    int i,j,k;
    k=getKey(str);
    for(i=0;;){
      j=(h1(k)+i*h2(k))%M;
      if(strcmp(H[j],str)==0){
	return 1;
      }
      else i=i+1;
      if(i!=M || H[j][0]!=NIL)break;
    }
    return NIL;
}

int insert(char str[]){
  int i,j,k;
  k=getKey(str);
  for(i=0;;){
    j=(h1(k)+i*h2(k))%M;
    if(H[j][0]==NIL){
      strcpy(H[j],str);
      return j;
    }
    else i=i+1;
    if(i!=M)break;
  }
}

int main(){
  int i, n, h;
  char str[L], com[9];
  for ( i = 0; i < M; i++ ) H[i][0] = NIL;

  scanf("%d", &n);

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

    if ( com[0] == 'i' ){
      insert(str);
    } else {
      if (find(str)==1){
	printf("yes\n");
      } else {
	printf("no\n");
      }
    }
  }

  return 0;
}


#include<stdio.h>
#include<string.h>

#define M 100000
#define L 14

char H[M][L];

int getChar(char ch){
  if ( ch == 'A') return 1;
  else if ( ch == 'C') return 2;
  else if ( ch == 'G') return 3;
  else return 4;
}
long long getKey(char str[]){
  long long sum = 0, p = 1, i;
  for ( i = 0; i < strlen(str); i++ ){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key){ return key % M;}
int h2(int key){ return 1 + key % (M - 1);}

int find(char str[]){
  long long trip;
  int i;
  for(i = 0; i < M; i++){
    trip = (h1(getKey(str))+ i * h2(getKey(str))) % M;
    if(strcmp(H[trip], str) == 0) return 1;
    if(H[trip][0] == '\0') return 0;
  }
  return 0;
}

int insert(char str[]){
  int  i = 0;
  long long ago;

  while(1){
   ago = (h1(getKey(str))+ i * h2(getKey(str))) % M;
    if(H[ago][0] == '\0'){
      strcpy(H[ago], str);
      break;
    }
    i++;
  }
  return 0;
}
int main(){
    int i, n, h;
    char str[L], com[9];
    for ( i = 0; i < M; i++ ) H[i][0] = '\0';
    scanf("%d", &n);
    for ( i = 0; i < n; i++ ){
	scanf("%s %s", com, str);
	if ( com[0] == 'i' ){
	    insert(str);
	} else {
	    if (find(str)){
		printf("yes\n");
	    } else {
		printf("no\n");
	    }
	}
    }
    return 0;
}


#include<stdio.h>
#include<string.h>

#define M 1100000
#define L 14

char H[M][L];

int getChar(char ch){
  if ( ch == 'A') return 1;
  else if ( ch == 'C') return 2;
  else if ( ch == 'G') return 3;
  else if( ch == 'T') return 4;
}

long long getKey(char str[]){
  long long sum = 0, p = 1, i;
  for ( i = 0; i < strlen(str); i++ ){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key){ return key % M;}
int h2(int key){ return 1 + key % (M - 1);}

int find(char str[]){
  long long trip;
  int i = 0;
  long long sum = getKey(str);

  while(1){
    trip = (h1(sum)+ i * h2(sum)) % M;
    if(strcmp(H[trip], str) == 0) return 1;
    if(H[trip][0] == '\0') return 0;
    i++;
  }
  return 0;
}

void insert(char str[]){
  int  i = 0;
  long long ago;
  long long sum = getKey(str);

  while(1){
    ago = (h1(sum)+ i * h2(sum)) % M;
    if(H[ago][0] == '\0' || strcmp(H[ago],str) == 0){
      strcpy(H[ago], str);
      break;
    }
    i++;
  }
}

int main(){
  int i, n, h;
  char str[L], com[9];
  for ( i = 0; i < M; i++ ) H[i][0] = '\0';
  scanf("%d", &n);
  for ( i = 0; i < n; i++ ){
    scanf("%s %s", com, str);
    if ( com[0] == 'i' ){
      insert(str);
    } else {
      if (find(str)){
	printf("yes\n");
      } else {
	printf("no\n");
      }
    }
  }
  return 0;
}


#include<stdio.h>
#include<string.h>

#define M 1000000
#define NIL (-1)
#define L 14

char Hash[M][L]; /* Hash Table */
int n;

int getChar(char ch){
    int code = 0;
    if ( ch == 'A') code = 1;
    else if ( ch == 'C') code = 2;
    else if ( ch == 'G') code = 3;
    else if ( ch == 'T') code = 4;
    return code;
}

/* convert a string into an integer value */
long long getKey(char str[]){
    long long sum = 0, p = 1, i;
    for ( i = 0; i < strlen(str); i++ ){
        sum += p*(getChar(str[i]));
        p *= 5;
    }
    return sum;
}

int h1(int key){ return key%M; }
int h2(int key){ return key%(M-1) + 1; }

int find(char str[]){
    int i = 0, flag = 0;
    int h_1, h_2, h_3;
    long long key = getKey(str);

    h_1 = h1(key);
    h_2 = h2(key);

    while (1) {

        h_3 = (h_1 + i * h_2) % M;
        if(Hash[h_3][0] == '\0') break;     // 空のとき

        else {
            if(strcmp(Hash[h_3], str) == 0) {
                flag = 1;
                break;
            }
            else continue;
        }
        i++;
    }
    return flag;
}

void insert(char str[]){
    int i = 0;
    int h_1, h_2, h_3;
    long long key = getKey(str);

    h_1 = h1(key);
    h_2 = h2(key);

    while (1) {
        h_3 = (h_1 + i * h_2) % M;
        if (Hash[h_3][0] == NIL){
            strcpy(Hash[h_3], str);
            break;
        }
        else continue;
        i++;
    }
}

int main(){
    int i, h, n;
    char str[L], com[9];
    for ( i = 0; i < M; i++ ) Hash[i][0] = '\0';

    scanf("%d", &n);

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

        if ( com[0] == 'i' ){
            insert(str);
        } else {
            if (find(str)){
                printf("yes\n");
            } else {
                printf("no\n");
            }
        }
    }

    return 0;
}


#include<stdio.h>
#include<string.h>

#define M 4000000
#define L 14

char H[M][L];

int getChar(char ch) {
	if (ch == 'A') {
		return 1;
	}
	else if (ch == 'C') {
		return 2;
	}
	else if (ch == 'G') {
		return 3;
	}
	else if (ch == 'T') {
		return 4;
	}
	return 0;
}

int getKey(char str[]) {
	int sum = 0, p = 1, i;
	for (i = 0; i < strlen(str); i++) {
		sum += p * (getChar(str[i]));
		p *= 5;
	}
	return sum;
}

int h1(int key) {
	return key % M;
}

int h2(int key) {
	return 1 + (key % (M - 1));
}

int getHashValue(char str[]) {
	int hashval, i;

	for (i = 0; i < M; i++) {
		hashval = (h1(getKey(str)) + i * h2(getKey(str))) % M;
		if (H[hashval] != '\0') break;
	}

//	do {
//		hashval = (h1(getKey(str)) + i * h2(getKey(str))) % M;
//		i++;
//	} while (H[hashval] == '\0');

	return hashval;
}

int find(char str[]) {
	return !strcmp(str, H[getHashValue(str)]);
}

void insert(char str[]) {
	strcpy(H[getHashValue(str)], str);
}

int main() {
	int i, n;
	char str[L], com[9];

	for (i = 0; i < M; i++) {
		H[i][0] = '\0';
	}

	scanf("%d", &n);

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

		if (com[0] == 'i') {
			insert(str);
		} else {
			if (find(str)) {
				printf("yes\n");
			} else {
				printf("no\n");
			}
		}
	}

	return 0;
}


#include<stdio.h>
#include<string.h>

#define M 1000003
#define L 14

char H[M][L];

int getChar(char ch) {
	if (ch == 'A') {
		return 1;
	}
	else if (ch == 'C') {
		return 2;
	}
	else if (ch == 'G') {
		return 3;
	}
	else if (ch == 'T') {
		return 4;
	}
	return 0;
}

long long getKey(char str[]) {
	long long sum = 0, p = 1, i;
	for (i = 0; i < strlen(str); i++) {
		sum += p * (getChar(str[i]));
		p *= 5;
	}
	return sum;
}

int h1(int key) {
	return key % M;
}

int h2(int key) {
	return 1 + (key % (M - 1));
}

int getHashValue(char str[]) {
	long long hashval, i = 0;

	do {
		hashval = (h1(getKey(str)) + i * h2(getKey(str))) % M;
		i++;
	} while (H[hashval] == '\0');

	return hashval;
}

int find(char str[]) {
	return !strcmp(str, H[getHashValue(str)]);
}

void insert(char str[]) {
	strcpy(H[getHashValue(str)], str);
}

int main() {
	int i, n;
	char str[L], com[9];

	for (i = 0; i < M; i++) {
		H[i][0] = '\0';
	}

	scanf("%d", &n);

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

		if (com[0] == 'i') {
			insert(str);
		} else {
			if (find(str)) {
				printf("yes\n");
			} else {
				printf("no\n");
			}
		}
	}

	return 0;
}


#include<stdio.h>
#include<string.h>

#define M 10000000
#define L 14

char H[M][L]; /* Hash Table */

int getChar(char ch){
  if ( ch == 'A') return 1;
  else if ( ch == 'C') return 2;
  else if ( ch == 'G') return 3;
  else if ( ch == 'T') return 4;
}

/* convert a string into an integer value */
long long getKey(char str[]){
  long long sum = 0, p = 1, i;
  for ( i = 0; i < strlen(str); i++ ){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key){
  key = key%M;
  return key;
}
int h2(int key){
  key = 1+key%(M-1);
  return key;
}

int find(char str[]){
  long long sum,key2,i;
  sum = getKey(str);
  for(i=0;i<20;i++){
    key2=(h1(sum)+i*h2(sum))%M;
    if(strcmp(H[key2],str)==0)return 1;
  }
  return 0;
}

int insert(char str[]){
  long long sum,key2,i;
  sum = getKey(str);
  for(i=0;i<20;i++){
    key2=(h1(sum)+i*h2(sum))%M;
    if(strlen(H[key2])==0){
      strcpy(H[key2],str);
      return 0;
    }
  }
}

int main(){
  int i, n, h;
  char str[L], com[9];
  for ( i = 0; i < M; i++ ) H[i][0] = '\0';

  scanf("%d", &n);

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

    if ( com[0] == 'i' ){
      insert(str);
    } else {
      if (find(str)){
	printf("yes\n");
      } else {
	printf("no\n");
      }
    }
  }

  return 0;
}


#include<stdio.h>
#include<string.h>

#define M 1000000
#define L 14

char H[M][L]; /* Hash Table */

int getChar(char ch){
  if ( ch == 'A') return 1;
  else if ( ch == 'C') return 2;
  else if ( ch == 'G') return 3;
  else if ( ch == 'T') return 4;
}

/* convert a string into an integer value */
long long getKey(char str[]){
  long long sum = 0, p = 1, i;
  for ( i = 0; i < strlen(str); i++ ){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key){
  int h;
  h =  key % M;
  return h;
}
int h2(int key){
  int h;
  h = 1 + key % (M-1);
  return h;
}

int find(char str[]){
  int i=0,j=0;
  int a=0,b=0,key=0;
  key=getKey(str);
  a = h1(key);
  b = h2(key);
  for(i=a;j<M;i+=b)
    {
      if(i >= M)
         i = i % M;
      if(strcmp(H[i],"")==0)
         return 0;
      else if(strcmp(H[i],str)== 0)
         return 1;
      j++;
    }
  return 0;

}

int insert(char str[]){
  int i=0,j=0;
  int a=0,b=0,key=0;
  key = getKey(str);
  a = h1(key);
  b = h2(key);

  for(i=a;j<M;i+=b)
    {
    if(i>=M)
      i = i % M;
    if(strcmp(H[i],"")==0)
     {
      strcpy(H[i],str);
      return 1;
      }
    j++;
    }
  return 0;

}

int main(){
    int i, n, h;
    char com[9],str[L];
    for ( i = 0; i < M; i++ )
      H[i][0] = '\0';
    scanf("%d", &n);
    for ( i = 0; i < n; i++ ){
	scanf("%s %s", com, str);
	if ( com[0] == 'i' ){
	    insert(str);
	} else {
	    if (find(str)){
		printf("yes\n");
	    } else {
		printf("no\n");
	    }
	}
    }

    return 0;
}


#include<stdio.h>
#include<string.h>

#define M 1000005
#define L 14

char HS[M][L];

int getChar(char ch){
  if ( ch == 'A') return 1;
  else if ( ch == 'C') return 2;
  else if ( ch == 'G') return 3;
  else if ( ch == 'T') return 4;
}

long long getKey(char str[]){
  long long sum = 0, p = 1, i;
  for ( i = 0; i < strlen(str); i++ ){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key){ return key % M; }
int h2(int key){ return 1 + key % (M-1); }

int find(char str[]){

  long long fin;
  int i = 0;
  long long sum = getKey(str);

  while(1){
    fin = (h1(sum) + h2(sum)*i) %M;
    if(strcmp(HS[fin],str) == 0) return 1;
    if(HS[fin][0] == '\0') return 0;
    i++;
  }

  return 0;

}

void insert(char str[]){

 int  i = 0;
  long long ser;
  long long sum = getKey(str);

  while(1){
    ser = (h1(sum) + h2(sum) *i) %M;
    if(HS[ser][0] == '\0' || strcmp(HS[ser],str) == 0){
      strcpy(HS[ser],str);
      break;
    }

    i++;
  }

}

int main(){
    int i, n, h;
    char str[L], com[9];
    for ( i = 0; i < M; i++ ) HS[i][0] = '\0';

    scanf("%d", &n);

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

    if ( com[0] == 'i' ){
        insert(str);
    }
    else {
        if (find(str)){
        printf("yes\n");
        } else {
        printf("no\n");
        }
    }
 }

    return 0;
}


#include<stdio.h>
#include<string.h>
#define M 1046527
#define NIL (-1)
#define L 14
char H[M][L];
int getChar(char ch){
    if(ch=='A') return 1;
    else if(ch=='C') return 2;
    else if(ch=='G') return 3;
    else if(ch=='T') return 4;
    else return 0;
}
long long getkey(char str[]){
    long long sum=0,p=1,i;
    for(i=0;i<strlen(str);i++){
        sum+=p*(getChar(str[i]));
        p*=5;
    }
    return sum;
}
int h1(int key){
    return key%M;
}
int h2(int key){
    return 1+(key%(M-1));
}
int find(char str[]){
    long long key,i,h;
    key=getkey(str);
    for(i=0;;i++){
        h=(h1(key)+i*h2((key)))%M;
        if(strcmp(H[h],str)==0) return 1;
        else if(strlen(H[h])==0) return 0;
    }
    return 0;
}
int insert(char str[]){
    long long key,i,h;
    key=getkey(str);
    for(i=0;;i++){
        h=(h1(key)+i*h2(key))%M;
        if(strcmp(H[h],str)==0) return 1;
        else if(strlen(H[h])==0){
            strcpy(H[h],str);
            return 0;
        }
    }
    return 0;
}

int main() {
    int i,n,h;
    char str[L],com[9];
    for(i=0;i<M;i++){
        H[i][0]='\0';
    }
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%s %s",com,str);
        if(com[0]=='i'){
            insert(str);
        }else{
            if(find(str)){
                printf("yes\n");
            }else{
                printf("no\n");
            }
        }
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>

#define M 1000010
#define L 20
int answer[M];
char dictionary[M][L];
int getChar(char ch)
{
	if(ch=='A')
		return 1;
	if(ch=='C')
		return 2;
	if(ch=='G')
		return 3;
	if(ch=='T')
		return 4;
	return 0;
}
long long int getkey(char str[])
{
	long long int sum=0,p=1,i;
	for(i=0;i<=strlen(str)-1;i++)
	{
		sum+=p*getChar(str[i]);
		p*=5;
	}
	return sum;
}
int h1(int key)
{
	return key%M;
}
int h2(int key)
{
	return 1+(key%(M-1));
}
int find(char str[20])
{
	long long int key,i,h;
	key=getkey(str);
	for(i=0;;i++)
	{
		h=(h1(key)+i*h2(key))%M;
		if(strcmp(str,dictionary[h])==0)
			return 1;
		if(strlen(dictionary[h])==0)
			return 0;
	}
	return 0;
}
void insert(char str[20])
{
	long long int key,i,h;
	key=getkey(str);
	for(i=0;;i++)
	{
		h=(h1(key)+i*h2(key))%M;
		if(strcmp(str,dictionary[h])==0)
			return;
		if(strlen(dictionary[h])==0)
		{
			strcpy(dictionary[h],str);
			return;
		}
	}
	return;
}
int main()
{
	char str[L],com[9];
	int n,i,answern=0;
	//for(i=0;i<M;i++)
	//	dictionary[i][0]='\0';

	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%s %s",com,str);
		if(strcmp(com,"insert")==0)
			insert(str);
		if(strcmp(com,"find")==0)
		{
			answern++;
			answer[answern]=find(str);
		}
	}
	for(i=1;i<=answern;i++)
	{
		if(answer[i])
			printf("yes\n");
		else
			printf("no\n");
	}
	return 0;
}


#include <stdio.h>
#include<string.h>
#define M 1046527
#define L 14

char H[M][L];

int get(char *c){
    int sum=0,p=1;
    for(int i=0;i<strlen(c);i++){
        if(c[i]=='A') sum+=1*p;
        else if(c[i]=='C') sum+=2*p;
        else if(c[i]=='G') sum+=3*p;
        else               sum+=4*p;
        p*=5;
    }
    return sum;
}

int h1(int key){ return key%M;}
int h2(int key){ return 1+(key%(M-1));}

void insert(char *c){
    long long i,h,key=get(c);
    i=0;
    while(1){
        h=(h1(key)+i*h2(key))%M;
        if(!strcmp(H[h],c)) return;
        else if(H[h][0]=='\0'){
            strcpy(H[h],c);
            return;
        }
        i++;
    }
    return;
}

int find(char *c){
    long long i,h,key=get(c);
    i=0;
    while(1){
        h=(h1(key)+i*h2(key))%M;
        if(!strcmp(H[h],c)) return 1;
        else if(H[h][0]=='\0') return 0;
        i++;
    }
    return 0;
}

int main(void){
    int n,i;
    char op[8],arr[4];

    for(i=0;i<M;i++){
        H[i][0]='\0';
    }

    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%s %s",op,arr);
        if(op[0]=='i'){
            insert(arr);
        }
        else {
            if(find(arr)) printf("yes\n");
            else printf("no\n");
        }
    }
    return 0;
}


#include <stdio.h>
#include <string.h>

#define MAX 244140625

int Hash[MAX];
char s[13];

int getChar(char);
int getKey();
int find();
void insert();

int main(){
  int n;
  char com[7];

  scanf("%d" ,&n);
  while(n--){
    scanf("%s %s" ,com ,s);

    if(com[0] == 'i'){
      insert();
    }else{
      if(find()){
	puts("yes");
      }else{
	puts("no");
      }
    }
  }

  return 0;
}

int getChar(char ch){
  if(ch == 'A'){
    return 1;
  }else if(ch == 'C'){
    return 2;
  }else if(ch == 'G'){
    return 3;
  }
  return 4;
}

int getKey(){
  int sum = 0, p = 1, i;

  for(i = 0 ; i < strlen(s) ; i++){
    sum += p*(getChar(s[i]));
    p *= 5;
  }
  return sum;
}

int find(){
  int key = getKey();

  if(Hash[key]){
    return 1;
  }

  return 0;
}

void insert(){
  Hash[getKey()] = 1;
}


#include <stdio.h>
#include <string.h>
#define N 1046557
#define M 1000000
#define L 14

char H[N][L]; /* Hash Table */

int getChar(char ch){
  if ( ch == 'A') return 1;
  else if ( ch == 'C') return 2;
  else if ( ch == 'G') return 3;
  else if ( ch == 'T') return 4;
}

/* convert a string into an integer value */
long long getKey(char str[]){
  long long sum = 0, p = 1, i;
  for ( i = 0; i < strlen(str); i++ ){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key){ return (key % N); }
int h2(int key){ return 1+(key % (N-1)); }

int insert(char str[]){
    long long i = 0, j, key;
    key = getKey(str);
    while(1){
	j = (h1(key) + i*h2(key)) % N;
	if(strcmp(H[j], str) == 0) return 1;
	if(H[j][0] == '\0'){
	    strcpy(H[j], str);
	    return j;
	}else i++;
    }
}

int find(char str[]){
    long long i = 0, j, k, key;
    key = getKey(str);
    while(1){
	k = (h1(key) + i*h2(key));
	j = k % N;
	if(strcmp(H[j], str) == 0 ) return 1;
	else if(H[j][0] == '\0' ) return 0;
	else i++;
    }
}

//--------------------------------------------------------------------
int main(){
    int i, n, h;
    char str[L], com[9];
    for ( i = 0; i < M; i++ ) H[i][0] = '\0';

    scanf("%d", &n);

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

	if ( com[0] == 'i' ){
	    insert(str);
	} else {
	    if (find(str)){
		printf("yes\n");
	    } else {
		printf("no\n");
	    }
	}
    }

    return 0;
}


#include<stdio.h>
#include<string.h>
#define M 1046527
#define NIL (-1)
#define L 14
char H[M][L];

int getChar(char ch){
  if ( ch == 'A') return 1;
  else if ( ch == 'C') return 2;
  else if ( ch == 'G') return 3;
  else if ( ch == 'T') return 4;
}

long long getKey(char str[]){
  long long sum = 0, p = 1, i;
  for ( i = 0; i < strlen(str); i++ ){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key){ return key % M; }
int h2(int key){ return 1 + (key % (M-1)); }

int fnd(char str[]){
  long long key, i, h;
  key = getKey(str);
  for ( i = 0;; i++){
    h = (h1(key)+i*h2(key))%M;
    if( strcmp(H[h],str) == 0 ) return 1;
    else if ( strlen(H[h]) == 0 ) return 0;
  }
  return 0;
}

int insert(char str[]){
  long long key, i, h;
  key = getKey(str);
  for ( i = 0; ; i++ ){
    h = (h1(key)+i*h2(key))%M;
    if( strcmp(H[h],str) == 0 ) return 1;
    else if ( strlen(H[h]) == 0 ){
      strcpy(H[h], str);
      return 0;
    }
  }
  return 0;
}
int main(){
  int g, n, h;
  char str[L], com[9];
  for ( g = 0; g < M; g++ ) H[g][0] = '\0';
  scanf("%d", &n);
  for ( g = 0; g < n; g++ ){
    scanf("%s %s", com, str);

    if ( com[0] == 'i' ){
      if (insert(str)){

      }
    } else {
      if (fnd(str) ){
	printf("yes\n");
      } else {
	printf("no\n");
      }
    }
  }
  return 0;
}


#include <stdio.h>
#include <string.h>

#define M 1046257
#define NIL (-1)
#define L 14

char H[M][L];

// 文字から数値に変換
int getChar(char ch) {
  if ( ch == 'A' ) return 1;
  else if ( ch == 'C' ) return 2;
  else if ( ch == 'G' ) return 3;
  else if ( ch == 'T' ) return 4;
  return 0;
}

// 文字列から数値へ変換して key を生成する
long long getKey(char str[]) {
  long long sum =0, p = 1, i;
  for(i=0;i<strlen(str);i++) {
    sum += p*(getChar(str[i]));
    p*= 5;
  }
  return sum;
}

int h1(int key){ return key % M; }
int h2(int key){ return 1 + (key % (M-1)); }

int find(char str[]) {
  long long key, i, h;
  key = getKey(str); // 文字列を数値に変換
  for(i=0; ;i++) {
    h = (h1(key) + i*h2(key)) %M;
    if( strcmp(H[h],str) == 0) return 1;
    else if ( strlen(H[h]) == 0 ) return 0;
  }
  return 0;
}

int insert(char str[]) {
  long long key, i, h;
  key = getKey(str); // 文字列を数値に変換
  for(i=0; ; i++) {
    h = (h1(key) + i * h2(key)) %M;
    if(strcmp(H[h],str) == 0) return 1;
    else if(strlen(H[h]) == 0) {
      strcpy(H[h],str);
      return 0;
    }
  }
  return 0;
}

int main(void) {
  int i, n;
  char str[L], com[9];
  for(i=0;i<M;i++) H[i][0] = '\0';
  scanf("%d",&n);
  for(i=0;i<n;i++) {
    scanf("%s %s",com,str); // より高速なscanfを使用
    if(com[0] == 'i') {
      insert(str);
    } else {
      if(find(str)){
        printf("yes\n");
      } else {
        printf("no\n");
      }
    }
  }
  return 0;
}


#include<stdio.h>
#include<string.h>

#define M 1046527/* your task*/
#define L 14

char H[M][L]; /* Hash Table */

int getChar(char ch){
  if ( ch == 'A') return 1;
  else if ( ch == 'C') return 2;
  else if ( ch == 'G') return 3;
  else if ( ch == 'T') return 4;
  return 0;
}

/* convert a string into an integer value */
long long getKey(char str[]){
  long long sum = 0, p = 1;
  int i;
  for ( i = 0; i < (int)strlen(str); i++ ){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key){ return key % M; }
int h2(int key){ return 1 + (key % (M - 1));}

int find(char str[]){
  int i,hash,key;
  key = getKey(str);
  i = 0;
  while(1){
    hash = (h1(key) + i * h2(key))%M;
    if(strcmp(H[hash],str) == 0)return 1;
    else if(strlen(H[hash]) == 0)return 0;
    i++;
  }
  return 0;
}

int insert(char str[]){
  int i,hash,key;
  key = getKey(str);
  i = 0;
  while(1){
    hash = (h1(key) + i * h2(key))%M;
    if(strcmp(H[hash],str) == 0)return 1;
    else if(strlen(H[hash]) == 0){
      strcpy(H[hash],str);
      return 0;
    }
    i++;
  }
  return 0;
  /* your task */

}

int main(){
  int i, n;
  char str[L], com[9];
  for ( i = 0; i < M; i++ ) H[i][0] = '\0';

  scanf("%d", &n);

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

    if ( com[0] == 'i' ){
      insert(str);
    } else {
      if (find(str)){
        printf("yes\n");
      } else {
        printf("no\n");
      }
    }
  }

  return 0;
}


#include <stdio.h>
#include <math.h>
#define DATANUMBER 30000

int main(void) {
	long long  N,i,j,sum;
	long long A[DATANUMBER+1];

	scanf("%lld",&N);
	for(i=1;i<=N;i++){
		scanf("%lld",&A[i]);
	}
	sum=0;
	for(i=1;i<=N;i++){
		for(j=i+1;j<=N;j++){
			A[i]=A[i]%(long long)(pow(10,9)+7);
			A[j]=A[j]%(long long)(pow(10,9)+7);
			sum+=(A[i]*A[j])%(long long)(pow(10,9)+7);
		}
	}
	sum=sum%(long long)(pow(10,9)+7);
	printf("%lld",sum);
	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 <stdio.h>
int main(){
  int n;
  scanf("%d",&n);
  if((n>200000)||(n<2)) return 1;
  int i,j;
  unsigned long int a[n],modulo=1000000007,ans=0;
  for (i=0;i<n;i++)
    scanf("%lu",&a[i]);
  for (i=0;i<n-1;i++)
    for(j=i+1;j<n;j++){
      ans+=a[i]*a[j];
      ans=ans%modulo;
    }
  printf("%lu\n",modulo);
  return 0;
}


#include<stdio.h>
#include<stdlib.h>

int main()
{
    long long int *N ;
    N= (int*)malloc(1000000007 * sizeof(int)) ;
    int  i, j, sum, n, m;
    sum=0 ;
    scanf("%d",&n) ;
    for(i=0 ; i<n ; i++)
    {
        scanf("%lld",&N[i]) ;
    }
    for(i=0 ; i<n-1 ; i++)
    {
        for(j=i+1 ; j<n ; j++)
        {
                m= N[i]*N[j]  ;
                sum= sum+ m ;
        }
    }
    printf("%lld", sum ) ;
}


#include<stdio.h>

#define mod 1000000007

int main(void){
	int N;
	long long sum=0;
	long long ans=0;
	scanf("%d",&N);

	int A[N];

	for(int i=0;i<N;i++) scanf("%d",&A[i]);

	for(int i=0;i<N;i++){
		sum+=A[i];
		sum%=mod;
	}

    for(int i=0;i<N-1;i++){
		sum-=A[i];
		if(sum<0) sum+=mod;

		ans+=A[i]*sum;
		ans%=mod;
	}

	printf("%lld\n",ans);

	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

void swap (int *x, int *y) {
  int temp;

  temp = *x;
  *x = *y;
  *y = temp;

  return;
}

int gcd(int a,int b){
  if(a<b){
    swap(&a,&b);
  }

  if(b==0){
    return a;
  }

  int r=a%b;
  while(r!=0){
    a = b;
    b = r;
    r = (a%b);
  }

  return b;
}

/* クイックソート */
int dn(const void*a,const void*b){return*(int*)b-*(int*)a;}

long int modpow(long int a,long int n,long int mod){
  long int ans=1;
  while(n>0){
    if(n & 1){
      ans=ans*a%mod;
    }
    a=a*a%mod;
    n/=2;
  }
  return ans;
}

long int modinv(long int a,long int mod){
  return modpow(a,mod-2,mod);
}

int max(int a,int b){
  if(a<b){
    return b;
  }
  else{
    return a;
  }
}

int min(int a,int b){
  if(a<b){
    return a;
  }
  else{
    return b;
  }
}

void chmax(int *a,int b){
  if(*a < b){
    *a = b;
  }

  return;
}

void chmin(int *a,int b){
  if(*a > b){
    *a =b;
  }

  return;
}

/****************************************\
| Thank you for viewing my code:)        |
| Written by RedSpica a.k.a. RanseMirage |
| Twitter:@asakaakasaka                  |
\****************************************/

int main(void){
  /*
  解法1

  all:=A[1]+...+A[n]として
  与式を変形すると

  (答え)=(((all-A[i])*A[i])の総和)/2
  ということがわかるので，それを実装

  「2で割る」というのは「2の逆元を掛ける」ということに言い換えられるから
  mod上での逆元を求める関数(53行目，modinv)を書いて，それを最後に掛ければよい

  */

  long int n;
  scanf("%ld",&n);

  long int A[n];
  long int all=0;

  long int MOD=1e9+7;

  for(int i=0;i<n;i++){
    scanf("%ld",&A[i]);
    all+=A[i];

    //常にMODを取る！
    all%=MOD;
  }

  long int ans=0;
  for(int i=0;i<n;i++){
    /*
    allは常にMODを取っていて，all<A[i]の可能性があるので
    MODを足して正にしないといけない

    (all-A[i])>=-(1e9+7)がいえるので，足す数は1回でいいけど
    別の問題で心配だったら2,3回足しても問題ナシ
    */
    ans+=(all-A[i]+MOD)*A[i];

    //常にMODを取る！
    ans%=MOD;
  }

  //MOD上の2の逆元
  long int v=modinv(2,MOD);

  //常にMODを取る！
  printf("%ld\n",ans*v%MOD);

  return 0;
}


#include <stdio.h>
#define MOD 1000000007

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

    long A[N];
    long long S[N + 1];
    long long ans = 0;
    for(int i = 0; i < N; i++) { scanf("%ld", &A[i]); }

    S[0] = 0;
    for(int i = 0; i < N; i++) { S[i + 1] = S[i] + A[i]; }

    for(int i = 0; i < N - 1; i++) {
        ans += (A[i] * ((S[N] - S[i + 1]) % MOD));
        ans %= MOD;
    }

    printf("%lld\n", ans);

    return 0;
}


#include <stdio.h>

int main(void) {
	int mod=1000000007;
	int n;
	int i;
	int j;
	long long sum=0;
	long long a[200000]={0};

	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%lld",&a[i]);
	}
	for(i=0;i<n-1;i++)
	{
		for(j=i+1;j<n;j++)
		{
			sum+=((a[i]%mod)*(a[j]%mod))%mod;
			sum%=mod;
		}
	}
	printf("%lld",sum);
}


#include <stdio.h>

int main(void){
  long long a = 0;
  long long sum_a = 0;
  long long sum = 0;
  int n = 0;

  scanf("%d",&n);

  scanf("%lld",&sum_a);

  while(--n){
    scanf("%lld",&a);
    sum = (sum + sum_a * a) % 1000000007;
    sum_a += a % 1000000007;
  }

  printf("%lld\n",sum);

  return 0;
}


#include <stdio.h>
#define N 400000
#define mod 1000000007
int main(int argc, char *argv[])
{
	long long a[N]={0},n,i,sum=0;
	scanf("%lld",&n);
	for(i=0;i<n;i++){
		scanf("%lld",&a[i]);
		sum+=a[i];
	}
	long long s=0,dum;
	for(i=0;i<=n-2;i++){
		sum-=a[i];
		dum=sum;
		dum%=mod;
		s+=(dum*a[i])%mod;
	}
	printf("%lld\n",s);
}


#include <stdio.h>

int main(void){
  int i, j ,n;
  long long ans;
  scanf("%d", &n);
  int a[n];
  for(i = 0; i < n; i++){
    scanf("%d", &a[i]);
  }

  long cnt = 0;
  for(i = 0; i < n - 1; i++){
    cnt += a[i];
    ans += a[i+1] * cnt;
  }
  ans = ans % 1000000007;

  printf("%lld\n", ans);

  return 0;
}


#include<stdio.h>

int main()
{
    long long int i,j,n;
    scanf("%lld",&n);
    int A[n];
    for(i=0;i<n;i++)
    {
     scanf("%lld",&A[i]);
    }
    int sum=0;
    for(i=0;i<n;i++)
    {
     for(j=i+1;j<n;j++)
      {
        sum=sum+A[i]*A[j];
      }
    }
    printf("%lld\n",sum);

    return 0;
}


#include<stdio.h>
#include<math.h>

int main(void){
    int N;
    scanf("%d",&N);
    long long A[N];
    long long sum = 0, answer;
    for(int i = 0; i < N; i++){
        scanf("%d",&A[i]);
    }
    for(int i = 0; i < N-1; i++){
        for(int j = i+1; j < N; j++){
            if(A[i]*A[j] > 2*pow(10,9)){
                answer = A[i]*A[j] % (long long)(pow(10,9)+7);
            }
            sum += answer;

            if(sum > (pow(10,9)+7)){
                sum %= (long long)(pow(10,9)+7);
            }
           // printf("%ld, %ld\n",answer,sum);
        }
    }
    printf("%ld\n",sum%(long)(pow(10,9)+7));
    return 0;
}


#include <stdio.h>

int main()
{
  unsigned long n;
  scanf("%lu",&n);
  unsigned long a[n];
  int i;
  i = 0;
  unsigned long sum = 0;
  unsigned long p = 0;
  while (i < n)
  {
    scanf("%lu",&a[i]);
    if (i != 0)
    {
        p += a[i - 1];
    	sum += a[i]*p;
    }
    i++;
  }
  printf("%lu",sum % (1000000000 + 7));
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
  long long int n,a[200001],sum=0;

  scanf("%lld", &n);

  for(int i = 0;i < n;i++)
    scanf("%lld",&a[i]);

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

  printf("%lld", sum%(1000000000+7));

  return 0;
}


/*
 * main.c
 *
 *  Created on: 2020/03/28
 *      Author: family
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX(a,b) (a > b ? a : b)
#define MIN(a,b) (a > b ? b : a)
typedef long long int ll;
typedef unsigned long long int ull;

int sort_inc(const void *a, const void *b) { return (*(int*)a - *(int*)b);}
int sort_dec(const void* a, const void* b) { return (*(int*)b - *(int*)a);}
int sort_dec_ll(const void *a, const void *b) {
    ll da = *(ll*)a, db = *(ll*)b;    int val = 0;
    if(da > db) { val = -1; }
    else if (da == db) { val = 0; }
    else { val = 1; }
    return val;
}
int sort_inc_ll(const void *a, const void *b) {
    ll da = *(ll*)a, db = *(ll*)b;    int val = 0;
    if(da > db) { val = 1; }
    else if (da == db) { val = 0; }
    else { val = -1; }
    return val;
}
int sort_dic(const void *a, const void *b) {
    char *pa = (char *)a;    char *pb = (char *)b;    int i = 0, val = 0, N = 10;
    for (i = 0; i < N; i++) {
    	char da = pa[i], db = pb[i];
    	if (da == db) continue;
  		if (da > db) val = 1; else val = -1;
        break;
    }
    return val;
}

void yesno(int ans) {
	if (ans == 1) {	printf("Yes\n");
	} else {		printf("No\n");	}
	return;
}

void okng(int ans) {
	if (ans == 1) { printf("OK\n");
	} else { 		printf("NG\n");	}
	return;
}

#define FIX_VAL (1000000000 + 7)
ll A[200002] = {0};
ll add[200002] = {0};
int main()
{
	int N = 0;
	scanf("%d", &N);
	int i = 0;
	for (i = 0; i < N; i++) {
		scanf("%lld", &A[i]);
	}

	int j = 0;
	ll prev = 0;
	for (j = N-1; j > 0; j--) {
		add[j] += A[j] + prev;
		prev = add[j];
		//printf("%lld A=%lld\n", add[j], A[j]);
	}

	ll ans = 0;
	for (i = 0; i < N-1; i++) {
		if (A[i] == 0) {
		    //
		} else {
			ans = ((A[i] * add[i+1])+ans) % FIX_VAL;
		}
	}
	printf("%lld\n", ans);
	return 0;
}


#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PI acos(-1)
typedef unsigned long long int ull;
typedef long double ld;

int main(void) {
  int   scan;

  ull    answer;
  ull    n;
  ull    *a;
  ull    mod;
  answer = 0;
  mod = 1000000007;
  scan = scanf("%llu", &n);
  a = (ull *)malloc(sizeof(ull) * n);
  for (ull i = 0; i < n; i++) {
    scan = scanf("%llu", &a[i]);
  }

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

  answer = answer % mod;

  printf("%llu", answer);

  return (0);
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define PI 3.141592653589793
#define MOD 1000000007
//#define DEBUG

//qsort用、昇順
int compare_int(const void *a, const void *b)
{
	return *(int*)a-*(int*)b;
}
//qsort(array, 10, sizeof(int), compare_int)

int n;
long long int sum, nisum;
long long int a[200100];
long long int nijyo[200100];

int main(){
	scanf("%d", &n);
	sum = 0;
	nisum = 0;
	for(int i=0; i<n; i++){
		scanf("%lld", &a[i]);
	}

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

	printf("%lld", sum);

	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define rep(i,l,r) for(int i=l;i<r;i++)
#define MOD 1000000007
typedef long long ll;

int main() {
	ll n,a[200001];
	scanf("%lld",&n);
	ll sum = 0;
	rep(i, 0, n) {
		scanf("%lld", &a[i]);
		sum += a[i];
	}
	ll ans = 0;
	rep(i, 0, n) {
		sum -= a[i];
		sum %= MOD;
		ans += (a[i] * sum)%MOD;
		ans %= MOD;
	}
	printf("%lld", ans);
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <time.h>
int main(){
    long long i,j,n,ans=0,a[200020]={},sum[200020]={};
    scanf("%lld",&n);
    for(i=0; i<n; i++){
        scanf("%lld",&a[i]);
        sum[0]+=a[i];
    }
    for(i=1; i<n; i++){
        sum[i]=sum[i-1]-a[i-1];
    }
    for(i=0; i<n; i++){
        sum[i]=sum[i]%1000000007;
    }
    for(i=0; i<n-1; i++){
        ans+=a[i]*sum[i+1];
        ans=ans%1000000007;
    }
    printf("%lld",ans);
    return 0;
}


// SPDX-License-Identifier: X11
// 2020-08-31
// Coprime (500pt)

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#define MAX 1000005

bool *erato(uint_least32_t bound);

int main() {
  uint_least32_t N;
  scanf("%u\n", &N);

  bool *P = erato(MAX);
  uint_least32_t *Ap = calloc(MAX, sizeof(uint_least32_t));

  bool set = true;
  bool pair = true;

  uint_least8_t s;
  uint_least32_t A, i, j;
  for (i = 0; i < N - 1; i++) {
    scanf("%u", &A);

    if (A == 1) {
      continue;
    }
    else if (P[A]) {
    if (Ap[A])
      pair = false;
    Ap[A]++;

    continue;
    }

    if (!(A & 1)) {
      if (Ap[2])
        pair = false;
      Ap[2]++;

      while (!(A & 1))
        A >>= 1;

      if (A == 1) {
        continue;
      }
      else if (P[A]) {
        if (Ap[A])
          pair = false;
        Ap[A]++;

        continue;
      }
    }

    if (!(A % 3)) {
      if (Ap[3])
        pair = false;
      Ap[3]++;

      while (!(A % 3))
        A /= 3;

      if (A == 1) {
        continue;
      }
      else if (P[A]) {
        if (Ap[A])
          pair = false;
        Ap[A]++;

        continue;
      }
    }

    for (j = 5, s = 2; j * j <= A; j += s, s = 6 - s)
      if (!(A % j)) {
        if (Ap[j])
          pair = false;
        Ap[j]++;

        while (!(A % j))
          A /= j;

        if (A == 1) {
          break;
        }
        else if (P[A]) {
          if (Ap[A])
            pair = false;
          Ap[A]++;

          break;
        }
      }
  }

  scanf("%u", &A);

  if (P[A]) {
    if (Ap[A])
      pair = false;
    Ap[A]++;

    if (Ap[A] == N)
      set = false;
  }

  if (!P[A] && !(A & 1)) {
    if (Ap[2])
      pair = false;
    Ap[2]++;

    if (Ap[2] == N)
      set = false;

    while (!(A & 1))
      A >>= 1;

    if (P[A]) {
      if (Ap[A])
        pair = false;
      Ap[A]++;

      if (Ap[A] == N)
        set = false;
    }
  }

  if (!P[A] && !(A % 3)) {
    if (Ap[3])
      pair = false;
    Ap[3]++;

    if (Ap[3] == N)
      set = false;

    while (!(A % 3))
      A /= 3;

    if (P[A]) {
      if (Ap[A])
        pair = false;
      Ap[A]++;

      if (Ap[A] == N)
        set = false;
    }
  }

  if(!P[A])
    for (i = 5, s = 2; i * i <= A; i += s, s = 6 - s)
      if (!(A % j)) {
        if (Ap[j])
          pair = false;
        Ap[j]++;

        if (Ap[j] == N)
          set = false;

        while (!(A % j))
          A /= j;

        if (A == 1) {
          break;
        }
        else if (P[A]) {
          if (Ap[A])
            pair = false;
          Ap[A]++;

          if (Ap[A] == N)
            set = false;

          break;
        }
      }

  if (pair)
    printf("pairwise coprime\n");
  else if (set)
    printf("setwise coprime\n");
  else
    printf("not coprime\n");

  free(P);
  free(Ap);
}

bool *erato(uint_least32_t bound) {
  if (!bound)
    return NULL;

  bool *array = calloc(bound, sizeof(bool));
  uint_least32_t i,j;
  uint_least8_t s,ss;

  if (bound < 3)
    return array;
  array[2] = true;

  if (bound < 4)
    return array;
  array[3] = true;

  if (bound < 5)
    return array;

  for (i = 5, s = 2; i < bound; i += s, s = 6 - s)
    array[i] = true;

  for (i = 5, s = 2; i < bound; i += s, s = 6 - s)
    if (array[i])
      for (j = i, ss = s; i * j < bound; j += ss, ss = 6 - ss)
        array[i * j] = false;

  return array;
}

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<stdio.h>
int gcd(int a,int b){
	int i;
	int val;
	i=a;
	while(i>1){
		if(a%i==0 && b%i==0){
			val=i;
			return val;
		}
		i--;
	}
	val=1;
	return val;
}
int main(){
	int n;
	scanf("%d",&n);
	int a[n];
	for(int i=0;i<n;i++)
	scanf("%d",&a[i]);
	int pairCount=0;
	for(int i=0;i<n-1;i++){
		for(int j=i+1;j<n;j++){
			if(gcd(a[i],a[j])!=1){
				pairCount++;
			}
		}
	}
	if(pairCount==0){
		printf("pairwise coprime");
		return 0;
	}
	else{
		int temp=gcd(a[0],a[1]);
		int val;
		for(int i=2;i<n;i++){
			val=gcd(temp,a[i]);
		}
		if(val==1){
			printf("setwise coprime");
			return 0;
		}
		else{
			printf("not coprime");
			return 0;
		}
	}

}


#include <stdio.h>

int gcd(int a, int b) {
	while (b > 0) {
		int r = a % b;
		a = b;
		b = r;
	}
	return a;
}

#define MAX 1123456

int N;
int A[MAX];

int era[MAX];
char exists[MAX];

int main(void) {
	int i, j;
	int good = 1;
	if (scanf("%d", &N) != 1) return 1;
	for (i = 0; i < N; i++) {
		if (scanf("%d", &A[i]) != 1) return 1;
	}
	for (i = 2; i < MAX; i++) {
		if (era[i] == 0) {
			era[i] = i;
			for (j = i + i; j < MAX; j += i) {
				if (era[j] == 0) era[j] = i;
			}
		}
	}
	for (i = 0; i < N; i++) {
		if (A[i] > 1) {
			if (exists[A[i]] == 2) {
				puts("not coprime");
				return 0;
			}
			exists[A[i]] = 2;
			j = A[i] / era[A[i]];
			while (j > 1) {
				if (exists[j] > 0) good = 0; else exists[j] = 1;
				j /= era[A[i]];
			}
		}
	}
	if (good) {
		puts("pairwise coprime");
	} else {
		int g = A[0];
		for (i = 1; i < N; i++) g = gcd(g, A[i]);
		printf("%s coprime\n", g == 1 ? "setwise" : "not");
	}
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define rep(i,N) for(int i=0;i<(int)N;i++)
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
static inline char GET(void)
{
  static char buf[1<<15],*s1=buf,*s2=buf;
  return s1==s2&&(s2=(s1=buf)+fread(buf,1,1<<15,stdin),s1==s2)?EOF:*s1++;
}
static inline void PUT(char c)
{
  static char buf[1<<15],*ptr=buf;
  if(ptr==buf+strlen(buf)||c==0){fwrite(buf,1,ptr-buf,stdout),ptr=buf;}*ptr++=c;
}
static inline int IN(void)
{
  int x=0,f=0,c=GET();while(c<48||c>57){f^=c==45,c=GET();}
  while(c>47&&c<58){x=x*10+c-48,c=GET();}return f?-x:x;
}
static inline void OUT(int a)
{
  if(a<0)PUT('-'),a=-a;
  int d[40],i=0;do{d[i++]=a%10;}while(a/=10);
  while(i--){PUT(d[i]+48);}PUT('\n');
}
static inline int Max(int x,int y){return x>y?x:y;}
int main(void)
{
  int H=IN(),W=IN(),M=IN(),row[H],col[W],bombY[M],bombX[M],
      idh=0,idw=0,hmax=0,wmax=0,obj=0;
  memset(row,0,sizeof(row));memset(col,0,sizeof(col));
  rep(i,M)
  {
    bombY[i]=IN()-1,bombX[i]=IN()-1;
    row[bombY[i]]++;col[bombX[i]]++;
    hmax=Max(hmax,row[bombY[i]]);wmax=Max(wmax,col[bombX[i]]);
  }
  rep(i,H){if(hmax==row[i]){idh++;}}rep(j,W){if(wmax==col[j]){idw++;}}
  rep(i,M){obj+=(row[bombY[i]]==hmax&&col[bombX[i]]==wmax);}
  return OUT(hmax+wmax-(1l*obj==1l*idh*idw)),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


#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <stdio.h>

int getint()
{
	int x = 0, c = getchar();
	while (c > 47 && c < 58)
	{
		x = x * 10 + c - 48;
		c = getchar();
	}
	return x;
}

const int MAX = 300001;
int res[300001 * 4];

int main()
{
	int H = getint();
	int W = getint();
	int M = getint();

	int h, w, ans;
	int mxr = 0;
	int mxc = 0;
	int ctrow = 0;
	int ctcol = 0;
	for (int i = 0; i < M; ++i)
	{
		h = getint(), w = getint();
		res[i + 2 * MAX] = h, res[i + 3 * MAX] = w;
		int x = ++res[h], y = ++res[w + MAX];

		if (x == mxr)
			++ctrow;
		else if (x > mxr)
			mxr = x, ctrow = 1;

		if (y == mxc)
			++ctcol;
		else if (y > mxc)
			mxc = y, ctcol = 1;
	}
	ans = mxr + mxc;

	long ct = ctrow;
	ct *= ctcol;
	if (ct > M)
	{
		printf("%d\n", ans);
		return 0;
	}

	for (int i = 0; i < M; ++i)
		if (res[res[i + 2 * MAX]] == mxr && res[res[i + 3 * MAX] + MAX] == mxc)
			--ct;
	if (ct <= 0)
		--ans;
	printf("%d\n", ans);
}


#include <stdio.h>
#include <stdlib.h>
#define max(a, b) (a > b? a : b);

long long **row, **col, *point;

void swap(long long *a, long long *b)
{
    long long tmp = *a;
    *a = *b;
    *b = tmp;
}

void sort(long long **array, long long length)
{
    for (int i = 0; i < length - 1; i++)
    {
        for (int j = 0; j < length - i - 1; j++)
        {
            if (array[j][1] < array[j + 1][1])
            {
                swap(&array[j][0], &array[j + 1][0]);
                swap(&array[j][1], &array[j + 1][1]);
            }
        }
    }
}

int main(void)
{
    long long H, W, M;
    scanf("%lld %lld %lld", &H, &W, &M);
    long long maxNum = 0;

    row = (long long**)calloc(H, sizeof(long long*));
    for (int i = 0; i < H; i++)
        row[i] = (long long*)calloc(2, sizeof(long long));

    col = (long long**)calloc(W, sizeof(long long*));
    for (int i = 0; i < W; i++)
        col[i] = (long long*)calloc(2, sizeof(long long));

    point = (long long*)calloc(H * W, sizeof(long long));
    for (long long i = 0; i < M; i++)
    {
        long long a, b;
        scanf("%lld %lld", &a, &b);

        row[a - 1][0] = a;
        row[a - 1][1]++;
        col[b - 1][0] = b;
        col[b - 1][1]++;

        point[a + b - 2] = 1;
    }

    sort(row, H);
    sort(col, W);

    long long tmpR = 0, tmpC = 0;
    for (int i = 0; i < H; i++)
    {
        if (tmpR > row[i][1])
            break;

        tmpR = row[i][1];

        for (int j = 0; j < W; j++)
        {
            if (tmpC > col[j][1])
                break;

            tmpC = col[j][1];

            long long total = tmpR + tmpC;

            if (point[row[i][0] + col[j][0] - 2] == 1)
            {
                total--;
                maxNum = max(maxNum, total);
            }
            else
            {
                printf("%lld", total);
                return 0;
            }
        }
    }

    printf("%lld", maxNum);

    for (int i = 0; i < H; i++)
        free(row[i]);
    free(row);

    for (int i = 0; i < W; i++)
        free(col[i]);
    free(col);

    free(point);
    return 0;
}


#include<stdio.h>
#define sfd(x) scanf("%lld",&x)
#define pfd(x) printf("%lld",x)
#define new printf("\n")

typedef long long ll;
ll total,n;

int main() {
    int n;
    sfd(n);
    if(n%1000==0) printf("0\n");
    else {
        int y=1000-(n%1000);
        printf("%d\n",y);
    }
}

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<stdio.h>

int main(){
	int N,yen=1000;
	scanf("%d",&N);

	for (yen; yen<N; yen+=1000){}
	printf("%d", yen-N);
}


#include<stdio.h>
int main()
{
	int a;
	scanf("%d",&a);

	printf("%d\n",(1000-a%1000)%1000);
	return 0;
}