#include<stdio.h>

#define MAX 500000

int Tree[MAX];
int n;

void maxHeapify(int i){
  int largest ,exchange, l, r;
  l = getLeft(i);
  r = getRight(i);

  if((n+1 >l) && (Tree[l] > Tree[i])) largest = l;

  else largest = i;

  if((n+1 > r) && (Tree[r] > Tree[largest])) largest = r;

  if (largest != i){

    exchange = Tree[i];
    Tree[i] = Tree[largest];
    Tree[largest] = exchange;
    maxHeapify(largest);

  }
}

void buildMaxHeap(){
  int i;
  for (i=(n/2); i+1>1; i--){
    maxHeapify(i);
     }
}

int getLeft(int i){
  return (2*i);
}

int getRight(int i){
  return (2*i + 1);
}
int getParent(int i){
  return i/2;
}

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

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

    scanf("%d",&Tree[i]);

  }

  buildMaxHeap();

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

    printf(" %d",Tree[i]);

  }

  printf("\n");

  return 0;
}


#include <stdio.h>
#define MAX 2000000

void swap(int *, int *);
void maxHeapify(int);

int H,A[MAX+1];

void swap(int *a, int *b){
  int temp;
  temp = *a;
  *a = *b;
  *b = temp;
  return;
}

void maxHeapify(int i){
  int l, r, largest;
  l = 2 * i;
  r = 2 * i + 1;

  if(l <= H && A[l] > A[i]) largest = l;
  else largest = i;
  if(r <= H && A[r] > A[largest]) largest = r;

  if(largest != i){
    swap(&A[i], &A[largest]);
    maxHeapify(largest);
  }
}

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

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

  for(int i=H/2; i>=1; i--) maxHeapify(i);

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

  return 0;
}

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

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

Maximum Heap

A binary heap which satisfies max-heap property is called max-heap. In a max-heap, for every node $i$ other than the root, $A[i] \leq A[parent(i)]$, that is, the value of a node is at most the value of its parent. The largest element in a max-heap is stored at the root, and the subtree rooted at a node contains values no larger than that contained at the node itself.

Here is an example of a max-heap.

Write a program which reads an array and constructs a max-heap from the array based on the following pseudo code.

$maxHeapify(A, i)$ move the value of $A[i]$ down to leaves to make a sub-tree of node $i$ a max-heap. Here, $H$ is the size of the heap.

1 maxHeapify(A, i)
2 l = left(i)
3 r = right(i)
4 // select the node which has the maximum value
5 if l ≤ H and A[l] > A[i]
6 largest = l
7 else
8 largest = i
9 if r ≤ H and A[r] > A[largest]
10 largest = r
11
12 if largest ≠ i　// value of children is larger than that of i
13 swap A[i] and A[largest]
14 maxHeapify(A, largest) // call recursively

The following procedure buildMaxHeap(A) makes $A$ a max-heap by performing maxHeapify in a bottom-up manner.

1 buildMaxHeap(A)
2 for i = H/2 downto 1
3 maxHeapify(A, i)

Input

In the first line, an integer $H$ is given. In the second line, $H$ integers which represent elements in the binary heap are given in order of node id (from $1$ to $H$).

Output

Print values of nodes in the max-heap in order of their id (from $1$ to $H$). Print a single space character before each value.

Constraint

$1 \leq H \leq 500,000$

$-2,000,000,000 \leq$ value of a node $\leq 2,000,000,000$

Sample Input 1

10
4 1 3 2 16 9 10 14 8 7

Sample Output 1

16 14 10 8 7 9 3 2 4 1

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

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>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define SWAP(a, b) (a += b, b = a - b, a -= b)

int parent(int i)
{
    return (int)(i / 2);
}

int left(int i)
{
    return 2 * i;
}

int right(int i)
{
    return 2 * i + 1;
}

void upHeap(int *H, int i)
{
    while (parent(i) >= 1 && H[i] > H[parent(i)])
    {
        SWAP(H[i], H[parent(i)]);
        i = parent(i);
    }
}

void insert(int *H, int *hsize, int key)
{
    *hsize += 1;
    H[*hsize] = key;
    upHeap(H, *hsize);
}

void maxHeapify(int *H, int *hsize, int i)
{
    int u = i;
    int l = left(u);
    int r = right(u);
    int largest;

    if (l <= *hsize && H[l] > H[u])
    {
        largest = l;
    }
    else
    {
        largest = u;
    }
    if (r <= *hsize && H[r] > H[largest])
    {
        largest = r;
    }

    if (largest != u)
    {
        SWAP(H[u], H[largest]);
        maxHeapify(H, hsize, largest);
    }
}

int extractMax(int *H, int *hsize)
{
    int max = H[1];
    H[1] = H[*hsize];
    *hsize -= 1;

    maxHeapify(H, hsize, 1);

    return max;
}

int main(void)
{
    int H[2000001];
    char command[10];
    int hsize = 0;
    int key;
    int max_value;

    while (1)
    {
        scanf("%s", command);
        if (command[0] == 'e' && command[1] == 'n')
        {
            break;
        }
        else if (command[0] == 'i')
        {
            scanf("%d", &key);
            insert(H, &hsize, key);
        }
        else if (command[0] == 'e')
        {
            max_value = extractMax(H, &hsize);
            printf("%d\n", max_value);
        }
    }

    return 0;
}

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

Priority Queue

A priority queue is a data structure which maintains a set $S$ of elements, each of with an associated value (key), and supports the following operations:

$insert(S, k)$: insert an element $k$ into the set $S$

$extractMax(S)$: remove and return the element of $S$ with the largest key

Write a program which performs the $insert(S, k)$ and $extractMax(S)$ operations to a priority queue $S$.
The priority queue manages a set of integers, which are also keys for the priority.

Input

Multiple operations to the priority queue $S$ are given. Each operation is given by "insert $k$", "extract" or "end" in a line. Here, $k$ represents an integer element to be inserted to the priority queue.

The input ends with "end" operation.

Output

For each "extract" operation, print the element extracted from the priority queue $S$ in a line.

Constraints

The number of operations $\leq 2,000,000$

$0 \leq k \leq 2,000,000,000$

Sample Input 1

insert 8
insert 2
extract
insert 10
extract
insert 11
extract
extract
end

Sample Output 1

8
10
11
2


#include <stdio.h>          // printf(), scanf()

#define MAX_V 15
#define min(a, b) ((a) < (b) ? (a) : (b))

static const int INF = 1000000000;

int d[MAX_V][MAX_V];
int dp[1 << MAX_V][MAX_V];
int V;

int
solve()
{
	int S, u, v;

	for (S = 0; S < (1 << V); ++S)
	{
		for (v = 0; v < V; ++v)
			dp[S][v] = INF;
	}

	dp[(1 << V) - 1][0] = 0;
	for (S = (1 << V) - 2; S >= 0; --S)
	{
		for (v = 0; v < V; ++v)
		{
			for (u = 0; u < V; ++u)
			{
				if (!(S >> u & 1))
				{
					int t = dp[S | (1 << u)][u] + d[v][u];
					dp[S][v] = min(dp[S][v], t);
				}
			}
		}
	}

	return dp[0][0];
}

int
main(int argc, char** argv)
{
	int E;
	int s, t, c;
	int i, j;

	scanf("%d %d", &V, &E);
	for (i = 0; i < V; ++i)
	{
		for (j = 0; j < V; ++j)
		{
			if (i == j)
				d[i][j] = 0;
			else
				d[i][j] = INF;
		}
	}

	for (i = 0; i < E; ++i)
	{
		scanf("%d %d %d", &s, &t, &c);
		d[s][t] = c;
	}

	int res = solve();
	if (res == INF)
		res = -1;

	printf("%d\n", res);
	return 0;
}

Traveling Salesman Problem

For a given weighted directed graph G(V, E), find the distance of the shortest route that meets the following criteria:

It is a closed cycle where it ends at the same point it starts.

It visits each vertex exactly once.

Input

|V| |E|
s0 t0 d0
s1 t1 d1
:
s|E|-1 t|E|-1 d|E|-1

|V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.

si and ti represent source and target vertices of i-th edge (directed) and di represents the distance between si and ti (the i-th edge).

Output

Print the shortest distance in a line. If there is no solution, print -1.

Constraints

2 ≤ |V| ≤ 15

0 ≤ di ≤ 1,000

There are no multiedge

Sample Input 1

4 6
0 1 2
1 2 3
1 3 9
2 0 1
2 3 6
3 2 4

Sample Output 1

16

Sample Input 2

3 3
0 1 1
1 2 1
0 2 1

Sample Output 2

-1


Range Minimum Query (RMQ)

Write a program which manipulates a sequence A = {a0, a1, . . . , an-1} with the following operations:

find(s, t): report the minimum element in as, as+1, . . . ,at.

update(i, x): change ai to x.

Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1.

Input

n q
com0 x0 y0
com1 x1 y1
...
comq−1 xq−1 yq−1

In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, q queries are given where com represents the type of queries. '0' denotes update(xi, yi) and '1' denotes find(xi, yi).

Output

For each find operation, print the minimum element.

Constraints

1 ≤ n ≤ 100000

1 ≤ q ≤ 100000

If comi is 0, then 0 ≤ xi < n, 0 ≤ yi < 231-1.

If comi is 1, then 0 ≤ xi < n, 0 ≤ yi < n.

Sample Input 1

3 5
0 0 1
0 1 2
0 2 3
1 0 2
1 1 2

Sample Output 1

1
2

Sample Input 2

1 3
1 0 0
0 0 5
1 0 0

Sample Output 2

2147483647
5


// AOJ GRL_1_A Shortest Path - Single Source Shortest Path
// 2018.4.26 bal4u

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

#define QMAX 150000
typedef struct { int t, s; } QUE;
QUE que[QMAX]; int qsize;

#define PARENT(i) ((i)>>1)
#define LEFT(i)   ((i)<<1)
#define RIGHT(i)  (((i)<<1)+1)

void min_heapify(int i)
{
	int l, r, min;
	QUE qt;

	l = LEFT(i), r = RIGHT(i);
	if (l < qsize && que[l].t < que[i].t) min = l; else min = i;
	if (r < qsize && que[r].t < que[min].t) min = r;
	if (min != i) {
		qt = que[i], que[i] = que[min], que[min] = qt;
		min_heapify(min);
	}
}

void deq()
{
	que[0] = que[--qsize];
	min_heapify(0);
}

void enq(int s, int t)
{
	int i, min;
	QUE qt;

	i = qsize++;
	que[i].s = s, que[i].t = t;
	while (i > 0 && que[min = PARENT(i)].t > que[i].t) {
		qt = que[i], que[i] = que[min], que[min] = qt;
		i = min;
	}
}

// バッファを経ずstdinから数値を得る
//#define getchar_unlocked()  getchar()
int in()
{
	int n = 0, c = getchar_unlocked();
	do n = 10*n + (c & 0xf), c = getchar_unlocked(); while (c >= '0');
	return n;
}

#define INF  0x70707070
#define MAX_V  100002
int V;  // 頂点数
int hi[MAX_V], *to[MAX_V], *d[MAX_V];
int dist[MAX_V];

void dijkstra(int start)
{
	int i, s, e, t, nt;

	memset(dist, INF, V<<2);
	qsize = 0;
	dist[start] = 0, enq(start, 0);
	while (qsize) {
		s = que[0].s, t = que[0].t, deq();
		if (dist[s] < t) continue;
		for (i = 0; i < hi[s]; i++) {
			e = to[s][i], nt = t + d[s][i];
			if (dist[e] > nt) dist[e] = nt, enq(e, nt);
		}
	}
}

int main()
{
	int E, r, i, j, k, s;
	int *memo;

	V = in(), E = in(), r = in();
	memo = malloc(sizeof(int)*3*E);
	j = 0; for (i = 0; i < E; i++) {
		memo[j++] = s = in(), hi[s]++;
		memo[j++] = in(), memo[j++] = in();
	}
	for (i = 0; i < V; i++) {
		if (hi[i]) to[i] = malloc(hi[i]<<2), d[i] = malloc(hi[i]<<2);
	}
	memset(hi, 0, V<<2);
	j = 0; while (E--) {
		s = memo[j++];
		k = hi[s]++, to[s][k] = memo[j++], d[s][k] = memo[j++];
	}
	dijkstra(r);
	for (i = 0; i < V; i++) {
		if (dist[i] == INF) puts("INF");
		else printf("%d\n", dist[i]);
	}
	return 0;
}

Single Source Shortest Path

For a given weighted graph G(V, E) and a source r, find the source shortest path to each vertex from the source (SSSP: Single Source Shortest Path).

Input

An edge-weighted graph G (V, E) and the source r.

|V| |E| r
s0 t0 d0
s1 t1 d1
:
s|E|-1 t|E|-1 d|E|-1

|V| is the number of vertices and |E| is the number of edges in G. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. r is the source of the graph.

si and ti represent source and target vertices of i-th edge (directed) and di represents the cost of the i-th edge.

Output

Print the costs of SSSP in the following format.

c0
c1
:
c|V|-1

The output consists of |V| lines. Print the cost of the shortest path from the source r to each vertex 0, 1, ... |V|-1 in order. If there is no path from the source to a vertex, print INF.

Constraints

1 ≤ |V| ≤ 100000

0 ≤ di ≤ 10000

0 ≤ |E| ≤ 500000

There are no parallel edges

There are no self-loops

Sample Input 1

4 5 0
0 1 1
0 2 4
1 2 2
2 3 1
1 3 5

Sample Output 1

0
1
3
4

Sample Input 2

4 6 1
0 1 1
0 2 4
2 0 1
1 2 2
3 1 1
3 2 5

Sample Output 2

3
0
2
INF


LCA: Lowest Common Ancestor

For a rooted tree, find the lowest common ancestor of two nodes u and v.

The given tree consists of n nodes and every node has a unique ID from 0 to n-1 where 0 is the root.

Input

n
k0 c1 c2 ... ck0
k1 c1 c2 ... ck1
:
kn-1 c1 c2 ... ckn-1
q
u1 v1
u2 v2
:
uq vq

The first line of the input includes an integer n, the number of nodes of the tree.

In the next n lines, the information of node i is given. ki is the number of children of node i, and c1, ... cki are node IDs of 1st, ... kth child of node i.

In the next line, the number of queryies q is given. In the next q lines, pairs of u and v are given as the queries.

Output

For each query, print the LCA of u and v in a line.

Constraints

1 ≤ n ≤ 100000

1 ≤ q ≤ 100000

Sample Input 1

8
3 1 2 3
2 4 5
0
0
0
2 6 7
0
0
4
4 6
4 7
4 3
5 2

Sample Output 1

1
1
0
0


#include <stdio.h>

int main( void )
{
	int i, x;

	scanf( "%d", &x );

	for( i = 1; x != 0; i++ ){
		printf( "Case %d: %d\n", i, x );
	}

	return 0;
}

Print Test Cases

In the online judge system, a judge file may include multiple datasets to check whether the submitted program outputs a correct answer for each test case. This task is to practice solving a problem with multiple datasets.

Write a program which reads an integer x and print it as is. Note that multiple datasets are given for this problem.

Input

The input consists of multiple datasets. Each dataset consists of an integer x in a line.

The input ends with an integer 0. You program should not process (print) for this terminal symbol.

Output

For each dataset, print x in the following format:

Case i: x

where i is the case number which starts with 1. Put a single space between "Case" and i. Also, put a single space between ':' and x.

Constraints

1 ≤ x ≤ 10000

The number of datasets ≤ 10000

Sample Input

3
5
11
7
8
19
0

Sample Output

Case 1: 3
Case 2: 5
Case 3: 11
Case 4: 7
Case 5: 8
Case 6: 19


#include <stdio.h>

int main()
{
    int a, b, c;
    int i, cnt;
    scanf("%d %d %d", &a, &b, &c);

    cnt = 0;
    for (i=a; i=b; i++) {
        if(i/c == 0) cnt++;
    }
    return 0;
}

How Many Divisors?

Write a program which reads three integers a, b and c, and prints the number of divisors of c between a and b.

Input

Three integers a, b and c are given in a line separated by a single space.

Output

Print the number of divisors in a line.

Constraints

1 ≤ a, b, c ≤ 10000

a ≤ b

Sample Input 1

5 14 80

Sample Output 1

3


#include<stdio.h>
int main(){
	long long a,b[10001],i,j,x,ans,ans2,ans3=0;
	scanf("%lld",&a);
	for(i=0;i<a;i++){
		scanf("%lld",&b[i]);
		ans3=ans3+b[i];
	}
	for(i=0;i<a;i++){
		for(j=i;j<a;j++){
			if(b[i]>b[j]){
				x=b[i];
				b[i]=b[j];
				b[j]=x;
			}
		}
	}
	ans=b[0];
	ans2=b[a-1];
	printf("%lld %lld %lld\n",ans,ans2,ans3);
	return 0;
}

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

Min, Max and Sum

Write a program which reads a sequence of $n$ integers $a_i (i = 1, 2, ... n)$, and prints the minimum value, maximum value and sum of the sequence.

Input

In the first line, an integer $n$ is given. In the next line, $n$ integers $a_i$ are given in a line.

Output

Print the minimum value, maximum value and sum in a line. Put a single space between the values.

Constraints

$0 < n \leq 10000$

$-1000000 \leq a_i \leq 1000000$

Sample Input

5
10 1 5 4 17

Sample Output

1 17 37


How many ways?

Write a program which identifies the number of combinations of three integers which satisfy the following conditions:

You should select three distinct integers from 1 to n.

A total sum of the three integers is x.

For example, there are two combinations for n = 5 and x = 9.

1 + 3 + 5 = 9

2 + 3 + 4 = 9

Input

The input consists of multiple datasets. For each dataset, two integers n and x are given in a line.

The input ends with two zeros for n and x respectively. Your program should not process for these terminal symbols.

Constraints

3 ≤ n ≤ 100

0 ≤ x ≤ 300

Output

For each dataset, print the number of combinations in a line.

Sample Input

5 9
0 0

Sample Output

2

Note

      解説


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

int main () {
   char s[100], t[100];
   int i, j, sn, tn;

   scanf("%s", s);
   scanf("%s", t);

   sn = strlen(s);
   tn = strlen(t);

   for (i = 0 ; ;i++) {
     for (j = 0; j < tn; j++) {
       if (s[i] != t[j]) {
         break;
       }
     }
   }

   if(tn == j-1) {
     printf("Yes\n");
   }  else {
       printf("No\n");
      }

   return 0;
}

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

Ring

Write a program which finds a pattern $p$ in a ring shaped text $s$.

Input

In the first line, the text $s$ is given.

In the second line, the pattern $p$ is given.

Output

If $p$ is in $s$, print Yes in a line, otherwise No.

Constraints

$1 \leq $ length of $p \leq $ length of $s \leq 100$

$s$ and $p$ consists of lower-case letters

Sample Input 1

vanceknowledgetoad
advance

Sample Output 1

Yes

Sample Input 2

vanceknowledgetoad
advanced

Sample Output 2

No


#include <stdio.h>

#define PRIME_MAX 1000000
int getprimes(int list[], int top){
    int i, j;
    int index = 0;
    char tmp[PRIME_MAX];

    for(i=0;i<top;i++) {
        tmp[i] = 1;
    }

    tmp[0] = tmp[1] = 0;
    for(i=2;i<top;i++) {
        if(tmp[i] == 1) {
            /* 素数 */
            list[index] = i;
            index ++;
            for(j=2;j*i<top;j++) {
                tmp[j*i] = 0;
            }
        }
    }

    list[index] = -1;
    return index;
}

int main(void) {
    int i, c;
    int n, base;
    int size;
    int primes[100000];
    int div[1000];
    int d;
    size = getprimes(primes, PRIME_MAX);

    while(scanf("%d\n", &base) == 1) {
        i = d = 0;
        n = base;
        while(n > 1) {
        	if((n % primes[i]) == 0) {
        		div[d] = primes[i];
        		n /= primes[i];
        		d++;
        	} else {
        		i++;
        	}

        	if(i >= size) {
        		div[d] = n;
        		d++;
        		break;
        	}
        }

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

    return 0;
}

Prime Factorization

Factorize a given integer n.

Input

n

An integer n is given in a line.

Output

Print the given integer n and :. Then, print prime factors in ascending order. If n is divisible by a prime factor several times, the prime factor should be printed according to the number of times. Print a space before each prime factor.

Constraints

2 ≤ n ≤ 109

Sample Input 1

12

Sample Output 1

12: 2 2 3

Sample Input 2

126

Sample Output 2

126: 2 3 3 7


#include <stdio.h>

#include <string.h>

int main() {

    char acl[20];

    int k;

    scanf("%d", &k);

    for(int i = 0; i<k; i++){

        strcat(acl, "ACL");

    }

    printf("%s", acl);

    return 0;

}

Score : 100 points

Problem Statement

You are given an integer K.
Print the string obtained by repeating the string ACL K times and concatenating them.

For example, if K = 3, print ACLACLACL.

Constraints

1 \leq K \leq 5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K

Output

Print the string obtained by repeating the string ACL K times and concatenating them.

Sample Input 1

3

Sample Output 1

ACLACLACL


#include<stdio.h>
#include<string.h>
int main(void){
    char name[100];
    scanf("%s", name);
    int length = strlen(name) - 1;
    char l = name[length];
    if(l != 's'){
        name[length + 1] = 's';
        name[length + 2] = '\0';
    }
    else if(l == 's'){
        name[length] = 'e';
        name[length + 1] = 's';
        name[length + 2] = '\0';
    }
    printf("%s", name);
    return 0;
}

Score : 100 points

Problem Statement

In the Kingdom of AtCoder, people use a language called Taknese, which uses lowercase English letters.

In Taknese, the plural form of a noun is spelled based on the following rules:

If a noun's singular form does not end with s, append s to the end of the singular form.

If a noun's singular form ends with s, append es to the end of the singular form.

You are given the singular form S of a Taknese noun. Output its plural form.

Constraints

S is a string of length 1 between 1000, inclusive.

S contains only lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the plural form of the given Taknese word.

Sample Input 1

apple

Sample Output 1

apples

apple ends with e, so its plural form is apples.

Sample Input 2

bus

Sample Output 2

buses

bus ends with s, so its plural form is buses.

Sample Input 3

box

Sample Output 3

boxs


#include <stdio.h>

int main() {
	int N,i,D1,count=0,D2,MAX=0;
	scanf("%d", &N);
	for (i = 0; i < N; i++) {
		scanf("%d %d", &D1, &D2);
		if (D1 == D2) {
			count = count + 1;
			if (MAX < count) {
				MAX = count;
			}
		}
		else {
			count = 0;
		}
	}
	if (MAX >= 3) {
		printf("Yes");
	}
	else {
		printf("No");
	}
	return 0;
}

Score : 200 points

Problem Statement

Tak performed the following action N times: rolling two dice.
The result of the i-th roll is D_{i,1} and D_{i,2}.

Check if doublets occurred at least three times in a row.
Specifically, check if there exists at lease one i such that D_{i,1}=D_{i,2}, D_{i+1,1}=D_{i+1,2} and D_{i+2,1}=D_{i+2,2} hold.

Constraints

3 \leq N \leq 100

1\leq D_{i,j} \leq 6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
D_{1,1} D_{1,2}
\vdots
D_{N,1} D_{N,2}

Output

Print Yes if doublets occurred at least three times in a row. Print No otherwise.

Sample Input 1

5
1 2
6 6
4 4
3 3
3 2

Sample Output 1

Yes

From the second roll to the fourth roll, three doublets occurred in a row.

Sample Input 2

5
1 1
2 2
3 4
5 5
6 6

Sample Output 2

No

Sample Input 3

6
1 1
2 2
3 3
4 4
5 5
6 6

Sample Output 3

Yes


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

int main(int argc, const char * argv[]) {
    // insert code here...
    int n = 0, i, j, cnt=0, save=0, connect=0;//connect=0初回  ３連続でない 7連続である
    int s[6][2];

    scanf("%d",&n);

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

//    printf("%d", n);
//    printf("\n");
//    for (i = 0; i < n; i++) {
//        for (j = 0; j < 2; j++) {
//            printf("%d ", s[i][j]);
//        }
//        printf("\n");
//    }
    for(i = 0; i < n; i++){
        if(s[i][0]==s[i][1]){
            if(connect==0||connect==7){
                cnt++;
            }else{
                cnt=1;
            }
            connect=7;
        }else{

            if(save<cnt){
                save = cnt;
            }

            connect=3;
        }
    }
    if(save>cnt){
        cnt=save;
    }
    if(cnt>=3){
        printf("Yes");
    }else{
        printf("No");
    }
    return 0;
}


#include <stdio.h>
int main(void){
    int n;
    int d[200];
    int count=0,h=0;

    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d %d",&d[i*2],&d[i*2+1]);
        if(d[i*2]==d[i*2+1]){
            count++;
            h=count;
        }
        else{
            count=0;
        }
    }

    if(h>=3)
        printf("Yes");
    else
        printf("No");

    return 0;
}


#include <stdio.h>

int main(void) {
	int N, DB, DA;
	int judge = 0;
	//Db Dbefor Da Dafter
	scanf("%d", &N);

	for (int i = 0; i < N; i++) {
		scanf("%d %d", &DA, &DB);
		if (judge!=3) {
			if (DA == DB)
				judge++;
			else
				judge = 0;
		}
	}
	if (judge == 3)
		printf("Yes");
	else
		printf("No");
	return 0;
}


#include <stdio.h>

int main()
{

    long long int n;
    long long int x;
    int m;
    int remember[100000];
    unsigned long long int remembersum[100000];
    long long int remembercount[100000];
    int r=0;
    int flag =0;//繰り返し

    scanf("%lld", &n);
    scanf("%lld", &x);
    scanf("%d", &m);

    unsigned long long int sum = 0;
    int i;
    int j;
    sum = x;
    for (i = 1; i < n;i++){
        if(x*x-m>0){
            x = (x * x )% m;
            if(x<100000){
                for (j = 0; j < r;j++){
                    if(remember[j]==x){
                        flag = 1;
                        //printf("*find*%lld\n", x);
                        sum += x;
                        break;

                        //同じの発見
                    }
                }
                //同じの発見したら抜ける
                if(flag==1)
                    break;
            }
        }
        else if (x * x - m == 0)
        {
            break;
        }
        else
            x = x * x;

        sum += x;
        if(x<100000){
            remember[r] = x;
            remembersum[r] = sum;
            remembercount[r] = i;
            r++;
            if(r>=100000)
                r = 0;
            //printf("*remember*%lld\n", x);
        }

    }
    if(flag==1){
        unsigned long long partsum = sum - remembersum[j];
        long long int partcount = i - remembercount[j];
        sum += (((long long int)(n - 1 - i) / partcount)) * partsum;
        n = (n -1- i) % partcount;
        x = remember[j];
        for (i = 0; i < n; i++)
        {
            if (x * x - m >= 0)
            {
                x = (x * x) % m;

            }
            else
                x = x * x;

            sum += x;

        }
    }

    printf("%lld", sum);

    printf("\n");
}

Score : 500 points

Problem Statement

Let us denote by f(x, m) the remainder of the Euclidean division of x by m.

Let A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M).
Find \displaystyle{\sum_{i=1}^N A_i}.

Constraints

1 \leq N \leq 10^{10}

0 \leq X < M \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X M

Output

Print \displaystyle{\sum_{i=1}^N A_i}.

Sample Input 1

6 2 1001

Sample Output 1

1369

The sequence A begins 2,4,16,256,471,620,\ldots Therefore, the answer is 2+4+16+256+471+620=1369.

Sample Input 2

1000 2 16

Sample Output 2

6

The sequence A begins 2,4,0,0,\ldots Therefore, the answer is 6.

Sample Input 3

10000000000 10 99959

Sample Output 3

492443256176507


#include <stdio.h>

int main() {
  long int a, b, c, d;
  long int ans1, ans2, ans;

  scanf("%ld%ld%ld%ld", &a, &b, &c, &d);

  long int x1 = a * c;
  long int x2 = a * d;
  long int x3 = b * c;
  long int x4 = b * d;

  if (x1 < x2) {
    ans1 = x2;
  } else {
    ans1 = x1;
  }

  if (x3 < x4) {
    ans2 = x4;
  } else {
    ans2 = x3;
  }

  if (ans1 < ans2) {
    ans = ans2;
  }  else {
    ans = ans1;
  }

  printf("%ld", ans);

  return 0;
}

Score : 200 points

Problem Statement

Given are integers a,b,c and d.
If x and y are integers and a \leq x \leq b and c\leq y \leq d hold, what is the maximum possible value of x \times y?

Constraints

-10^9 \leq a \leq b \leq 10^9

-10^9 \leq c \leq d \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

a b c d

Output

Print the answer.

Sample Input 1

1 2 1 1

Sample Output 1

2

If x = 1 and y = 1 then x \times y = 1.
If x = 2 and y = 1 then x \times y = 2.
Therefore, the answer is 2.

Sample Input 2

3 5 -4 -2

Sample Output 2

-6

The answer can be negative.

Sample Input 3

-1000000000 0 -1000000000 0

Sample Output 3

1000000000000000000


#include<stdio.h>
int main(){
  int D, T, S;
  scanf("%d %d %d\n", &D, &T, &S);
  if(D > T * S)
    printf("No\n");
  else
    printf("Yes\n");
}

Score : 100 points

Problem Statement

Takahashi is meeting up with Aoki.

They have planned to meet at a place that is D meters away from Takahashi's house in T minutes from now.

Takahashi will leave his house now and go straight to the place at a speed of S meters per minute.

Will he arrive in time?

Constraints

1 \leq D \leq 10000

1 \leq T \leq 10000

1 \leq S \leq 10000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

D T S

Output

If Takahashi will reach the place in time, print Yes; otherwise, print No.

Sample Input 1

1000 15 80

Sample Output 1

Yes

It takes 12.5 minutes to go 1000 meters to the place at a speed of 80 meters per minute. They have planned to meet in 15 minutes so he will arrive in time.

Sample Input 2

2000 20 100

Sample Output 2

Yes

It takes 20 minutes to go 2000 meters to the place at a speed of 100 meters per minute. They have planned to meet in 20 minutes so he will arrive just on time.

Sample Input 3

10000 1 1

Sample Output 3

No

He will be late.


#include<stdio.h>

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

    if(d<=t*s){
        printf("Yes\n");
    }else{
        printf("No\n");
    }
    return 0;
}


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

int main() {
  char S[1001] = {'\0'};
  char T[1001] = {'\0'};

  int count = 0,min = 1000;
  int len_S = 0;
  int len_T = 0;

  gets(S);
  gets(T);
  len_S = strlen(S);
  len_T = strlen(T);

  for(int i=0;i + len_T < len_S;i++){
    count = 0;
    for(int j=0;j<len_T;j++){
      // printf("%c:%c\n",S[i+j],T[j]);
      if(S[i+j] != T[j]){
        count++;
      }
    }
    if(min > count) min = count;
  }
  printf("%d\n",min);

  return 0;
}

Score : 200 points

Problem Statement

Given are two strings S and T.

Let us change some of the characters in S so that T will be a substring of S.

At least how many characters do we need to change?

Here, a substring is a consecutive subsequence. For example, xxx is a substring of yxxxy, but not a substring of xxyxx.

Constraints

The lengths of S and T are each at least 1 and at most 1000.

The length of T is at most that of S.

S and T consist of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the minimum number of characters in S that need to be changed.

Sample Input 1

cabacc
abc

Sample Output 1

1

For example, changing the fourth character a in S to c will match the second through fourth characters in S to T.

Since S itself does not have T as its substring, this number of changes - one - is the minimum needed.

Sample Input 2

codeforces
atcoder

Sample Output 2

6


#include <stdio.h>
#include<string.h>
#include<math.h>
int mul(int n, int s)
{
if (n==0)
{
return s;
}
else
{
s=s+(n%10);
n=n/10;
return mul(n,s);
}
}
int main()
{
    int a,b,c,d,e,f,g,h,i,j,s=0;
scanf("%d",&a);
b=mul(a,s);
if(b%9==0&&a%9==0)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
return 0;
}

Score : 200 points

Problem Statement

An integer N is a multiple of 9 if and only if the sum of the digits in the decimal representation of N is a multiple of 9.

Determine whether N is a multiple of 9.

Constraints

0 \leq N < 10^{200000}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N is a multiple of 9, print Yes; otherwise, print No.

Sample Input 1

123456789

Sample Output 1

Yes

The sum of these digits is 1+2+3+4+5+6+7+8+9=45, which is a multiple of 9, so 123456789 is a multiple of 9.

Sample Input 2

0

Sample Output 2

Yes

Sample Input 3

31415926535897932384626433832795028841971693993751058209749445923078164062862089986280

Sample Output 3

No


#include <stdio.h>
typedef long long ll;
#define rep(i,n) for(int i=0; i<(int)(n); i++)

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

while (N>0)
{
   sum+=N%10;
   N=N/10;
}
if (0==sum%9)
{
 printf("Yes");
}
else
{
   printf("No");

}

   return 0;
}


#include <stdio.h>
#include <string.h>
int main(void){
    char s;
    int ans=0;
    while(scanf("%c", &s)==1){
        ans+=(int)s-'0';
        if(ans>=9)ans-=9;
    }

    if(ans%9==0)printf("Yes");
    else printf("No");
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<stdbool.h>
#define e 1000000007

typedef  long long ll;

int int_cmp(const void *a, const void *b){
    const int *ia = (const int *)a;
    const int *ib = (const int *)b;
    return *ia  - *ib;
}

ll int_cmp1(const void *a, const void *b){
    const int *ia = (const int *)a;
    const int *ib = (const int *)b;
    return *ia  - *ib;
}

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

int min(int a,int b){
	if(a>b)
		a=b;
	return a;
}

int absolu(int n){
	if(n<0)
		n*=-1;

	return n;
}

void solve(){
   char s[10000000000000000000000000];
	int i;

	scanf("%s",s);
	ll ans=0,len=strlen(s);

	for(i=0 ; i<len ; i++){
		int val=s[i]-'0';
		ans+=val;
	}

	 if(ans%9==0)
		printf("YES\n");
	else
		printf("NO\n");
}

int main(){
 /* int t;
	scanf("%d",&t);

	while(t--)*/
		solve();

	return 0;
}


#include<stdio.h>
int main()
{
  char n[200001];
  scanf("%s",n);
  int sum=0;
  for (int i=0;n[i]!='\0';i++) sum+=n[i]-'0';
  if (sum%9==0) printf("Yes");
  else printf("No");
  return 0;
}


#include <stdio.h>
int main()
{
    long long int N;
	long long int a[1000000000], T, i;
	scanf("%lld", &N);
	T=0;
	for(i=0;i<N;i++)
	{
		scanf("%lld", &a[i]);
	}
	for(i=1;i<N;i++)
	{
	if(a[i]<a[i-1])
	{
		T+=a[i-1]-a[i];
	}
	}
	printf("%lld", T);
	return 0;
}

Score : 300 points

Problem Statement

N persons are standing in a row. The height of the i-th person from the front is A_i.

We want to have each person stand on a stool of some heights - at least zero - so that the following condition is satisfied for every person:

Condition: Nobody in front of the person is taller than the person. Here, the height of a person includes the stool.

Find the minimum total height of the stools needed to meet this goal.

Constraints

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

1 \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 the minimum total height of the stools needed to meet the goal.

Sample Input 1

5
2 1 5 4 3

Sample Output 1

4

If the persons stand on stools of heights 0, 1, 0, 1, and 2, respectively, their heights will be 2, 2, 5, 5, and 5, satisfying the condition.

We cannot meet the goal with a smaller total height of the stools.

Sample Input 2

5
3 3 3 3 3

Sample Output 2

0

Giving a stool of height 0 to everyone will work.


Score : 400 points

Problem Statement

A maze is composed of a grid of H \times W squares - H vertical, W horizontal.

The square at the i-th row from the top and the j-th column from the left - (i,j) - is a wall if S_{ij} is # and a road if S_{ij} is ..

There is a magician in (C_h,C_w). He can do the following two kinds of moves:

Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in.

Move B: Use magic to warp himself to a road square in the 5\times 5 area centered at the square he is currently in.

In either case, he cannot go out of the maze.

At least how many times does he need to use the magic to reach (D_h, D_w)?

Constraints

1 \leq H,W \leq 10^3

1 \leq C_h,D_h \leq H

1 \leq C_w,D_w \leq W

S_{ij} is # or ..

S_{C_h C_w} and S_{D_h D_w} are ..

(C_h,C_w) \neq (D_h,D_w)

Input

Input is given from Standard Input in the following format:

H W
C_h C_w
D_h D_w
S_{11}\ldots S_{1W}
\vdots
S_{H1}\ldots S_{HW}

Output

Print the minimum number of times the magician needs to use the magic. If he cannot reach (D_h,D_w), print -1 instead.

Sample Input 1

4 4
1 1
4 4
..#.
..#.
.#..
.#..

Sample Output 1

1

For example, by walking to (2,2) and then using the magic to travel to (4,4), just one use of magic is enough.

Note that he cannot walk diagonally.

Sample Input 2

4 4
1 4
4 1
.##.
####
####
.##.

Sample Output 2

-1

He cannot move from there.

Sample Input 3

4 4
2 2
3 3
....
....
....
....

Sample Output 3

0

No use of magic is needed.

Sample Input 4

4 5
1 2
2 5
#.###
####.
#..##
#..##

Sample Output 4

2


Score : 100 points

Problem Statement

We have weather records at AtCoder Town for some consecutive three days. A string of length 3, S, represents the records - if the i-th character is S, it means it was sunny on the i-th day; if that character is R, it means it was rainy on that day.

Find the maximum number of consecutive rainy days in this period.

Constraints

|S| = 3

Each character of S is S or R.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum number of consecutive rainy days in the period.

Sample Input 1

RRS

Sample Output 1

2

We had rain on the 1-st and 2-nd days in the period. Here, the maximum number of consecutive rainy days is 2, so we should print 2.

Sample Input 2

SSS

Sample Output 2

0

It was sunny throughout the period. We had no rainy days, so we should print 0.

Sample Input 3

RSR

Sample Output 3

1

We had rain on the 1-st and 3-rd days - two "streaks" of one rainy day, so we should print 1.


#include <stdio.h>

int main(void){
  char S[32];
  int i, ans=0,tmp=0;
  scanf("%s",S);

  for(i = 0;i<3;i++){
    if(S[i-1] == 'R'){
      if(S[i] == 'R'){
        tmp++;
      }
    }else{
      if(S[i] == 'R'){
        tmp=1;
      }
    }
    if(ans<tmp){
      ans=tmp;
    }
  }
  printf("%d",ans);
  return 0;
}


#include<stdio.h>
int main(void){
  char S[3];
  int i,r=0;
  printf("%c\n",S);
  for(i=0;i<4;i++){
    if(S[i]=='R')r++;
  }
  printf("%d",r);
  return 0;
}


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

int main(void)
{
    int X,K,D;
    int n;
  int t;
  int min = X + K*D;

    scanf("%d,%d,%d",&X,&K,&D);
    for (n = 0; n < K; n++)
    {
        t = 0;
      t = X;
      t += 2*n*D -K*D;
        t = abs(t);
        if (t < min)
        {
            min = t;
        }
        if (min == 0)
        {
            break;
        }

    }

    printf("%d",min);

    return 0;

}

Score : 300 points

Problem Statement

Takahashi, who lives on the number line, is now at coordinate X. He will make exactly K moves of distance D in the positive or negative direction.

More specifically, in one move, he can go from coordinate x to x + D or x - D.

He wants to make K moves so that the absolute value of the coordinate of the destination will be the smallest possible.

Find the minimum possible absolute value of the coordinate of the destination.

Constraints

-10^{15} \leq X \leq 10^{15}

1 \leq K \leq 10^{15}

1 \leq D \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X K D

Output

Print the minimum possible absolute value of the coordinate of the destination.

Sample Input 1

6 2 4

Sample Output 1

2

Takahashi is now at coordinate 6. It is optimal to make the following moves:

Move from coordinate 6 to (6 - 4 =) 2.

Move from coordinate 2 to (2 - 4 =) -2.

Here, the absolute value of the coordinate of the destination is 2, and we cannot make it smaller.

Sample Input 2

7 4 3

Sample Output 2

1

Takahashi is now at coordinate 7. It is optimal to make, for example, the following moves:

Move from coordinate 7 to 4.

Move from coordinate 4 to 7.

Move from coordinate 7 to 4.

Move from coordinate 4 to 1.

Here, the absolute value of the coordinate of the destination is 1, and we cannot make it smaller.

Sample Input 3

10 1 2

Sample Output 3

8

Sample Input 4

1000000000000000 1000000000000000 1000000000000000

Sample Output 4

1000000000000000

The answer can be enormous.


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

#define LLD_ABS(x) (((x) >= 0 ) ? (x) : (-x))

int main()
{
    long long int x, k, d, answer, abs_x, req_d_num;

    scanf("%lld %lld %lld", &x, &k, &d);

    abs_x = LLD_ABS(x);

    req_d_num = abs_x/d;

    if (req_d_num >= k)
    {
        answer = abs_x - d*k;
    } else {
        if (((k - req_d_num)%2) == 1)
        {
            answer = (d - (abs_x%d));
        } else {
            answer = abs_x%d;
        }
    }

    printf("%lld", answer);

    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
//if(strcmp(s[i],"AC")==0)
//if(s[l]=='#')
/*小文字判定
if((moji>= 'a') && (moji<='z')){
    printf("a\n");
    return 0;
  }
  */

int W_Length(char *str){
	int length = 0;
	while(*str++ != '\0')
		length++;
	return length;
}

void swap(int *a, int *b){
  int temp;
  temp=*a;
  *a=*b;
  *b=temp;
}
//swap(&a, &b)

double deg(double theta){//deg to theta
return theta*(2*3.14159265359)/360;
}

int cmp( const void *p, const void *q ) {
    return *(int*)p - *(int*)q;
}//ちいさい順
//qsort(a,n,sizeof(int),cmp);
//char s[n][11]

int cmp_s(const void * left, const void * right)
{
	char *left_char = (char *)left;
  char *right_char = (char *)right;
  return strcmp(left_char, right_char);
}
//qsort(s,n,sizeof(s[0]),cmp_s);

int main (void)
{
	long long int a,k,d,b,c,n,i,x,temp;
  char s[200005];

scanf("%lld %lld %lld",&x,&k,&d);
  //printf("x=%lld, k=%lld, d=%lld",x,k,d);
  if(d<=-1){
  d=-d;
  }

    temp=x/d;
  if(x>d){
    if(temp>=k){
    printf("%lld\n",x-k*d);
      return 0;
    }
  }

    k=k-temp;

  if(k%2==0){
  printf("%lld\n",x-d*temp);
    return 0;
  }
  else{
  printf("%lld\n",d*(temp+1)-x);
    //printf("k=%lld, temp=%lld, x=%lld",k,temp,x);
  }

return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define repp(i,l,r,k)for(ll i=(l);i<(r);i+=(k))
#define INF ((1LL<<62)-(1LL<<31))
#define max(p,q)((p)>(q)?(p):(q))
#define min(p,q)((p)<(q)?(p):(q))
#define bit(n,m)(((n)>>(m))&1)
int upll(const void*a, const void*b){return*(ll*)a<*(ll*)b?-1:*(ll*)a>*(ll*)b?1:0;}
int downll(const void*a, const void*b){return*(ll*)a<*(ll*)b?1:*(ll*)a>*(ll*)b?-1:0;}
void sortup(ll*a,int n){qsort(a,n,sizeof(ll),upll);}
void sortdown(ll*a,int n){qsort(a,n,sizeof(ll),downll);}
ll pom(ll a,ll n,int m){ll x=1;for(a%=m;n;n/=2)n&1?x=x*a%m:0,a=a*a%m;return x;}
//#define MOD 998244353
#define MOD 1000000007
#define invp(a,p)pom(a,p-2,p)

ll a[100][100];

int main(){
	int n;
	scanf("%d",&n);
	rep(_,0,n){
		char s[20]={};
		scanf("%s",s);
		int p=-1;
		for(int i=0;i<20;i++)if(s[i]=='.')p=i;
		ll x;
		sscanf(s,"%lld",&x);
		rep(i,0,9)x*=10;
		if(p==-1){
		}else{
			ll d;
			sscanf(s+p+1,"%lld",&d);
			int z=9-strlen(s+p+1);
			rep(i,0,z)d*=10;
			x+=d;
		}
		//printf("%lld\n",x);
		int p2=0,p5=0;
		while(x%2==0)x/=2,p2++;
		while(x%5==0)x/=5,p5++;
		a[p2][p5]++;
		//printf("%lld %lld\n",p2,p5);
	}

	ll ans=0;
	rep(i,0,50)rep(j,0,50)rep(ii,0,50)rep(jj,0,50){
		if(i+ii>=18&&j+jj>=18){
			ans+=a[i][j]*a[ii][jj];
			if(i==ii&&j==jj)ans-=a[i][j];
		}
	}
	printf("%lld\n",ans/2);

}

Score : 300 points

Problem Statement

You are given N real values A_1, A_2, \ldots, A_N.
Compute the number of pairs of indices (i, j)
such that i < j and the product A_i \cdot A_j is integer.

Constraints

2 \leq N \leq 200\,000

0 < A_i < 10^4

A_i is given with at most 9 digits after the decimal.

Input

Input is given from Standard Input in the following format.

N
A_1
A_2
\vdots
A_N

Output

Print the number of pairs with integer product A_i \cdot A_j (and i < j).

Sample Input 1

5
7.5
2.4
17.000000001
17
16.000000000

Sample Output 1

3

There are 3 pairs with integer product:

7.5 \cdot 2.4 = 18

7.5 \cdot 16 = 120

17 \cdot 16 = 272

Sample Input 2

11
0.9
1
1
1.25
2.30000
5
70
0.000000001
9999.999999999
0.999999999
1.000000001

Sample Output 2

8


Score : 700 points

Problem Statement

Limak can repeatedly remove one of the first two characters of a string,
for example abcxyx \rightarrow acxyx \rightarrow cxyx \rightarrow cyx.

You are given N different strings S_1, S_2, \ldots, S_N.
Among N \cdot (N-1) / 2 pairs (S_i, S_j),
in how many pairs could Limak obtain one string from the other?

Constraints

2 \leq N \leq 200\,000

S_i consists of lowercase English letters a-z.

S_i \neq S_j

1 \leq |S_i|

|S_1| + |S_2| + \ldots + |S_N| \leq 10^6

Input

Input is given from Standard Input in the following format.

N
S_1
S_2
\vdots
S_N

Output

Print the number of unordered pairs (S_i, S_j)
where i \neq j and Limak can obtain one string from the other.

Sample Input 1

3
abcxyx
cyx
abc

Sample Output 1

1

The only good pair is (abcxyx, cyx).

Sample Input 2

6
b
a
abc
c
d
ab

Sample Output 2

5

There are five good pairs: (b, abc), (a, abc), (abc, c), (b, ab), (a, ab).


#include <stdio.h>

int main()
{
  int n;
  scanf("%d",&n);
  if(n>=30)
    printf("Yes");
  else
    printf("No");
}

Score : 100 points

Problem Statement

You will turn on the air conditioner if, and only if, the temperature of the room is 30 degrees Celsius or above.

The current temperature of the room is X degrees Celsius. Will you turn on the air conditioner?

Constraints

-40 \leq X \leq 40

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print Yes if you will turn on the air conditioner; print No otherwise.

Sample Input 1

25

Sample Output 1

No

Sample Input 2

30

Sample Output 2

Yes


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

int main()
{
    int x;scanf("%d",&x);if(x>=30)printf("YES\n");
    else
        printf("NO\n");
    return 0;
}


#include<stdio.h>
#include<math.h>
int main()
{
  double a,b,dis;
  int num;
  int X[200000];
  int Y[200000];
  int count=0;
  scanf("%d　%lf",&num,&dis);
  for (int i=0;i<num;i++)
  {
    scanf("%d %d",&X[i],&Y[i]);
    a=X[i];
    b=Y[i];
    if(pow(a,2.0)+pow(b,2.0)<=pow(dis,2.0))
    {
      count++;
    }
       }
    printf("%d",count);
    return 0;
  }

Score : 200 points

Problem Statement

We have N points in the two-dimensional plane. The coordinates of the i-th point are (X_i,Y_i).

Among them, we are looking for the points such that the distance from the origin is at most D. How many such points are there?

We remind you that the distance between the origin and the point (p, q) can be represented as \sqrt{p^2+q^2}.

Constraints

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

0 \leq D \leq 2\times 10^5

|X_i|,|Y_i| \leq 2\times 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N D
X_1 Y_1
\vdots
X_N Y_N

Output

Print an integer representing the number of points such that the distance from the origin is at most D.

Sample Input 1

4 5
0 5
-2 4
3 4
4 -4

Sample Output 1

3

The distance between the origin and each of the given points is as follows:

\sqrt{0^2+5^2}=5

\sqrt{(-2)^2+4^2}=4.472\ldots

\sqrt{3^2+4^2}=5

\sqrt{4^2+(-4)^2}=5.656\ldots

Thus, we have three points such that the distance from the origin is at most 5.

Sample Input 2

12 3
1 1
1 1
1 1
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

Sample Output 2

7

Multiple points may exist at the same coordinates.

Sample Input 3

20 100000
14309 -32939
-56855 100340
151364 25430
103789 -113141
147404 -136977
-37006 -30929
188810 -49557
13419 70401
-88280 165170
-196399 137941
-176527 -61904
46659 115261
-153551 114185
98784 -6820
94111 -86268
-30401 61477
-55056 7872
5901 -163796
138819 -185986
-69848 -96669

Sample Output 3

6


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
	int n,d,s=0;
	scanf("%d %d",&n,&d);
	while(n--)
	{
		long long int x,y;
		double dis;
		scanf("%lld %lld",&x,&y);
		x=x*x;
		y=y*y;
		dis=x+y;
		dis=(double) sqrt(dis);
		if(dis<d || dis==d){
			s++;

		}
	}
	printf("%d\n",s);
	return 0;
�}


#include<stdio.h>
#include<math.h>
int main (void){
long long int n,d,i,count;
long long int s,z;
long long int x[2*10*10*10*10*10];
long long int y[2*10*10*10*10*10];
s=0;
z=0;
count=0;
scanf("%lld %lld",&n,&d);
for(i=0;i<n;i++){
    scanf("%lld %lld",&x[i],&y[i]);
    s = pow(x[i],2)+pow(y[i],2);
    z = sqrt(s);
    if(d>=z)count++;
}
printf("%lld",count);
return 0;
}


#include<stdio.h>

int main(){
  long long k, count = 1;
  long long se = 7;
  scanf("%lld", &k);
  while(1){
    if(se % k == 0){
      printf("%lld\n" , count);
      break;
    }
    if (k % 2 == 0) {
      printf("-1\n");
      break;
    }
    se= se*10 + 7;
    count++;
  }
  return 0;
}

Score : 300 points

Problem Statement

Takahashi loves the number 7 and multiples of K.

Where is the first occurrence of a multiple of K in the sequence 7,77,777,\ldots? (Also see Output and Sample Input/Output below.)

If the sequence contains no multiples of K, print -1 instead.

Constraints

1 \leq K \leq 10^6

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print an integer representing the position of the first occurrence of a multiple of K. (For example, if the first occurrence is the fourth element of the sequence, print 4.)

Sample Input 1

101

Sample Output 1

4

None of 7, 77, and 777 is a multiple of 101, but 7777 is.

Sample Input 2

2

Sample Output 2

-1

All elements in the sequence are odd numbers; there are no multiples of 2.

Sample Input 3

999983

Sample Output 3

999982


#include <stdio.h>
#include <math.h>
#include <string.h>
typedef long long ll;
#define N 105

ll cal(ll n){
    ll t = n;
    ll p = 1,q = 1;
    while(t >= 10){
        p *= 10;
        q *= 9;
        t /= 10;
    }

    t = n;
    ll cnt = 0;
    while(p){
        ll num = t/p;t %= p;
        if(num == 7){
            cnt += num*q;
            --cnt;
            break;
        }
        if(num > 7) --num;
        cnt += num*q;
        p /= 10;
        q /= 9;
    }
    return n-cnt;
}

int main()
{
    ll n;
    while(~scanf("%lld",&n)) printf("%lld\n",cal(n));
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdbool.h>
typedef long long LL;
typedef long double Lf;
#define in (void)scanf
#define Rep(i,a,n)for(int i=(int)(a);i<(int)(n);i++)
//#define Rep(i,a,n)for(LL i=(LL)(a);i<(LL)(n);i++)
#define rep(i,n)Rep(i,0,n)
#define Repp(i,l,r,k)for(int i=(int)(l);i<(int)(r);i+=(int)(k))
//#define Repp(i,l,r,k)for(LL i=(LL)(l);i<(LL)(r);i+=(LL)(k))
#define Ma(x,y) ((x)>(y)?(x):(y))
#define mi(x,y) ((x)<(y)?(x):(y))
#define MOD 1000000007//998244353,1000000007,1000000009,4999999999999997
int upi(const void* a, const void* b) { return*(int*)a<*(int*)b ? -1 : *(int*)a> * (int*)b ? 1 : 0; }
int downi(const void* a, const void* b) { return*(int*)a<*(int*)b ? 1 : *(int*)a> * (int*)b ? -1 : 0; }
void sortupi(int* a, int n) { qsort(a, n, sizeof(int), upi); } //1,2,3,4,...
void sortdowni(int* a, int n) { qsort(a, n, sizeof(int), downi); }
int upLL(const void* a, const void* b) { return*(LL*)a<*(LL*)b ? -1 : *(LL*)a> * (LL*)b ? 1 : 0; }
int downLL(const void* a, const void* b) { return*(LL*)a<*(LL*)b ? 1 : *(LL*)a> * (LL*)b ? -1 : 0; }
void sortupLL(LL* a, int n) { qsort(a, n, sizeof(LL), upLL); }
void sortdownLL(LL* a, int n) { qsort(a, n, sizeof(LL), downLL); }
int cmp_char(const void* a, const void* b) { return *(char*)a - *(char*)b; } // a,b,c,d
int cmp_str(const void* a, const void* b) { return strcmp(*(const char**)a, *(const char**)b); } // aaa,aab
LL gcd(LL p, LL q) { while (q) { LL t = p % q; p = q; q = t; }return p; }
LL modpow(LL a,LL n,int m){LL x=1;for(a%=m;n;n/=2)n&1?x=x*a%m:0,a=a*a%m;return x;}
#define invmodpow(b,p)modpow(b,p-2,p)// a/b≡a*b^{-1}(mod p)...return b^{-1}(mod p)
typedef struct fac { LL n, exp; }F;
LL fact(LL n,F*a){LL x=n,i,j,c=0;for(i=2;i*i<=x;i++){for(j=1;x%i==0;j++)x/=i;if(j>1){a[c].n=i;a[c++].exp=j-1;}}if(x-1){a[c].n=x;a[c++].exp=1;}return c;}
LL nibtan(LL* a,LL n,LL x){LL p=0,q=n/2,r=n-1;while(r-p>1){if(x>=a[q])p=q;if(x<=a[q])r=q;q=(p+r)/2;}return a[p]==x?p:r;}
//snippet

#define N 100009

int n, k, a[N];

LL check(int x) {
	LL ret = 0;
	rep(i, n)ret += (LL)ceill((Lf)a[i] / (Lf)x) - 1;
	return ret;
}

void run(void) {
	//saw editorial
	in("%d%d", &n, &k);
	rep(i, n)in("%d", &a[i]);

	int p = 0, q = MOD / 2, r = MOD, ans = n;
	while (r - p > 1) {
		if (check(q) <= (LL)k) r = q;
		else p = q;
		q = (p + r) / 2;
	}
	if (check(r) <= k)ans = r;
	if (check(q) <= k)ans = q;
	if (check(p) <= k)ans = p;
	printf("%d\n", ans ? ans : 1);
}

int main(void) {
	int test = 1;
//	in("%d", &test);
	while (test--) {
		run();
	}
	return 0;
}

Score : 500 points

Problem Statement

We have N logs of lengths A_1,A_2,\cdots A_N.

We can cut these logs at most K times in total. When a log of length L is cut at a point whose distance from an end of the log is t (0<t<L), it becomes two logs of lengths t and L-t.

Find the shortest possible length of the longest log after at most K cuts, and print it after rounding up to an integer.

Constraints

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

0 \leq K \leq 10^9

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \cdots A_N

Output

Print an integer representing the answer.

Sample Input 1

2 3
7 9

Sample Output 1

4

First, we will cut the log of length 7 at a point whose distance from an end of the log is 3.5, resulting in two logs of length 3.5 each.

Next, we will cut the log of length 9 at a point whose distance from an end of the log is 3, resulting in two logs of length 3 and 6.

Lastly, we will cut the log of length 6 at a point whose distance from an end of the log is 3.3, resulting in two logs of length 3.3 and 2.7.

In this case, the longest length of a log will be 3.5, which is the shortest possible result. After rounding up to an integer, the output should be 4.

Sample Input 2

3 0
3 4 5

Sample Output 2

5

Sample Input 3

10 10
158260522 877914575 602436426 24979445 861648772 623690081 433933447 476190629 262703497 211047202

Sample Output 3

292638192


#include<stdio.h>
int main()
{
	int a,b,c,k;
	int flag1=0,flag2=0;
	scanf("%d%d%d%d",&a,&b,&c,&k);
	while(a>b&&k>0)
	{b=2*b;
	k--;
	if(a<b){flag1=1;break;}
	}
	while(b>c&&k>0)
	{c=2*c;
	k--;
	if(b<c){flag2=1;break;}
	}
	if(flag1==1&flag2==1)printf("Yes");
	else printf("No");
	return 0;
}

Score: 200 points

Problem Statement

M-kun has the following three cards:

A red card with the integer A.

A green card with the integer B.

A blue card with the integer C.

He is a genius magician who can do the following operation at most K times:

Choose one of the three cards and multiply the written integer by 2.

His magic is successful if both of the following conditions are satisfied after the operations:

The integer on the green card is strictly greater than the integer on the red card.

The integer on the blue card is strictly greater than the integer on the green card.

Determine whether the magic can be successful.

Constraints

1 \leq A, B, C \leq 7

1 \leq K \leq 7

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C
K

Output

If the magic can be successful, print Yes; otherwise, print No.

Sample Input 1

7 2 5
3

Sample Output 1

Yes

The magic will be successful if, for example, he does the following operations:

First, choose the blue card. The integers on the red, green, and blue cards are now 7, 2, and 10, respectively.

Second, choose the green card. The integers on the red, green, and blue cards are now 7, 4, and 10, respectively.

Third, choose the green card. The integers on the red, green, and blue cards are now 7, 8, and 10, respectively.

Sample Input 2

7 4 2
3

Sample Output 2

No

He has no way to succeed in the magic with at most three operations.


#include <stdio.h>

int main() {
  int red, blue, green, k;
  int i=0;

  scanf("%d%d%d%d", &red, &green, &blue, &k);

  while(green<=red || blue<=green) {
    if(green<=red) {
      green = 2*green;
      i++;
    } else if(blue<=green) {
      blue = 2*blue;
      i++;
    }
  }

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

  if(i<=k) {
    printf("Yes");
  } else if(i>k){
    printf("No");
  }

  return 0;
}


#include<stdio.h>
int main() {
	int a, b, c;
	int k;
	scanf("%d %d %d", &a, &b, &c);
	scanf("%d", &k);
	for (int i = 0; i < k; i++) {
		if (a > b) {
			b *= 2;
			continue;
		}
		if (b > c) {
			c *= 2;
			continue;
		}
		if (a < b && b < c) {
			printf("Yes");
			break;
		}
	}
  	if(a>b||b>c){
      printf("No");
    }
	return 0;
}


#include<stdio.h>

int main (void){

    int L,R,d;
    int min,max;
    scanf("%d %d %d",&L,&R,&d);

    min=L+d-L%d;
    max=R-R%d;

    printf("%d",max/d-min/d+1);

    return 0 ;

}

Score : 100 points

Problem Statement

How many multiples of d are there among the integers between L and R (inclusive)?

Constraints

All values in input are integers.

1 \leq L \leq R \leq 100

1 \leq d \leq 100

Input

Input is given from Standard Input in the following format:

L R d

Output

Print the number of multiples of d among the integers between L and R (inclusive).

Sample Input 1

5 10 2

Sample Output 1

3

Among the integers between 5 and 10, there are three multiples of 2: 6, 8, and 10.

Sample Input 2

6 20 7

Sample Output 2

2

Among the integers between 6 and 20, there are two multiples of 7: 7 and 14.

Sample Input 3

1 100 1

Sample Output 3

100


Score : 200 points

Problem Statement

We have N squares assigned the numbers 1,2,3,\ldots,N. Each square has an integer written on it, and the integer written on Square i is a_i.

How many squares i satisfy both of the following conditions?

The assigned number, i, is odd.

The written integer is odd.

Constraints

All values in input are integers.

1 \leq N, a_i \leq 100

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \cdots a_N

Output

Print the number of squares that satisfy both of the conditions.

Sample Input 1

5
1 3 4 5 7

Sample Output 1

2

Two squares, Square 1 and 5, satisfy both of the conditions.

For Square 2 and 4, the assigned numbers are not odd.

For Square 3, the written integer is not odd.

Sample Input 2

15
13 76 46 15 50 98 93 77 31 43 84 90 6 24 14

Sample Output 2

3


#include <stdio.h>

int main() {
    int N,An,count=0;
    scanf("%d\n", &N);
    for(int i=0;i<N;i++){
        scanf("%d",&An);
        if(i%2 == 1){
            if(An%2 == 1){
                count++;
            }
        }
    }
    printf("%d", count);
    return 0;
}


Score : 300 points

Problem Statement

Let f(n) be the number of triples of integers (x,y,z) that satisfy both of the following conditions:

1 \leq x,y,z

x^2 + y^2 + z^2 + xy + yz + zx = n

Given an integer N, find each of f(1),f(2),f(3),\ldots,f(N).

Constraints

All values in input are integers.

1 \leq N \leq 10^4

Input

Input is given from Standard Input in the following format:

N

Output

Print N lines. The i-th line should contain the value f(i).

Sample Input 1

20

Sample Output 1

0
0
0
0
0
1
0
0
0
0
3
0
0
0
0
0
3
3
0
0

For n=6, only (1,1,1) satisfies both of the conditions. Thus, f(6) = 1.

For n=11, three triples, (1,1,2), (1,2,1), and (2,1,1), satisfy both of the conditions. Thus, f(6) = 3.

For n=17, three triples, (1,2,2), (2,1,2), and (2,2,1), satisfy both of the conditions. Thus, f(17) = 3.

For n=18, three triples, (1,1,3), (1,3,1), and (3,1,1), satisfy both of the conditions. Thus, f(18) = 3.


#include<stdio.h>

int main() {
	int n;
	int result = 0;

	scanf("%d", &n);

	int b, c;
	int i_flag, j_flag, k_flag;
	for (int cor = 1; cor <= n; cor++)
	{
		result = 0;
		b = 0, c = 0;
		i_flag = 0, j_flag = 0, k_flag = 0;
		for (int i = 1; i_flag == 0; i++)
		{
			j_flag = 0;
			for (int j = 1; j_flag == 0; j++)
			{
				k_flag = 0;
				for (int k = 1; k_flag == 0; k++)
				{
					int cal = i * i + j * j + k * k + i * j + j * k + k * i;
					if (cal == cor) {
						if (i == j && j == k) {
							result += 1;
						}
						else if (i == j || i == k || j == k) {
							b++;
							if (b == 3) {
								result += 3;
								b = 0;
							}
						}
						else {
							c++;
							if (c == 6) {
								result += 6;
								c = 0;
							}
						}
					}
					else if (cal > cor) {
						k_flag = 1;
						if (k == 1) {
							j_flag = 1;
							if (j == 1) {
								i_flag = 1;
								printf("%d\n", result);
							}
						}
					}
				}
			}
		}
	}

	return 0;
}


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

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

  int max;
  int count;
  for(int i=1;i<=N;i++){
    count = 0;
    max = sqrt(N);
    for(int x=1;x<max;x++){
      for(int y=1;y<x+1;y++){
	for(int z=1;z<y+1;z++){
	  if(pow(x,2.0)+pow(y,2.0)+pow(z,2.0)+x*y+y*z+z*x == i){
	    if(x==y && y==z) count++;
	    else if(x==y || y==z || z==x) count += 3;
	    else count += 6;
	  }
	}
      }
    }
    printf("%d\n", count);
  }

  return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
    int num, counter_ac = 0, counter_wa = 0, counter_tle = 0, counter_re = 0;
    char result[10];

    scanf("%d", &num);

    int i = 0;
    while(i < num){
        scanf("%s", result);
        if(strcmp(result, "AC") == 0){
            counter_ac++;
        } else if(strcmp(result, "WA") == 0){
            counter_wa++;
        } else if(strcmp(result, "TLE") == 0){
            counter_tle++;
        } else{
            counter_re++;
        }
        i++;
    }
    i = 0;

    printf("AC x %d\n", counter_ac);
    printf("WA x %d\n", counter_wa);
    printf("TLE x %d\n", counter_tle);
    printf("RE x %d\n", counter_re);
}

Score : 200 points

Problem Statement

Takahashi is participating in a programming contest called AXC002, and he has just submitted his code to Problem A.

The problem has N test cases.

For each test case i (1\leq i \leq N), you are given a string S_i representing the verdict for that test case. Find the numbers of test cases for which the verdict is AC, WA, TLE, and RE, respectively.

See the Output section for the output format.

Constraints

1 \leq N \leq 10^5

S_i is AC, WA, TLE, or RE.

Input

Input is given from Standard Input in the following format:

N
S_1
\vdots
S_N

Output

Let C_0, C_1, C_2, and C_3 be the numbers of test cases for which the verdict is AC, WA, TLE, and RE, respectively. Print the following:

AC x C_0
WA x C_1
TLE x C_2
RE x C_3

Sample Input 1

6
AC
TLE
AC
AC
WA
TLE

Sample Output 1

AC x 3
WA x 1
TLE x 2
RE x 0

We have 3, 1, 2, and 0 test case(s) for which the verdict is AC, WA, TLE, and RE, respectively.

Sample Input 2

10
AC
AC
AC
AC
AC
AC
AC
AC
AC
AC

Sample Output 2

AC x 10
WA x 0
TLE x 0
RE x 0


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

int main(void)
{
    int n;
    int ac = 0, wa = 0, tle = 0, re = 0;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        char tmp[10];
        scanf("%s", tmp);
        if (!strcmp(tmp, "AC"))
        {
            ac++;
        }
        if (!strcmp(tmp, "WA"))
        {
            wa++;
        }
        if (!strcmp(tmp, "TLE"))
        {
            tle++;
        }
        if (!strcmp(tmp, "RE"))
        {
            re++;
        }
    }
    printf("AC x %d\n", ac);
    printf("WA x %d\n", wa);
    printf("TLE x %d\n", tle);
    printf("RE x %d\n", re);

    return 0;
}


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

int main(){
  char C[3];
  int AC=0;
  int WA=0;
  int TLE=0;
  int RE=0;
  int k=0;
  int N=0;
  k = scanf("%d", &N);
  for (int i=0; i<N; i++){
    k = scanf("%s", C);
    if (strcmp(C, "AC")==0){
      ++AC;
    }else if (strcmp(C, "WA")==0){
      ++WA;
    }else if (strcmp(C, "TLE")==0){
      ++TLE;
    }else{
      ++RE;
    }

  }

  printf("AC x %d\nWA x %d\nTLE x %d\nRE x %d\n", AC, WA, TLE, RE);

  return 0;
}


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

int main(void)
{
	int i,n,C0,C1,C2,C3;
	char str[100];

	C0 = C1 = C2 = C3 = 0;

	scanf("%d",&n);

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

		if(str == "AC"){
			C0++;
		}
		else if(str =="WA"){
			C1++;
		}
		else if(str =="TLE"){
			C2++;
		}
		else if(str =="RE"){
			C3++;
		}
	}

	printf("AC * %d\nWA * %d\nTLE * %d\nRE * %d\n",C0,C1,C2,C3);

	return 0;
}


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

int main(void) {
    int n;
    int ac=0,wa=0,tle=0,re=0;
    char s[3];
        scanf("%d", &n);
        for(int i=0;i<n;i++){
          scanf("%s", s);
          if(!strcmp(s,"AC"))
            ac += 1;
          if(!strcmp(s,"WA"))
            wa += 1;
          if(!strcmp(s,"TLE"))
            tle += 1;
          if(!strcmp(s,"RE"))
            re += 1;
        }
        printf("AC x %d\nWA x %d\nTLE x %d\nRE x %d\n",ac,wa,tle,re);

        return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<stdbool.h>
#include<assert.h>
#include<ctype.h>
typedef long long ll;
typedef long double ld;
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define repp(i,l,r,k)for(ll i=(l);i<(r);i+=(k))
#define rrep(i,l,r)for(ll i=(l);i>=(r);i--)
#define INF (1LL<<60)
#define MOD1 1000000007
#define MOD2 998244353
#define MAX_N (1 << 20)
#define YES printf("Yes\n")
#define NO printf("No\n")
#define PN printf("\n")
#define charsize 100005 //10^5+5
#define PI 3.141592653589793238

void swap(ll *a, ll *b){ll c;c=*b;*b=*a;*a= c;}
void cin(ll *n){ scanf("%lld",&(*n)); }
ll max2(ll a,ll b){return a>=b?a:b;}
ll min2(ll a,ll b){return a>=b?b:a;}
ll min3(ll a, ll b, ll c){return (a<=b && a<=c) ? a : b<=c ? b : c;}
ll max3(ll a, ll b, ll c){return (a>=b && a>=c) ? a : b>=c ? b : c;}
ll minn(ll n, ll a[]){ll b=INF;rep(i,0,n) b=min2(b,a[i]);return b;}
ll maxn(ll n, ll a[]){ll b=-INF;rep(i,0,n) b=max2(b,a[i]);return b;}
ll POW(ll a, ll b){ll c=1;rep(i,0,b) c*=a;return c;}
double POW_d(double a, double b){double c=1;rep(i,0,b) c*=a;return c;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll mod_MOD1(ll n){n+= n<0?((-n)/MOD1+1)*MOD1:0; return n%=MOD1;}
ll mod_p(ll n ,ll p){n+= n<0?((-n)/p+1)*p:0; return n%=p;}
ll change_into_num(char s[] , ll len, ll p){ return !p ? 0 : POW(10,p-1)*(s[len-p]-'0') + change_into_num(s,len,p-1); }
ll digits(ll a, ll b){return a/b?1+digits(a/b,b):1;}
ll base(ll n, ll a, ll i){return i==1?n%a:base(n/a,a,i-1);}

void lr_lower( int *l, int *r, ll am, ll val , int type ){ (type<3) ? ( am < val ?  ( *l = (*l+*r)/2 ) : ( *r= (*l+*r)/2 ) ) : ( am <= val ? ( *l = (*l+*r)/2 ) : ( *r= (*l+*r)/2 ) ); }
void lr_upper( int *l, int *r, ll am, ll val , int type ){ (type<3) ? ( am <= val ?  ( *l = (*l+*r)/2 ) : ( *r= (*l+*r)/2 ) ) : ( am < val ? ( *l = (*l+*r)/2 ) : ( *r= (*l+*r)/2 ) ); }
int cmp_lower( ll a, ll b, int type ){ return (type==1) ? ( a==b ? 1 : 0 ) : (type==2) ? ( a>=b ? 1 : 0 ) : ( a>b ? 1 : 0 ) ; }
int cmp_upper( ll a, ll b, int type ){ return (type==1) ? ( a==b ? 1 : 0 ) : (type==2) ? ( a<=b ? 1 : 0 ) : ( a<b ? 1 : 0 ) ; }
// return smallest p  which meets  a[p]==val :1  >=:2   >:3
ll lower_bound( ll a[], int l, int r, ll val , int type ){  while(r-l>1) lr_lower(&l,&r,a[ (l+r)/2 ],val,type);  return cmp_lower(a[l],val,type) ? l : cmp_lower(a[r],val,type) ? r : -1;  }
// return biggest p  which meets   a[p]==val :1  <=:2   <:3
ll upper_bound( ll a[], int l, int r, ll val , int type ){  while(r-l>1) lr_upper(&l,&r,a[ (l+r)/2 ],val,type); return cmp_upper(a[r],val,type) ? r : cmp_upper(a[l],val,type) ? l : -1; }
// count i  which meets ai==x
ll count(ll a[], int l, int r, ll x){  int p = lower_bound(a,l,r,x,1);  return p==-1 ? 0 : upper_bound(a,p,r,x,1)-p+1; }

ll *factors[2] , fac_cnt=0 , is_factor_prepared=0;
ll factor_pre(){  rep(i,0,1){ if(is_factor_prepared++) return 0; }  ll tmp=(1e6)/2+1, fac_tmp[tmp];  rep(i,0,tmp){fac_tmp[i]=i?2*i+1:2;}  rep(i,1,tmp){if(fac_tmp[i]){  repp(j,3,tmp/(2*i+1)+1,2 ){  if( j*(2*i+1)<tmp ) fac_tmp[ (j*(2*i+1)-1)/2 ]=0;  }  }else continue;}  rep(i,0,tmp){if(fac_tmp[i]){  rep(j,0,2){ factors[j] = realloc( factors[j] , sizeof(ll)*( fac_cnt +1 ) );  factors[j][j?fac_cnt++:fac_cnt]=j?0:fac_tmp[i];  }  } }  return 0;  }
ll factor(ll n, ll new_common_plus){ factor_pre();  rep(i,0,fac_cnt){ ll cnt=0; rep(j,0,1){ while( ( cnt+= n %factors[0][i]==0 ? 1 : 0 )  && (n/=factors[0][i]) %factors[0][i]==0 ) continue; } factors[1][i]= new_common_plus==1 ? cnt :  new_common_plus==2 ? max2(factors[1][i],cnt) : factors[1][i]+cnt ;  if( factors[0][i]> n ) break; } return n; }
ll judge_prime(ll n){ factor_pre(); rep(i,0,fac_cnt){ if(n<factors[0][i]*factors[0][i] || n==factors[0][i]) break; else if(n%factors[0][i]==0) n/=n; }  return n==1?0:1; }

ll *mf_arr,*inv_arr,*finv_arr,is_minv_made=0,is_mf_made=0;
ll makeinv(ll n , ll mod){  rep(i,0,1){if(is_minv_made++) return 0;}  inv_arr = realloc(inv_arr, sizeof(ll)*2 );  finv_arr = realloc(finv_arr, sizeof(ll)*2 );  inv_arr[1]=1;finv_arr[0]=finv_arr[1]=1;  rep(i,2,n+1){  inv_arr = realloc(inv_arr, sizeof(ll)*(i+1) );  finv_arr = realloc(finv_arr, sizeof(ll)*(i+1) );  inv_arr[i]= mod - inv_arr[mod%i] * (mod / i) % mod;  finv_arr[i] = finv_arr[i - 1] * inv_arr[i] % mod;  }  return 0; }
ll make_mf(ll n, ll mod){  rep(i,0,1){ if(is_mf_made++) return 0; }  mf_arr = realloc(mf_arr, sizeof(ll)*2 );  ll x=1;  mf_arr[0]=mf_arr[1]=x;  rep(i,2,n+1){   x=x*i%mod;  mf_arr = realloc(mf_arr, sizeof(ll)*(i+1) );  mf_arr[i]=x; }  return 0;  }
ll m_inv(ll x, ll mod, ll is_fac ){ makeinv(2*1e6+10,mod);  return is_fac?finv_arr[x]:inv_arr[x]; }
ll m_f(ll x, ll mod){  make_mf(2*1e6+10,mod);  return mf_arr[x];  }
// ll mod_nck(ll n, ll k, ll mod){ return m_f[n]*finv_arr[k]%mod*finv_arr[n-k]%mod; }
ll m_p(ll r,ll n,ll mod){  ll t=1,s=r;  while(n>0){ t = (n&1) ? t*s%mod : t;  s=s*s%mod; n>>=1; }  return r?t:0;  }
ll m_mul2(ll a, ll b, ll mod){ return a*b%mod; }
ll m_mul3(ll a, ll b, ll c, ll mod){ return m_mul2(a*b%mod,c,mod); }
ll m_mul4(ll a, ll b, ll c, ll d, ll mod){ return m_mul3(a*b%mod,c,d,mod); }
ll m_mul5(ll a, ll b, ll c, ll d, ll e, ll mod){ return m_mul4(a*b%mod,c,d,e,mod); }

int upll(const void*a, const void*b){return*(ll*)a<*(ll*)b?-1:*(ll*)a>*(ll*)b?1:0;}
int downll(const void*a, const void*b){return*(ll*)a<*(ll*)b?1:*(ll*)a>*(ll*)b?-1:0;}
int cmp_string( const void * a , const void * b ) {  return strcmp( (char *)a , (char *)b ); }  // qsort((void*)s,n,sizeof(s[0]),int_sort );
int cmp_char(const void * a, const void * b) { return *(char *)a - *(char *)b;}
void sortup(ll*a,int n){qsort(a,n,sizeof(ll),upll);}
void sortdown(ll*a,int n){qsort(a,n,sizeof(ll),downll);}
void sort_string(int n,int size,char s[][size]){ qsort( (void*)s , n , sizeof(s[0]) , cmp_string ); }
void sort_char(char *s){ qsort( (void *)s , strlen(s) , sizeof(char) , cmp_char ); }
ll unique_string(ll n ,ll size, char s[][size]){ ll ans=1; rep(i,1,n) if( strcmp(s[i],s[i-1]) ) ans++; return ans; }
ll unique_num(ll n , ll a[]){ ll ans=1; rep(i,1,n) if( a[i]!=a[i-1] ) ans++; return ans; }

typedef struct{ ll a , b;}fr;
int cmp1( const void *p, const void *q ) { return ((fr*)p) ->a - ((fr*)q)->a;}
int cmp2( const void *p, const void *q ) { return ((fr*)q) ->a - ((fr*)p)->a;}
void strsortup(fr*a,int n){qsort(a,n,sizeof(fr),cmp1);}
void strsortdown(fr*a,int n){qsort(a,n,sizeof(fr),cmp2);}

// char s[1151154];
int main(void){
    // fgets(s,sizeof(s),stdin);
    ll n,k;
    ll ans=0;
    cin(&n);
    cin(&k);
    // scanf("%s",s);
    ll a[n],z=0,mi=0;
    rep(i,0,n){
        cin(&a[i]);
        if(!a[i]) z++;
        if(a[i]<0) mi++;
    }
    if(z>n-k){
        printf("0\n"); return 0;
    }

    sortup(a,n);
    // printf("%lld\n",z+mi);
    if(z+mi==n){
        if(mi%2){
            ans=1;
            rrep(i,n-1,n-k){
                ans=mod_MOD1(ans*a[i]);
            }
        }else{
            ans=1;
            rrep(i,n-1,n-k){
                ans=mod_MOD1(ans*a[i]);
            }
        }
        printf("%lld\n",mod_MOD1(ans));
        return 0;
    }
    if(n==k){
        ans=1;
        rep(i,0,n){
            ans=mod_MOD1(ans*a[i]);
        }
        printf("%lld\n",ans); return 0;
    }

    if(k%2){
        ll tmp=a[n-1],cnt=1;
        rrep(i,n-2,n-k){
            if(a[i]<=0) break;
            tmp=mod_MOD1(tmp*a[i]);
            tmp=mod_MOD1(tmp*a[i-1]);
            cnt+=2; i--;
        }
        if(cnt<k){
            rep(i,0,k-cnt){
                tmp=mod_MOD1(tmp*a[i]);
            }
        }
        ans=tmp;
        ll d=0,p=0,sa=0;
        rrep(i,0+(k-cnt),mi){
            if(a[i+1]>0) break;
            tmp*=m_inv(a[n-1-i],MOD1,0); tmp%=MOD1;
            tmp*=m_inv(a[n-2-i],MOD1,0); tmp%=MOD1;
            tmp*= mod_MOD1( a[i]*a[i+1] );  tmp%=MOD1;
            d+=a[n-1-i]*a[n-2-i];
            p+=a[i]*a[i+1];
            if( sa<p-d ){
                ans=tmp;
                sa=p-d;
            }
            i--;
        }
        printf("%lld\n",mod_MOD1(ans));
        return 0;
    }

    ll tmp=1,cnt=0;
    rrep(i,n-1,n-k){
        if(a[i-1]<=0) break;
        tmp=mod_MOD1(tmp*a[i]);
        tmp=mod_MOD1(tmp*a[i-1]);
    // printf("%lld %lld\n",a[i],a[i-1]);
        cnt+=2; i--;
    }
    // printf("%lld\n",tmp);
    if(cnt<k){
        rep(i,0,k-cnt){
            tmp=mod_MOD1(tmp*a[i]);
        }
    }
    ans=tmp;
    ll d=0,p=0,sa=0;
    repp(i,0+(k-cnt),mi,2){
        if(a[i+1]>0) break;
        tmp*=m_inv(a[n-1-i],MOD1,0); tmp%=MOD1;
        tmp*=m_inv(a[n-2-i],MOD1,0); tmp%=MOD1;
        tmp*= mod_MOD1( a[i]*a[i+1] );  tmp%=MOD1;
        d+=a[n-1-i]*a[n-2-i];
        p+=a[i]*a[i+1];
        if( sa<p-d ){
            ans=tmp;
            sa=p-d;
        }
        // printf("%lld\n",tmp);
    }

    printf("%lld\n",ans);
    return 0;
}

Score : 500 points

Problem Statement

Given are N integers A_1,\ldots,A_N.

We will choose exactly K of these elements. Find the maximum possible product of the chosen elements.

Then, print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Constraints

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

|A_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N K
A_1 \ldots A_N

Output

Print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Sample Input 1

4 2
1 2 -3 -4

Sample Output 1

12

The possible products of the two chosen elements are 2, -3, -4, -6, -8, and 12, so the maximum product is 12.

Sample Input 2

4 3
-1 -2 -3 -4

Sample Output 2

1000000001

The possible products of the three chosen elements are -24, -12, -8, and -6, so the maximum product is -6.

We print this value modulo (10^9+7), that is, 1000000001.

Sample Input 3

2 1
-1 1000000000

Sample Output 3

1000000000

The possible products of the one chosen element are -1 and 1000000000, so the maximum product is 1000000000.

Sample Input 4

10 10
1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1

Sample Output 4

999983200

Be sure to print the product modulo (10^9+7).


#pragma GCC optimize("Ofast")
#include<stdio.h>
int main(void)
{
    long n = 0;
    char c=getchar_unlocked();
    while(c<='9'&&c>='0') {n=(n<<1)+(n<<3)+c-'0'; c=getchar_unlocked();}
    long ans = n * (n * (n + 3) + 2);
    ans /= 6;
    int u, v, i = 1;
    ++n;
    while(++i < n)
    {
        u = v = 0;
        c=getchar_unlocked();
        while(c<='9'&&c>='0') {u=(u<<1)+(u<<3)+c-'0'; c=getchar_unlocked();}
        c=getchar_unlocked();
        while(c<='9'&&c>='0') {v=(v<<1)+(v<<3)+c-'0'; c=getchar_unlocked();}
        switch(u > v)
        {
            case 1: ans -= v * (n - u); break;
            default: ans -= u * (n - v);
        }
    }
    printf("%ld", ans + 1);
    return 0;
}

Score : 600 points

Problem Statement

We have a tree with N vertices and N-1 edges, respectively numbered 1, 2,\cdots, N and 1, 2, \cdots, N-1. Edge i connects Vertex u_i and v_i.

For integers L, R (1 \leq L \leq R \leq N), let us define a function f(L, R) as follows:

Let S be the set of the vertices numbered L through R. f(L, R) represents the number of connected components in the subgraph formed only from the vertex set S and the edges whose endpoints both belong to S.

Compute \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).

Constraints

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

1 \leq u_i, v_i \leq N

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}

Output

Print \sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).

Sample Input 1

3
1 3
2 3

Sample Output 1

7

We have six possible pairs (L, R) as follows:

For L = 1, R = 1, S = \{1\} and we have 1 connected component.

For L = 1, R = 2, S = \{1, 2\} and we have 2 connected components.

For L = 1, R = 3, S = \{1, 2, 3\} and we have 1 connected component, since S contains both endpoints of each of the edges 1, 2.

For L = 2, R = 2, S = \{2\} and we have 1 connected component.

For L = 2, R = 3, S = \{2, 3\} and we have 1 connected component, since S contains both endpoints of Edge 2.

For L = 3, R = 3, S = \{3\} and we have 1 connected component.

The sum of these is 7.

Sample Input 2

2
1 2

Sample Output 2

3

Sample Input 3

10
5 3
5 7
8 9
1 9
9 10
8 4
7 4
6 10
7 2

Sample Output 3

113


#include<stdio.h>

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

    int result;
    result = a + a*a + a*a*a;
    printf("%d", result);

    return 0;
}

Score : 100 points

Problem Statement

Given an integer a as input, print the value a + a^2 + a^3.

Constraints

1 \leq a \leq 10

a is an integer.

Input

Input is given from Standard Input in the following format:

a

Output

Print the value a + a^2 + a^3 as an integer.

Sample Input 1

2

Sample Output 1

14

When a = 2, we have a + a^2 + a^3 = 2 + 2^2 + 2^3 = 2 + 4 + 8 = 14.

Print the answer as an input. Outputs such as 14.0 will be judged as incorrect.

Sample Input 2

10

Sample Output 2

1110


#include <stdio.h>

int main(){
    int a;
    scanf("%d",&a);
    printf("%d",a+a*a+a*a*a);
    return 0;
}


#include<stdio.h>
int main(){
    char a[10001];
    char b[10001];
    int i = 0, count = 0, ans = 0;
    scanf("%s", &*a);
    scanf("%s", &*b);
    for(i = 0; i >= 0; i++){
        if(a[i] == '\0'){
            break;
        }else{
            count++;
        }
    }
    for(i = 0; i < count; i++){
        if(a[i] != b[i]){
            ans++;
        }
    }
    printf("%d\n", ans);
    return 0;
}

Score : 200 points

Problem Statement

Given are strings S and T. Consider changing S to T by repeating the operation below. Find the minimum number of operations required to do so.

Operation: Choose one character of S and replace it with a different character.

Constraints

S and T have lengths between 1 and 2\times 10^5 (inclusive).

S and T consists of lowercase English letters.

S and T have equal lengths.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the answer.

Sample Input 1

cupofcoffee
cupofhottea

Sample Output 1

4

We can achieve the objective in four operations, such as the following:

First, replace the sixth character c with h.

Second, replace the eighth character f with t.

Third, replace the ninth character f with t.

Fourth, replace the eleventh character e with a.

Sample Input 2

abcde
bcdea

Sample Output 2

5

Sample Input 3

apple
apple

Sample Output 3

0

No operations may be needed to achieve the objective.


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

int main(void){

    int count=0;
    int ans=0;
    int size=2*pow(10,5);
    char S[size];
    char T[size];

    scanf("%s",S);
    scanf("%s",T);

    for(int i=0;i<size;i++){
        if(S[i]!='\0'){
            count++;
        }else{
            break;
        }
    }

    for(int i=0;i<count;i++){
        if(S[i]!=T[i]){
            ans++;
        }
    }

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

    return 0;
}


#include <stdio.h>

int main()
{
	char s;

	scanf("%c", &s);

	if ('A' <= s && s <= 'Z')
		s = 'A';
	else
		s = 'a';

	printf("%c\n", s);

	return 0;
}

Score : 100 points

Problem Statement

An uppercase or lowercase English letter \alpha will be given as input.
If \alpha is uppercase, print A; if it is lowercase, print a.

Constraints

\alpha is an uppercase (A - Z) or lowercase (a - z) English letter.

Input

Input is given from Standard Input in the following format:

α

Output

If \alpha is uppercase, print A; if it is lowercase, print a.

Sample Input 1

B

Sample Output 1

A

B is uppercase, so we should print A.

Sample Input 2

a

Sample Output 2

a

a is lowercase, so we should print a.


Score : 200 points

Problem Statement

A shop sells N kinds of fruits, Fruit 1, \ldots, N, at prices of p_1, \ldots, p_N yen per item, respectively. (Yen is the currency of Japan.)

Here, we will choose K kinds of fruits and buy one of each chosen kind. Find the minimum possible total price of those fruits.

Constraints

1 \leq K \leq N \leq 1000

1 \leq p_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
p_1 p_2 \ldots p_N

Output

Print an integer representing the minimum possible total price of fruits.

Sample Input 1

5 3
50 100 80 120 80

Sample Output 1

210

This shop sells Fruit 1, 2, 3, 4, and 5 for 50 yen, 100 yen, 80 yen, 120 yen, and 80 yen, respectively.

The minimum total price for three kinds of fruits is 50 + 80 + 80 = 210 yen when choosing Fruit 1, 3, and 5.

Sample Input 2

1 1
1000

Sample Output 2

1000


#include <stdio.h>

int main(void){
  int p = 0;
  int pmin[1000] = {0};
  int n = 1;
  int k = 1;
  int i = 0;
  int j = 0;
  int sum = 0;

  for(j = 0; j < k; j++){
    pmin[j] = 0x7fff;
  }

  scanf("%d %d",&n,&k);
  for(i = 0; i < n; i++){
    scanf("%d",&p);
    for(j = 0; j < k; j++){
      if(pmin[j] > p ){
      	pmin[j] = p;
        break;
      }
    }
  }

  for(j = 0; j < k; j++){
    sum += pmin[j];
  }

  printf("%d\n",sum);
  return 0;
}


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

int to26gram(long long N) {
    if( N < 26 ) {
        printf("%c", 'a' + (int)N );
        return 0;
    }

    to26gram( (N / 26) - 1 );
    printf("%c", 'a' + (int)(N % 26) );
    return 0;
}

int main(void) {
    long long N;
    bool chk = false;

    scanf("%lld", &N);
    N--;

    to26gram( N );
    printf("\n");

    return 0;
}

Score : 300 points

Problem Statement

1000000000000001 dogs suddenly appeared under the roof of Roger's house, all of which he decided to keep. The dogs had been numbered 1 through 1000000000000001, but he gave them new names, as follows:

the dogs numbered 1,2,\cdots,26 were respectively given the names a, b, ..., z;

the dogs numbered 27,28,29,\cdots,701,702 were respectively given the names aa, ab, ac, ..., zy, zz;

the dogs numbered 703,704,705,\cdots,18277,18278 were respectively given the names aaa, aab, aac, ..., zzy, zzz;

the dogs numbered 18279,18280,18281,\cdots,475253,475254 were respectively given the names aaaa, aaab, aaac, ..., zzzy, zzzz;

the dogs numbered 475255,475256,\cdots were respectively given the names aaaaa, aaaab, ...;

and so on.

To sum it up, the dogs numbered 1, 2, \cdots were respectively given the following names:

a, b, ..., z, aa, ab, ..., az, ba, bb, ..., bz, ..., za, zb, ..., zz, aaa, aab, ..., aaz, aba, abb, ..., abz, ..., zzz, aaaa, ...

Now, Roger asks you:

"What is the name for the dog numbered N?"

Constraints

N is an integer.

1 \leq N \leq 1000000000000001

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer to Roger's question as a string consisting of lowercase English letters.

Sample Input 1

2

Sample Output 1

b

Sample Input 2

27

Sample Output 2

aa

Sample Input 3

123456789

Sample Output 3

jjddja


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

#define SIZE 100005
#define PI acosl(-1) //3.14159265358979323846264338327950L
#define rep(i, N) for (i = 0; i < N; i++) //制御変数iを用いてN回転
#define array(N, t) (t*)calloc(N, sizeof(t)) //t型N要素の1次元配列を動的確保後0クリア(freeを忘れずに)
#define zero(a); {int iter; rep(iter, sizeof(a)/sizeof((a)[0])) (a)[iter]=0;} //1次元配列aのゼロ初期化?

typedef long long ll; typedef long double ld;
typedef struct node { int data; struct node *next; } Node;

Node *list[SIZE];
int queue[SIZE]; int q_head = 0, q_tail = 0;

void print_log(); //とりあえず文字を出力してどこまで実行できているか確認(for debug)
void printa_int(int *a, int size); //int型の1次元配列aを出力(for debug)
void printl_int(int size); //リンクリストを出力(for debug)
int comp(const void *a, const void *b); //昇順並び替えに使う比較関数
Node* add_node(int i, int data); void free_list(Node* head);
int queue_empty(); int queue_full();
void enqueue(int data); int dequeue();
ll* factorize(ll N); //Nを素因数分解、得られる配列は素因数,指数,素因数,…となっている(添字はintで指定する必要あり?)
void check_factorization(ll *fact); //素因数分解を表示(for debug)

int main() {
    int i, j, N;
    ll num;
    scanf("%d", &N);
    ll *a = array(N, ll);
    rep(i, N) scanf("%lld", a + i);
    for (i = 0; i < N; i += 2) {
        num = a[i + 1];
        for (j = i + 2; j < N; j++) {
            num = num ^ a[j];
            a[j] = a[j] ^ a[i] ^ a[i + 1];
        }
        printf("%lld %lld", num, num ^ a[i + 1]^a[i]);
        if (i == N - 2) printf("\n");
        else printf(" ");
    }

    free(a);
    return 0;
}

void print_log() {
    printf("=== print log ===\n");
}
void printa_int(int *a, int size) {
    int i;
    printf("[");
    for (i = 0; i < size; i++)
        printf("%d, ", a[i]);
    printf("]\n");
}
void printl_int(int size) {
    int i;
    Node *t;
    rep(i, size) {
        printf("[%d] ", i);
        for (t = list[i]; t; t = t->next) printf("%d, ", t->data);
        printf("\n");
    }
}
int comp(const void *a, const void *b) {
    return *(int*)a - *(int*)b;
}
Node* add_node(int i, int data) {
    if (!list[i]) {
        list[i] = (Node*)malloc(sizeof(Node*));
        list[i]->data = data;
    } else {
        Node *t;
        t = list[i];
        list[i] = (Node*)malloc(sizeof(Node*));
        list[i]->data = data;
        list[i]->next = t;
    }
    return list[i];
}
void free_list(Node* head) {
    Node *prev = NULL, *t = head;
    while (t) {
        prev = t;
        t = t->next;
        free(prev);
    }
}
int queue_empty() {
    return q_head == q_tail;
}
int queue_full() {
    return (q_head - 1) % SIZE == q_tail % SIZE;
}
void enqueue(int data) {
    if (queue_full()) {
        printf("full!\n");
        return;
    }
    queue[q_tail++] = data;
    q_tail %= SIZE;
}
int dequeue() {
    if (queue_empty()) {
        printf("empty!\n");
        return -444444;
    }
    int data = queue[q_head++];
    q_head %= SIZE;
    return data;
}
ll* factorize(ll N) {
    ll *res = array(((sqrt(N) + 1) * 2), ll);
    ll i, x = N;
    int count = 0;
    for (i = 2; i * i <= N; i++) {
        ll e = 0;
        while (x % i == 0) {
            x /= i;
            e++;
        }
        if (e > 0) {
            res[count * 2] = i;
            res[count * 2 + 1] = e;
            count++;
        }
    }
    if (x > 1) {
        res[count * 2] = x;
        res[count * 2 + 1] = 1;
    }
    return res;
}
void check_factorization(ll *fact) {
    int i = 1;
    printf("%lld^%lld", fact[0], fact[1]);
    while (fact[i * 2] > 0) {
        printf(" * %lld^%lld", fact[i * 2], fact[i * 2 + 1]);
        i++;
    }
    printf("\n");
}

Score : 500 points

Problem Statement

There are N Snuke Cats numbered 1, 2, \ldots, N, where N is even.

Each Snuke Cat wears a red scarf, on which his favorite non-negative integer is written.

Recently, they learned the operation called xor (exclusive OR).

What is xor?

For n non-negative integers x_1, x_2, \ldots, x_n, their xor, x_1~\textrm{xor}~x_2~\textrm{xor}~\ldots~\textrm{xor}~x_n is defined as follows:

When x_1~\textrm{xor}~x_2~\textrm{xor}~\ldots~\textrm{xor}~x_n is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if the number of integers among x_1, x_2, \ldots, x_n whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even.

For example, 3~\textrm{xor}~5 = 6.

They wanted to use this operation quickly, so each of them calculated the xor of the integers written on their scarfs except his scarf.

We know that the xor calculated by Snuke Cat i, that is, the xor of the integers written on the scarfs except the scarf of Snuke Cat i is a_i.
Using this information, restore the integer written on the scarf of each Snuke Cat.

Constraints

All values in input are integers.

2 \leq N \leq 200000

N is even.

0 \leq a_i \leq 10^9

There exists a combination of integers on the scarfs that is consistent with the given information.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

Print a line containing N integers separated with space.

The i-th of the integers from the left should represent the integer written on the scarf of Snuke Cat i.

If there are multiple possible solutions, you may print any of them.

Sample Input 1

4
20 11 9 24

Sample Output 1

26 5 7 22

5~\textrm{xor}~7~\textrm{xor}~22 = 20

26~\textrm{xor}~7~\textrm{xor}~22 = 11

26~\textrm{xor}~5~\textrm{xor}~22 = 9

26~\textrm{xor}~5~\textrm{xor}~7 = 24

Thus, this output is consistent with the given information.


#include<stdio.h>
int main()
{
	int n;
	scanf("%d", &n);
	int i;
	int a[200005];
	for (i = 0; i < n; i++)
		scanf("%d", &a[i]);
	int x = 0;
	for (i = 0; i < n; i++)
		x ^= a[i];
	for (i = 0; i < n - 1; i++)
		printf("%d ", x ^ a[i]);
	printf("%d\n", x ^ a[n - 1]);
	return 0;
}


Score : 600 points

Problem Statement

We have a grid with A horizontal rows and B vertical columns, with the squares painted white. On this grid, we will repeatedly apply the following operation:

Assume that the grid currently has a horizontal rows and b vertical columns. Choose "vertical" or "horizontal".

If we choose "vertical", insert one row at the top of the grid, resulting in an (a+1) \times b grid.

If we choose "horizontal", insert one column at the right end of the grid, resulting in an a \times (b+1) grid.

Then, paint one of the added squares black, and the other squares white.

Assume the grid eventually has C horizontal rows and D vertical columns. Find the number of ways in which the squares can be painted in the end, modulo 998244353.

Constraints

1 \leq A \leq C \leq 3000

1 \leq B \leq D \leq 3000

A, B, C, and D are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print the number of ways in which the squares can be painted in the end, modulo 998244353.

Sample Input 1

1 1 2 2

Sample Output 1

3

Any two of the three squares other than the bottom-left square can be painted black.

Sample Input 2

2 1 3 4

Sample Output 2

65

Sample Input 3

31 41 59 265

Sample Output 3

387222020


#include<stdio.h>

int main(){
   int x[5];
   for(int i=0;i<5;i++){
      scanf("%d",&x[i]);
   }
   for(int i=0;i<5;i++){
      if(x[i]==0){
         printf("%d\n",i+1);
      }
   }
   return 0;
}

Score : 100 points

Problem Statement

We have five variables x_1, x_2, x_3, x_4, and x_5.

The variable x_i was initially assigned a value of i.

Snuke chose one of these variables and assigned it 0.

You are given the values of the five variables after this assignment.

Find out which variable Snuke assigned 0.

Constraints

The values of x_1, x_2, x_3, x_4, and x_5 given as input are a possible outcome of the assignment by Snuke.

Input

Input is given from Standard Input in the following format:

x_1 x_2 x_3 x_4 x_5

Output

If the variable Snuke assigned 0 was x_i, print the integer i.

Sample Input 1

0 2 3 4 5

Sample Output 1

1

In this case, Snuke assigned 0 to x_1, so we should print 1.

Sample Input 2

1 2 0 4 5

Sample Output 2

3


#include<stdio.h>
int main(void){
int x[5];
for(int i=0;i<5;i++){
scanf("%d",&x[i]);
if(x[i]==0){
printf("%d",i+1);
}
}
return 0;}


#include <stdio.h>

int main(){
    int i, x;
    for (i=0; i<5; i++){
        scanf("%d", &x);
        if (x == 0){
            printf("%d\n", i+1);
            return 0;
        }
    }
    return 0;
}


#include <stdio.h>

int main(void)
{
  long long int x[6];
  scanf("%lld %lld %lld %lld %lld", &x[1], &x[2], &x[3], &x[4], &x[5]);
  for(long long int i=1;i<=5;i++){
    if(x[i]==0){
      printf("%lld\n", i);
    }
  }
  return 0;
}


#include<stdio.h>

int main(void){
   int x,y;

   scanf("%d %d",&x,&y);
   if(y%2!=0){
      printf("NO\n");
      return 0;
   }
   if(x<=y/2&&x>=(y+3)/4){
      printf("YES\n");
      return 0;
   }
   printf("NO\n");
   return 0;
}

Score : 200 points

Problem Statement

There are some animals in a garden. Each of them is a crane with two legs or a turtle with four legs.

Takahashi says: "there are X animals in total in the garden, and they have Y legs in total." Determine whether there is a combination of numbers of cranes and turtles in which this statement is correct.

Constraints

1 \leq X \leq 100

1 \leq Y \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

If there is a combination of numbers of cranes and turtles in which the statement is correct, print Yes; otherwise, print No.

Sample Input 1

3 8

Sample Output 1

Yes

The statement "there are 3 animals in total in the garden, and they have 8 legs in total" is correct if there are two cranes and one turtle. Thus, there is a combination of numbers of cranes and turtles in which the statement is correct.

Sample Input 2

2 100

Sample Output 2

No

There is no combination of numbers of cranes and turtles in which this statement is correct.

Sample Input 3

1 2

Sample Output 3

Yes

We also consider the case in which there are only cranes or only turtles.


#include <stdio.h>

int main(void)
{
	int data1,data2;
  	int c;
  	int i;
	scanf("%d%d",&data1,&data2);	/* 入力部分 */

  	for(i = 0;i < data1;i++){
    if(data2 == (data1 * 2) + (i * 2)){
      c = 1;
    }
    }
  if(c == 1){
	printf("Yes");}
  else printf("No");

	return 0;
}


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

int main() {
    int x, n;
    int p[100];
    int i, j;
    int ans, mans, flag;
    scanf("%d %d", &x, &n);
    for (i=0;i<n;i++) scanf("%d", &p[i]);
    ans=1000;
    mans=1000;
    if (n==0) {
        printf("%d\n", x);
        return 0;
    }
    for (j=x;j<102;j++) {
        flag=1;
        for (i=0;i<n;i++) {
            if(p[i]==j) flag=0;
        }
        if (flag) {
            ans=j;
            break;
        }
    }
    for (j=x;j>-101;j--) {
        flag=1;
        for (i=0;i<n;i++) {
            if(p[i]==j) flag=0;
        }
        if (flag) {
            mans=j;
            break;
        }
    }
    if (abs(x-ans)>abs(x-mans)) {
        printf("%d\n", mans);
    } else if (abs(x-ans)==abs(x-mans)) {
        printf("%d\n", mans);
    } else {
        printf("%d\n", ans);
    }
    return 0;
}

Score : 300 points

Problem Statement

Given are an integer X and an integer sequence of length N: p_1, \ldots, p_N.

Among the integers not contained in the sequence p_1, \ldots, p_N (not necessarily positive), find the integer nearest to X, that is, find the integer whose absolute difference with X is the minimum. If there are multiple such integers, report the smallest such integer.

Constraints

1 \leq X \leq 100

0 \leq N \leq 100

1 \leq p_i \leq 100

p_1, \ldots, p_N are all distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X N
p_1 ... p_N

Output

Print the answer.

Sample Input 1

6 5
4 7 10 6 5

Sample Output 1

8

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the one nearest to 6 is 8.

Sample Input 2

10 5
4 7 10 6 5

Sample Output 2

9

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the ones nearest to 10 are 9 and 11. We should print the smaller one, 9.

Sample Input 3

100 0

Sample Output 3

100

When N = 0, the second line in the input will be empty. Also, as seen here, X itself can be the answer.


#pragma warning(disable:4996)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <float.h>

#pragma region 定義
typedef long long  ll;
typedef unsigned long long  ull;

#define _itoa(A,N) sprintf(A, "%d", N);

int i_dsort(const void* a, const void* b);
int i_asort(const void* a, const void* b);

int  _gcd(int a, int b);
int  _swp(int* a, int* b);
int _cknum(char* a, int n);
int _atoi(char* s, int len);

int s_asort(const void* a, const void* b);
int s_dsort(const void* a, const void* b);

//昇順
int s_asort(const void* a, const void* b) {
	return(strcmp((char*)a, (char*)b));
}
int s_dsort(const void* a, const void* b) {
	return(strcmp((char*)b, (char*)a));
}

int i_dsort(const void* a, const void* b) {
	return(*(int*)b - *(int*)a);
}
int i_asort(const void* a, const void* b) {
	return(*(int*)a - *(int*)b);
}

int _gcd(int a, int b) {
	int r, tmp;
	if (a < b) { tmp = a; a = b; b = tmp; }
	r = a % b;
	while (r) {
		if (!r) break;
		a = b; b = r;
		r = a % b;
	}
	return(b);
}
// puts("Yes\n") puts("No\n")
// scanf("%d.%d") 浮動小数点入力を整数２つで得る

//#define pai  3.1415926535
//#define  _USE_MATH_DEFINES でM_PIが使える

#define deg_rad(X)  (X*(M_PI/180))

int _swp(int* a, int* b)
{
	int tmp;
	tmp = *b; *b = *a; *a = tmp;
	return 0;
}
#define _abs(x) ((x)>0?(x):-(x))
#define _min(x,y) ((x)<(y)?(x):(y))
#define _max(x,y) ((x)<(y)?(y):(x))

int _cknum(char* a, int n) {
	int i;
	char t = '0';

	for (i = 0; i < n; i++) {
		if (a[i] < '0' || a[i]>'9') return 1;
	}
	return 0;
}

int _atoi(char* s, int len) {
	char tmp[20];
	memcpy(tmp, s, len);
	tmp[len] = 0x00;
	return (atoi(tmp));
}

//素因数分解　pf素数が入る　pc 素数の数
ll _pfact(ll a);
static ll pf[100000000];
static ll pc;

ll _pfact(ll a) {
	long i, r;
	if (a % 2 == 0) {
		pf[pc] = 2; pc++;
		return(_pfact(a / 2));
	}
	r = sqrt(a);
	for (i = 3; i <= r; i += 2) {
		if (a % i == 0) {
			pf[pc] = i; pc++;
			return(_pfact(a / i));
		}
	}
	if (a != 1) {
		pf[pc] = a; pc++;
	}
	return 0;

}

#pragma endregion

//ABC 163 -B
static int f[101];
int main(void) {
	int i, x, n, p[101];

	scanf("%d %d", &x, &n);
	for (i = 0; i < n; i++) {
		scanf("%d", &p[i]);
		f[p[i]] = 1;
	}

	int min = 1000,ans,t;

	for (i = 0; i <= 100; i++) {
		if (f[i])continue;
		t=abs(x - i);
		if (t < min) {
			min = t; ans = i;
		}
	}
	(min == 1000) ? printf("101"):printf("%d\n",ans);
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int acs(const void *a, const void *b) {
  return *(int*)a - *(int*)b;
} /* 1,2,3,4.. */
int des(const void *a, const void *b) {
  return *(int*)b - *(int*)a;
} /* 8,7,6,5.. */
int cmp_char(const void *a, const void *b) {
  return *(char*)a - *(char*)b;
} /* a,b,c,d.. */
int cmp_str(const void *a, const void *b) {
  return strcmp(*(const char **)a, *(const char **)b);
} /* aaa,aab.. */
#define min(a,b) (a < b ? a : b)
#define max(a,b) (a > b ? a : b)
#define rep(i, l, r) for (int i = l; i < r; i++)
#define MAX 100001
#define MOD 1000000007
#define INF 1000000009
typedef long long int lli;

int main(void) {
  int x, n, t;
  int p[102] = {0};
  scanf("%d %d", &x, &n);
  rep(i, 0, n) {
    scanf("%d", &t);
    p[t] = 1;
  }

  int min = INF, ans = INF;
  rep(i, 0, 102) {
    if (p[i]) continue;
    if (abs(i - x) > min) continue;
    if (abs(i - x) < min) ans = i;
    else ans = min(i, ans);
    min = abs(i - x);
  }
  printf("%d\n", ans);
  return 0;
}