#include <stdio.h>

int main()
{
	unsigned int n, m;
	scanf("%u %u", &n, &m);
	int i, rec;
	for (i = 1; i <= (m / n); i++) {
		if (m % i == 0) {
			rec = i;
		}
	}
	printf("%d", rec);
	return 0;
}

Score : 400 points

Problem Statement

You are given integers N and M.

Consider a sequence a of length N consisting of positive integers such that a_1 + a_2 + ... + a_N = M. Find the maximum possible value of the greatest common divisor of a_1, a_2, ..., a_N.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

N \leq M \leq 10^9

Input

Input is given from Standard Input in the following format:

N M

Output

Print the maximum possible value of the greatest common divisor of a sequence a_1, a_2, ..., a_N that satisfies the condition.

Sample Input 1

3 14

Sample Output 1

2

Consider the sequence (a_1, a_2, a_3) = (2, 4, 8). Their greatest common divisor is 2, and this is the maximum value.

Sample Input 2

10 123

Sample Output 2

3

Sample Input 3

100000 1000000000

Sample Output 3

10000


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

int main(void)
{
    char s[4];

    scanf("%s",s);

    for (int i = 0; i < 3; i++)
    {
        s[i]=(s[i]=='1')? '9':'1';
    }

    printf("%s",s);

    return 0;
}

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

Score : 100 points

Problem Statement

Cat Snuke is learning to write characters.
Today, he practiced writing digits 1 and 9, but he did it the other way around.

You are given a three-digit integer n written by Snuke.
Print the integer obtained by replacing each digit 1 with 9 and each digit 9 with 1 in n.

Constraints

111 \leq n \leq 999

n is an integer consisting of digits 1 and 9.

Input

Input is given from Standard Input in the following format:

n

Output

Print the integer obtained by replacing each occurrence of 1 with 9 and each occurrence of 9 with 1 in n.

Sample Input 1

119

Sample Output 1

991

Replace the 9 in the ones place with 1, the 1 in the tens place with 9 and the 1 in the hundreds place with 9. The answer is 991.

Sample Input 2

999

Sample Output 2

111

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>

void Do(int n){
  if(n > 10) Do(n / 10);
  if(n % 10 == 1) printf("9");
  else printf("1");
}

int main(void){
  int n;
  scanf("%d", &n);
  Do(n);
  printf("\n");
  return 0;
}


#include<stdio.h>
int main(void){
	int N,x,a,b,c,i;
	scanf("%d", &N);
	x = N;
	for (i = 0; i < 999 - N; i++){

		if (x % 111 == 0){
			break;
		}
		x++;
	}
	printf("%d", x);

		return 0;
}

Score : 200 points

Problem Statement

Kurohashi has never participated in AtCoder Beginner Contest (ABC).

The next ABC to be held is ABC N (the N-th ABC ever held).
Kurohashi wants to make his debut in some ABC x such that all the digits of x in base ten are the same.

What is the earliest ABC where Kurohashi can make his debut?

Constraints

100 \leq N \leq 999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If the earliest ABC where Kurohashi can make his debut is ABC n, print n.

Sample Input 1

111

Sample Output 1

111

The next ABC to be held is ABC 111, where Kurohashi can make his debut.

Sample Input 2

112

Sample Output 2

222

The next ABC to be held is ABC 112, which means Kurohashi can no longer participate in ABC 111.
Among the ABCs where Kurohashi can make his debut, the earliest one is ABC 222.

Sample Input 3

750

Sample Output 3

777


#include<stdio.h>
int main()
{
   int n,x,temp=0,i=0;
   scanf("%d",&n);
   while(n!=0){
    x=n%10;
    if(x>temp){
        temp=x;
    }
    n=n/10;
    i++;
   }
   while(i>0){
    printf("%d",temp);
    i--;
   }
   return 0;

}


#include <stdio.h>
#include<stdlib.h>
int compare(const void *a,const void *b){
    return *(int*)a-*(int*)b;
}
int Min(int a,int b){
    if(a<=b){
        return a;
    }else{
        return b;
    }
}
int main()
{   int ans,nodd,neven,j=0,k=0,i,n,odd[100000],even[100000],oddpr=0,oddse=0,evenpr=0,evense=0,cnt=0;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        if(i%2==0){
            scanf("%d",&odd[j]);
            j++;
        }else{
            scanf("%d",&even[k]);
            k++;
        }
    }
    qsort(odd,j,sizeof(int),compare);
    qsort(even,k,sizeof(int),compare);
    /*for(i=0;i<j;i++){
        printf("%d ",odd[i]);
    }
    for(i=0;i<k;i++){
        printf("%d ",even[i]);
    }
   */
    for(i=0;i<j-1;i++){
        if(odd[i]==odd[i+1]){
            cnt++;
        }else{
            if(cnt>oddpr){
                nodd=odd[i];
                oddse=oddpr;
                oddpr=cnt;
            }else if(cnt>oddse){
                oddse=cnt;
            }
            cnt=0;
        }
    }
    if(cnt>oddpr){
                nodd=odd[i];
                oddse=oddpr;
                oddpr=cnt;
            }else if(cnt>oddse){
                oddse=cnt;
            }
    cnt=0;
    for(i=0;i<k-1;i++){
        if(even[i]==even[i+1]){
            cnt++;
        }else{
            if(cnt>evenpr){
                neven=even[i];
                evense=evenpr;
                evenpr=cnt;
            }else if(cnt>evense){
                evense=cnt;
            }
            cnt=0;
        }
    }
    if(cnt>evenpr){
                neven=even[i];
                evense=evenpr;
                evenpr=cnt;
            }else if(cnt>evense){
                evense=cnt;
            }
    if(neven==nodd&&(evense!=0||oddse!=0)){
        ans=Min(oddpr+evense,evenpr+oddse);
    }else{
        ans=oddpr+evenpr;
    }
    printf("%d",ans);

    return 0;
}

Score : 300 points

Problem Statement

A sequence a_1,a_2,... ,a_n is said to be /\/\/\/ when the following conditions are satisfied:

For each i = 1,2,..., n-2, a_i = a_{i+2}.

Exactly two different numbers appear in the sequence.

You are given a sequence v_1,v_2,...,v_n whose length is even.
We would like to make this sequence /\/\/\/ by replacing some of its elements.
Find the minimum number of elements that needs to be replaced.

Constraints

2 \leq n \leq 10^5

n is even.

1 \leq v_i \leq 10^5

v_i is an integer.

Input

Input is given from Standard Input in the following format:

n
v_1 v_2 ... v_n

Output

Print the minimum number of elements that needs to be replaced.

Sample Input 1

4
3 1 3 2

Sample Output 1

1

The sequence 3,1,3,2 is not /\/\/\/, but we can make it /\/\/\/ by replacing one of its elements: for example, replace the fourth element to make it 3,1,3,1.

Sample Input 2

6
105 119 105 119 105 119

Sample Output 2

0

The sequence 105,119,105,119,105,119 is /\/\/\/.

Sample Input 3

4
1 1 1 1

Sample Output 3

2

The elements of the sequence 1,1,1,1 are all the same, so it is not /\/\/\/.


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

main()
{
  int n,*a,*b,i,maxa=0,maxai=0,maxb=0,maxbi=0,count;
  int maxa2=0,maxai2=0,maxb2=0,maxbi2=0;
  int c[1000000]={};
  scanf("%d",&n);
  a = malloc(sizeof(int)*(n/2));
  b = malloc(sizeof(int)*(n/2));
  for(i=0; i<n/2; i++)
  {
    scanf("%d%d",&a[i],&b[i]);
  }
//aの処理
  for(i=0; i<n/2; i++)
  {
    c[a[i]]++;
    if(maxa<c[a[i]])
    {
      maxa2 = maxa;
      maxai2 = maxai;
      maxa = c[a[i]];
      maxai = a[i];
    }
  }
//bの処理
  for(i=0; i<1000000; i++)
    c[i] = 0;
  for(i=0; i<n/2; i++)
  {
    c[b[i]]++;
    if(maxb<c[b[i]])
    {
      maxb2 = maxb;
      maxbi2 = maxbi;
      maxb = c[b[i]];
      maxbi = b[i];
    }
  }
  count = n - maxa - maxb;
  if(count!=0)
  {
      if(maxai!=maxbi)
        printf("%d\n",count);
      else if(maxa>=maxb)
        printf("%d\n",n-maxa-maxb2);
      else
        printf("%d\n",n-maxa2-maxb);
  }
  else
  {
    if(a[0]==b[0])
      printf("%d\n",n/2);
    else
      printf("0\n");
  }
}


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

int main(void){
    char S[200001];
    char T[200001];
    memset(&S,'\0',sizeof(S));
    memset(&T,'\0',sizeof(T));
    scanf("%s",S);
    scanf("%s",T);

    int size = strlen(S);

    char alpha[26];
    memset(&alpha,'\0',sizeof(alpha));
    for(int i = 0;i<size;i++){
        // printf("alpha %c\n",alpha[T[i] - 'a']);
        // printf("alalpha %c\n",alpha[alpha[T[i] - 'a'] - 'a']);
        if(alpha[T[i] - 'a'] == '\0'){
            if(S[i] != T[i]){
                alpha[T[i] - 'a'] = S[i];
            }
        }else if(alpha[T[i] - 'a'] != S[i]){
            printf("No\n");
            return 0;
        }else{
            continue;
        }
    }
    // printf("S %s\n",S);
    // printf("T %s\n",T);
    printf("Yes\n");
    return 0;
}

Score : 300 points

Problem Statement

You are given strings S and T consisting of lowercase English letters.

You can perform the following operation on S any number of times:

Operation: Choose two distinct lowercase English letters c_1 and c_2, then replace every occurrence of c_1 with c_2, and every occurrence of c_2 with c_1.

Determine if S and T can be made equal by performing the operation zero or more times.

Constraints

1 \leq |S| \leq 2 \times 10^5

|S| = |T|

S and T consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

If S and T can be made equal, print Yes; otherwise, print No.

Sample Input 1

azzel
apple

Sample Output 1

Yes

azzel can be changed to apple, as follows:

Choose e as c_1 and l as c_2. azzel becomes azzle.

Choose z as c_1 and p as c_2. azzle becomes apple.

Sample Input 2

chokudai
redcoder

Sample Output 2

No

No sequences of operation can change chokudai to redcoder.

Sample Input 3

abcdefghijklmnopqrstuvwxyz
ibyhqfrekavclxjstdwgpzmonu

Sample Output 3

Yes


#include <stdio.h>

int main()
{
	int i, N, x, a[101];
	scanf("%d %d", &N, &x);
	for (i = 1; i <= N; i++) scanf("%d", &(a[i]));

	int count = 0, min, argmin, flag[101] = {};
	while (1) {
		min = 1000000001;
		argmin = 0;
		for (i = 1; i <= N; i++) {
			if (flag[i] == 0 && a[i] < min) {
				min = a[i];
				argmin = i;
			}
		}
		if (x < min) break;
		else if (argmin == 0) {
			if (x > 0) count--;
			break;
		} else {
			x -= min;
			flag[argmin] = 1;
			count++;
		}
	}

	printf("%d\n", count);
	fflush(stdout);
	return 0;
}

Score : 200 points

Problem Statement

There are N children, numbered 1, 2, ..., N.

Snuke has decided to distribute x sweets among them.
He needs to give out all the x sweets, but some of the children may get zero sweets.

For each i (1 \leq i \leq N), Child i will be happy if he/she gets exactly a_i sweets.
Snuke is trying to maximize the number of happy children by optimally distributing the sweets.
Find the maximum possible number of happy children.

Constraints

All values in input are integers.

2 \leq N \leq 100

1 \leq x \leq 10^9

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N x
a_1 a_2 ... a_N

Output

Print the maximum possible number of happy children.

Sample Input 1

3 70
20 30 10

Sample Output 1

2

One optimal way to distribute sweets is (20, 30, 20).

Sample Input 2

3 10
20 30 10

Sample Output 2

1

The optimal way to distribute sweets is (0, 0, 10).

Sample Input 3

4 1111
1 10 100 1000

Sample Output 3

4

The optimal way to distribute sweets is (1, 10, 100, 1000).

Sample Input 4

2 10
20 20

Sample Output 4

0

No children will be happy, no matter how the sweets are distributed.


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

int asc(const void *x, const void *y) {
	const int* a=(const int*)x;
	const int* b=(const int*)y;
	if(*a>*b)return 1;
	if(*a<*b)return -1;
	return 0;
}

int main(){

    int kids;
    int candy_max;
    int array[128] = {0};
    int happy = 0;
    int pointer = 0;
    int num;

    scanf("%d %d\n",&kids,&candy_max);
    int candy = candy_max;

    for(int i=0; i<kids; i++){
        scanf("%d",&array[i]);
    }

    //sort
    qsort( array, kids, sizeof(array[0]), asc);

    //degug
    //printf("degug:");
    //for(int i=0; i<kids; i++){
    //    printf("%d ", array[i]);
    //}
    //printf("\n");

    //candyを小さいほうから割り当てる
    for(int i=0; i < kids; i++){
        candy -= array[i];
        if(candy == 0){
            happy++;
            printf("%d",happy);
            return 0;
        }
        if(candy <= 0){
            printf("%d",happy);
            return 0;
        }
        happy++;
    }

    if(candy > 0 && candy != candy_max){
        happy--;
        printf("%d",happy);
        return 0;
    }

    printf("0");
    return 0;
}


#include <stdio.h>

int main(void) {
	int a,b,c,d;
	scanf("%d",&a);
	if(a%2){
		b=a/2;
		c=a/2+1;
	}else{
		b=a/2;
		c=a/2;
	}
	d=b*c;
	printf("%d",d);
	return 0;
}

Score : 100 points

Problem Statement

Find the number of ways to choose a pair of an even number and an odd number from the positive integers between 1 and K (inclusive). The order does not matter.

Constraints

2\leq K\leq 100

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the number of ways to choose a pair of an even number and an odd number from the positive integers between 1 and K (inclusive).

Sample Input 1

3

Sample Output 1

2

Two pairs can be chosen: (2,1) and (2,3).

Sample Input 2

6

Sample Output 2

9

Sample Input 3

11

Sample Output 3

30

Sample Input 4

50

Sample Output 4

625


#include <stdio.h>

int main(void){
    int n, ans;
    scanf("%d", &n);
    ans = n*n/4;
    printf("%d\n", ans);

    return 0;
}


#include <stdio.h>

int main(){
    //読み取り
    int a;
    int ans;
    scanf("%d",&a);
    if(a%2==0){
        ans = (a/2)*(a/2);
        printf("%d",ans);
    }else{
        ans = ((a+1)/2) * ((a-1)/2);
        printf("%d",ans);
    }
    return 0;
}


#include<stdio.h>

int main(void){
int a;
scanf("%d\n",a);
printf("%d",(a/2)*((a+1)/2));
}


/*  ex7-1
    toshiwaka */

//point構造体
typedef struct point{
    int x_elem;
    int y_elem;
}point;

//ref_pointからx,y軸方向にshift_x,shift_yだけそれぞれ移動
//移動後の点の座標をshifted_pointのメンバに代入
void shift_point(point *shifted_point, point *ref_point, int shift_x, int shift_y);

#include <stdio.h>

int main(void) {
  //point:正方形の頂点
  //diff:2点間のx,y軸方向の距離
  point point[4], diff;

  //入力
  scanf("%d %d %d %d", &point[0].x_elem, &point[0].y_elem, &point[1].x_elem, &point[1].y_elem);

  //2点間のx,y軸方向の距離
  diff.x_elem = point[1].x_elem - point[0].x_elem;
  diff.y_elem = point[1].y_elem - point[0].y_elem;

  //残りの２点を算出
  shift_point(&point[2], &point[1], -1 * diff.y_elem, diff.x_elem);
  shift_point(&point[3], &point[0], -1 * diff.y_elem, diff.x_elem);

  //出力
  printf("%d %d %d %d\n", point[2].x_elem, point[2].y_elem, point[3].x_elem, point[3].y_elem);

  return 0;
}

//ref_pointからx,y軸方向にshift_x,shift_yだけそれぞれ移動
//移動後の点の座標をshifted_pointのメンバに代入
void shift_point(point *shifted_point, point *ref_point, int shift_x, int shift_y){
  shifted_point->x_elem = ref_point->x_elem + shift_x;
  shifted_point->y_elem = ref_point->y_elem + shift_y;
};

Score : 200 points

Problem Statement

There is a square in the xy-plane. The coordinates of its four vertices are (x_1,y_1),(x_2,y_2),(x_3,y_3) and (x_4,y_4) in counter-clockwise order.
(Assume that the positive x-axis points right, and the positive y-axis points up.)

Takahashi remembers (x_1,y_1) and (x_2,y_2), but he has forgot (x_3,y_3) and (x_4,y_4).

Given x_1,x_2,y_1,y_2, restore x_3,y_3,x_4,y_4. It can be shown that x_3,y_3,x_4 and y_4 uniquely exist and have integer values.

Constraints

|x_1|,|y_1|,|x_2|,|y_2| \leq 100

(x_1,y_1) ≠ (x_2,y_2)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

x_1 y_1 x_2 y_2

Output

Print x_3,y_3,x_4 and y_4 as integers, in this order.

Sample Input 1

0 0 0 1

Sample Output 1

-1 1 -1 0

(0,0),(0,1),(-1,1),(-1,0) is the four vertices of a square in counter-clockwise order.
Note that (x_3,y_3)=(1,1),(x_4,y_4)=(1,0) is not accepted, as the vertices are in clockwise order.

Sample Input 2

2 3 6 6

Sample Output 2

3 10 -1 7

Sample Input 3

31 -41 -59 26

Sample Output 3

-126 -64 -36 -131


#include<stdio.h>
#include<string.h>
int main(void)
{
  long int l;
  long int n,m;
  long int p[20], r[60];
  long int i;
  scanf("%ld",&l);
  n = 2*l;  m = 3*l-2;
  printf("%ld %ld\n",n,m);
  for(i=1;i<l;i++){
    printf("%ld %ld %ld\n",i,i+1,0);
    printf("%ld %ld %ld\n",i+4,i+5,1);
  }
  for(i=0;i<l;i++){
    printf("%ld %ld %ld\n",i+1,i+5,0);
  }
  return 0;
}

Score : 700 points

Problem Statement

You are given an integer L. Construct a directed graph that satisfies the conditions below. The graph may contain multiple edges between the same pair of vertices. It can be proved that such a graph always exists.

The number of vertices, N, is at most 20. The vertices are given ID numbers from 1 to N.

The number of edges, M, is at most 60. Each edge has an integer length between 0 and 10^6 (inclusive).

Every edge is directed from the vertex with the smaller ID to the vertex with the larger ID. That is, 1,2,...,N is one possible topological order of the vertices.

There are exactly L different paths from Vertex 1 to Vertex N. The lengths of these paths are all different, and they are integers between 0 and L-1.

Here, the length of a path is the sum of the lengths of the edges contained in that path, and two paths are considered different when the sets of the edges contained in those paths are different.

Constraints

2 \leq L \leq 10^6

L is an integer.

Input

Input is given from Standard Input in the following format:

L

Output

In the first line, print N and M, the number of the vertices and edges in your graph.
In the i-th of the following M lines, print three integers u_i,v_i and w_i, representing the starting vertex, the ending vertex and the length of the i-th edge.
If there are multiple solutions, any of them will be accepted.

Sample Input 1

4

Sample Output 1

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

In the graph represented by the sample output, there are four paths from Vertex 1 to N=8:

1 → 2 → 3 → 4 → 8 with length 0

1 → 2 → 3 → 7 → 8 with length 1

1 → 2 → 6 → 7 → 8 with length 2

1 → 5 → 6 → 7 → 8 with length 3

There are other possible solutions.

Sample Input 2

5

Sample Output 2

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


#include <stdio.h>
#include <math.h>
int main(){
  int n,k,buf,buf2;
  scanf("%d%d",&n,&k);
  buf=n/k;//n%k==0
  buf=pow(buf,3);
  if(k%2==0){
    buf2=((2*n)/k)-(n/k);
    buf2=pow(buf2,3);
  }
  printf("%d",buf+buf2);
}

Score : 300 points

Problem Statement

You are given integers N and K. Find the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K.
The order of a,b,c does matter, and some of them can be the same.

Constraints

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

N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K.

Sample Input 1

3 2

Sample Output 1

9

(1,1,1),(1,1,3),(1,3,1),(1,3,3),(2,2,2),(3,1,1),(3,1,3),(3,3,1) and (3,3,3) satisfy the condition.

Sample Input 2

5 3

Sample Output 2

1

Sample Input 3

31415 9265

Sample Output 3

27

Sample Input 4

35897 932

Sample Output 4

114191


#include <stdio.h>
int main(void){
	int a,b;
	scanf("%d %d",&a,&b);
	printf("%d",(a-b)+1);
	return 0;
}

Score : 100 points

Problem Statement

There is an N-car train.

You are given an integer i. Find the value of j such that the following statement is true: "the i-th car from the front of the train is the j-th car from the back."

Constraints

1 \leq N \leq 100

1 \leq i \leq N

Input

Input is given from Standard Input in the following format:

N i

Output

Print the answer.

Sample Input 1

4 2

Sample Output 1

3

The second car from the front of a 4-car train is the third car from the back.

Sample Input 2

1 1

Sample Output 2

1

Sample Input 3

15 11

Sample Output 3

5


/* ex7-3
   Sagzar1993  */

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

int main(void) {
    int row, col, i, j;//行と列の数

    /*  各行・各列が全て白であるか否かを記憶する配列
        各行、各列について全てのマス目が白なら１、そうでなければ０ */
    int *ptrWhiteRow, *ptrWhiteCol;

    char **ptrColor; //各マスの記号の配列
    scanf("%d %d", &row, &col); //行数、列数の入力

    //各配列のポインタについて、メモリを動的確保
    ptrColor = (char**)calloc(row, sizeof(char*));
    ptrWhiteRow = (int*)calloc(row, sizeof(int));
    ptrWhiteCol = (int*)calloc(row, sizeof(int));
    //メモリの確保に失敗した場合はエラー終了
    if(ptrColor == NULL || ptrWhiteRow == NULL || ptrWhiteCol == NULL){
        exit(1);
    }
    //各行の要素分のメモリを確保
    for(i = 0; i < row; i++){
        ptrColor[i] = (char*)calloc(col, sizeof(char));
        //メモリの確保に失敗した場合はエラー終了
        if(ptrColor[i] == NULL){
            exit(1);
        }
    }

    for(i = 0; i < row; i++){
        for(j = 0; j < col; j++){
            scanf(" %c", &ptrColor[i][j]); //各マス目の色記号の入力
        }
    }

    for(i = 0; i < row; i++){
        for(j = 0; j < col; j++){
            if(ptrColor[i][j] != '.'){
                break; //各行について、白でないマス目があれば終了して次の行へ
            }
        }
        if(j == col){
            ptrWhiteRow[i] = 1; //行の全てのマス目が白ならwhiteRowに1を保存
        }
    }

    for(j = 0; j < col; j++){
        for(i = 0; i < row; i++){
            if(ptrColor[i][j] != '.'){
                break; //各列について、白でないマス目があれば終了して次の行へ
            }
        }
        if(i == row){
            ptrWhiteCol[j] = 1; //列の全てのマス目が白ならwhiteColに1を保存
        }
    }

    for(i = 0; i < row; i++){
        if(ptrWhiteRow[i] == 1){
            continue;  //各行について、whiteRowを参照し、その行の全てのマス目が白ならスキップ
        }

        for(j = 0; j < col; j++){
            if(ptrWhiteCol[j] == 1){
                continue; //各行の各列のマス目について、その列が全てのマス目が白である列ならスキップ
            }
            printf("%c", ptrColor[i][j]); //そのマス目の行と列がともに全て白でないときは出力
        }
        printf("\n"); //各行について、全てのマス目を調べ終えたら改行
    }

    //ptrColorの各行の要素分のメモリを解放
    for(i = 0; i < row; i++){
        free(ptrColor[i]);
    }
    //ptrColorの各行の配列と他の２つの配列へのポインタの領域を解放
    free(ptrColor);
    free(ptrWhiteRow);
    free(ptrWhiteCol);

    return 0;
}

Score : 200 points

Problem Statement

There is a grid of squares with H horizontal rows and W vertical columns.
The square at the i-th row from the top and the j-th column from the left is represented as (i, j).
Each square is black or white.
The color of the square is given as an H-by-W matrix (a_{i, j}).
If a_{i, j} is ., the square (i, j) is white; if a_{i, j} is #, the square (i, j) is black.

Snuke is compressing this grid.
He will do so by repeatedly performing the following operation while there is a row or column that consists only of white squares:

Operation: choose any one row or column that consists only of white squares, remove it and delete the space between the rows or columns.

It can be shown that the final state of the grid is uniquely determined regardless of what row or column is chosen in each operation.
Find the final state of the grid.

Constraints

1 \leq H, W \leq 100

a_{i, j} is . or #.

There is at least one black square in the whole grid.

Input

Input is given from Standard Input in the following format:

H W
a_{1, 1}...a_{1, W}
:
a_{H, 1}...a_{H, W}

Output

Print the final state of the grid in the same format as input (without the numbers of rows and columns); see the samples for clarity.

Sample Input 1

4 4
##.#
....
##.#
.#.#

Sample Output 1

###
###
.##

The second row and the third column in the original grid will be removed.

Sample Input 2

3 3
#..
.#.
..#

Sample Output 2

#..
.#.
..#

As there is no row or column that consists only of white squares, no operation will be performed.

Sample Input 3

4 5
.....
.....
..#..
.....

Sample Output 3

#

Sample Input 4

7 6
......
....#.
.#....
..#...
..#...
......
.#..#.

Sample Output 4

..#
#..
.#.
.#.
#.#


/* ex5_3
   UMAboogie */
#include<stdio.h>

int main(void){
  int height,width;
  scanf("%d %d",&height,&width);
  char grid[height][width];
  int row_exist[height];
  int col_exist[width];
  for(int i = 0;i < height;i++){
    for(int j = 0;j < width;j++){
      scanf(" %c",&grid[i][j]);
      if(grid[i][j] == '#'){
        row_exist[i]=1;
        col_exist[j]=1;
      }
    }
  }
  for(int i = 0;i < height;i++){
      for(int j;j < width;j++){
        if(row_exist[i] == 1 && col_exist[j] == 1){
          printf("%c",grid[i][j]);
        }
      }
      printf("\n");
    }
  return 0;
}


#include<stdio.h>

int main(void){
    int a, b;

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

    printf("%d\n",a*b-(a+b-1));

}

Score: 100 points

Problem Statement

There is a farm whose length and width are A yard and B yard, respectively. A farmer, John, made a vertical road and a horizontal road inside the farm from one border to another, as shown below: (The gray part represents the roads.)

What is the area of this yard excluding the roads? Find it.

Note

It can be proved that the positions of the roads do not affect the area.

Constraints

A is an integer between 2 and 100 (inclusive).

B is an integer between 2 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

A B

Output

Print the area of this yard excluding the roads (in square yards).

Sample Input 1

2 2

Sample Output 1

1

In this case, the area is 1 square yard.

Sample Input 2

5 7

Sample Output 2

24

In this case, the area is 24 square yards.


#include <stdio.h>

int main(void)
{
    int n,s=0,t,i,j;
    scanf("%d",&n);
    for(i=1;i<=n;i+=2){
        t=0;
        for(j=1;j<=i;j++)if(i%j==0)t++;
        if(t==8)s++;
    }
    printf("%d",s);
    return 0;
}

Score: 200 points

Problem Statement

The number 105 is quite special - it is odd but still it has eight divisors.
Now, your task is this: how many odd numbers with exactly eight positive divisors are there between 1 and N (inclusive)?

Constraints

N is an integer between 1 and 200 (inclusive).

Input

Input is given from Standard Input in the following format:

N

Output

Print the count.

Sample Input 1

105

Sample Output 1

1

Among the numbers between 1 and 105, the only number that is odd and has exactly eight divisors is 105.

Sample Input 2

7

Sample Output 2

0

1 has one divisor. 3, 5 and 7 are all prime and have two divisors. Thus, there is no number that satisfies the condition.


#include <stdio.h>

int main(void){
  int i, j, k;
  int N, M, Q;
  int left, right;
  scanf("%d %d %d", &N, &M, &Q);

  int table[501][501] = {{0}};

  for(i = 0; i < M; i++){
    scanf("%d %d", &left, &right);
    table[left][right]++;
  }

  for(i = 0; i < Q; i++){
    scanf("%d %d", &left, &right);
    int count = 0;
    for(j = left; j <= right; j++)
      for(k = left; k <= right; k++)
       count += table[j][k];
    printf("%d\n", count);
  }

  return 0;
}

Score: 400 points

Problem Statement

In Takahashi Kingdom, there is a east-west railroad and N cities along it, numbered 1, 2, 3, ..., N from west to east.
A company called AtCoder Express possesses M trains, and the train i runs from City L_i to City R_i (it is possible that L_i = R_i).
Takahashi the king is interested in the following Q matters:

The number of the trains that runs strictly within the section from City p_i to City q_i, that is, the number of trains j such that p_i \leq L_j and R_j \leq q_i.

Although he is genius, this is too much data to process by himself. Find the answer for each of these Q queries to help him.

Constraints

N is an integer between 1 and 500 (inclusive).

M is an integer between 1 and 200 \ 000 (inclusive).

Q is an integer between 1 and 100 \ 000 (inclusive).

1 \leq L_i \leq R_i \leq N (1 \leq i \leq M)

1 \leq p_i \leq q_i \leq N (1 \leq i \leq Q)

Input

Input is given from Standard Input in the following format:

N M Q
L_1 R_1
L_2 R_2
:
L_M R_M
p_1 q_1
p_2 q_2
:
p_Q q_Q

Output

Print Q lines. The i-th line should contain the number of the trains that runs strictly within the section from City p_i to City q_i.

Sample Input 1

2 3 1
1 1
1 2
2 2
1 2

Sample Output 1

3

As all the trains runs within the section from City 1 to City 2, the answer to the only query is 3.

Sample Input 2

10 3 2
1 5
2 8
7 10
1 7
3 10

Sample Output 2

1
1

The first query is on the section from City 1 to 7. There is only one train that runs strictly within that section: Train 1.
The second query is on the section from City 3 to 10. There is only one train that runs strictly within that section: Train 3.

Sample Input 3

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

Sample Output 3

7
9
10
6
8
9
6
7
8
10


#include<stdio.h>
int main(void)
{
  int n,k;

  scanf("%d %d",&n,&k);
  int a=n/k;
  int b=n%k;
  if(b>=1){
    printf("%d\n",a+1);
  }
  else{
    printf("%d\n",a);
  }
  return 0;
}

Score : 100 points

Problem Statement

Takahashi has decided to distribute N AtCoder Crackers to K users of as evenly as possible.
When all the crackers are distributed, find the minimum possible (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user.

Constraints

1 \leq N,K \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user.

Sample Input 1

7 3

Sample Output 1

1

When the users receive two, two and three crackers, respectively, the (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user, is 1.

Sample Input 2

100 10

Sample Output 2

0

The crackers can be distributed evenly.

Sample Input 3

1 1

Sample Output 3

0


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

typedef long long ll;

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

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

int main(){

  int n;
  scanf("%d",&n);
  for(int i = 0; i < 26; ++i){
    for(int j = 0; j < 16; ++j){
      if( i*4 + 7*j == n ){
        printf("Yes");
        return 0;
      }
    }
  }

  printf("No");

  return 0;
}

Score : 200 points

Problem Statement

La Confiserie d'ABC sells cakes at 4 dollars each and doughnuts at 7 dollars each.
Determine if there is a way to buy some of them for exactly N dollars. You can buy two or more doughnuts and two or more cakes, and you can also choose to buy zero doughnuts or zero cakes.

Constraints

N is an integer between 1 and 100, inclusive.

Input

Input is given from Standard Input in the following format:

N

Output

If there is a way to buy some cakes and some doughnuts for exactly N dollars, print Yes; otherwise, print No.

Sample Input 1

11

Sample Output 1

Yes

If you buy one cake and one doughnut, the total will be 4 + 7 = 11 dollars.

Sample Input 2

40

Sample Output 2

Yes

If you buy ten cakes, the total will be 4 \times 10 = 40 dollars.

Sample Input 3

3

Sample Output 3

No

The prices of cakes (4 dollars) and doughnuts (7 dollars) are both higher than 3 dollars, so there is no such way.


#include <stdio.h>

int main(void){
  int N;

  scanf("%d",&N);
  for(int i = 0;7*i <= N;i++){
    for(int j = 0;7*i+4*j <= N;j++){
      if(7*i+j*4 == N){
        printf("Yes\n");
        return 0;
      }
    }
  }

  printf("No\n");
  return 0;
}


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

int main(void){
  int N;

  scanf("%d",&N);

  for(int i = 0; i <= N; i = i + 4){
    for(int j = 0; i + j <= N; j = j + 7){
      if(N == i+j){
        printf("Yes");
        return 0;
      }
    }
  }
  printf("No");
  return 0;
}


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

int main(void){
	long N;
	scanf("%d",&N);
	int a[64],i;

	if(N!=0){

	for(i=0;i<64;i++){
		a[i]=abs(N%2);
		N-=a[i];
		N/=-2;
	}

	int check=0;

	for(i=64;i>=0;i--){

		if(a[i]!=0){
			check++;
		}

		if(check>0){
			printf("%d",a[i]);
		}
	}
	}
	else{
		printf("0");
	}
	return 0;
}

Score : 300 points

Problem Statement

Given an integer N, find the base -2 representation of N.

Here, S is the base -2 representation of N when the following are all satisfied:

S is a string consisting of 0 and 1.

Unless S = 0, the initial character of S is 1.

Let S = S_k S_{k-1} ... S_0, then S_0 \times (-2)^0 + S_1 \times (-2)^1 + ... + S_k \times (-2)^k = N.

It can be proved that, for any integer M, the base -2 representation of M is uniquely determined.

Constraints

Every value in input is integer.

-10^9 \leq N \leq 10^9

Input

Input is given from Standard Input in the following format:

N

Output

Print the base -2 representation of N.

Sample Input 1

-9

Sample Output 1

1011

As (-2)^0 + (-2)^1 + (-2)^3 = 1 + (-2) + (-8) = -9, 1011 is the base -2 representation of -9.

Sample Input 2

123456789

Sample Output 2

11000101011001101110100010101

Sample Input 3

0

Sample Output 3

0


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

if (r < 1200)

    printf("ABC");

else if (1200 <= r &&  r< 2800)

    printf("ARC");

else{
        if (2800<=r && r <= 4208);

    printf("AGC");
}
    return 0;
}

Score : 100 points

Problem Statement

A programming competition site AtCode regularly holds programming contests.

The next contest on AtCode is called ABC, which is rated for contestants with ratings less than 1200.

The contest after the ABC is called ARC, which is rated for contestants with ratings less than 2800.

The contest after the ARC is called AGC, which is rated for all contestants.

Takahashi's rating on AtCode is R. What is the next contest rated for him?

Constraints

0 ≤ R ≤ 4208

R is an integer.

Input

Input is given from Standard Input in the following format:

R

Output

Print the name of the next contest rated for Takahashi (ABC, ARC or AGC).

Sample Input 1

1199

Sample Output 1

ABC

1199 is less than 1200, so ABC will be rated.

Sample Input 2

1200

Sample Output 2

ARC

1200 is not less than 1200 and ABC will be unrated, but it is less than 2800 and ARC will be rated.

Sample Input 3

4208

Sample Output 3

AGC


#include <stdio.h>

int main(void)
{
  int R;
  scanf("%d", &R);
  if(R < 1200){
    printf("ABC\n");
  } else if(R < 2800){
    printf("ARC\n");
  } else {
    printf("AGC\n");
  }
  return 0;
}


#include <stdio.h>

int main(void)
{
	int i,A[3],max,min,ans;

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

	max = A[0];
	min = A[2];

	for(i = 0; i < 3; i++)
	{
		if(A[i] > max){
			max = A[i];
		}else if(A[i] < min){
			min = A[i];
		}
	}

	ans = max - min;
	printf("%d",ans);

	return 0;
}

Score : 100 points

Problem Statement

You have three tasks, all of which need to be completed.

First, you can complete any one task at cost 0.

Then, just after completing the i-th task, you can complete the j-th task at cost |A_j - A_i|.

Here, |x| denotes the absolute value of x.

Find the minimum total cost required to complete all the task.

Constraints

All values in input are integers.

1 \leq A_1, A_2, A_3 \leq 100

Input

Input is given from Standard Input in the following format:

A_1 A_2 A_3

Output

Print the minimum total cost required to complete all the task.

Sample Input 1

1 6 3

Sample Output 1

5

When the tasks are completed in the following order, the total cost will be 5, which is the minimum:

Complete the first task at cost 0.

Complete the third task at cost 2.

Complete the second task at cost 3.

Sample Input 2

11 5 5

Sample Output 2

6

Sample Input 3

100 100 100

Sample Output 3

0


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

int main(void)
{
 char s[100],t[100];
 int a,b,i,n;

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

 n=strlen(s);

 a=s[0];
 b=t[0];

 for(i=1;i<n;i++){
   a=s[i]+a;
   b=t[i]+b;
 }

 if(a==b) printf("Yes\n");
 else printf("No\n");

 return 0;
}

Score : 200 points

Problem Statement

You are given string S and T consisting of lowercase English letters.

Determine if S equals T after rotation.

That is, determine if S equals T after the following operation is performed some number of times:

Operation: Let S = S_1 S_2 ... S_{|S|}. Change S to S_{|S|} S_1 S_2 ... S_{|S|-1}.

Here, |X| denotes the length of the string X.

Constraints

2 \leq |S| \leq 100

|S| = |T|

S and T consist of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

If S equals T after rotation, print Yes; if it does not, print No.

Sample Input 1

kyoto
tokyo

Sample Output 1

Yes

In the first operation, kyoto becomes okyot.

In the second operation, okyot becomes tokyo.

Sample Input 2

abc
arc

Sample Output 2

No

abc does not equal arc after any number of operations.

Sample Input 3

aaaaaaaaaaaaaaab
aaaaaaaaaaaaaaab

Sample Output 3

Yes


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

int main(){

  char str[2][105]={{0}};
  scanf("%s",str[0]);
  scanf("%s",str[1]);

  int i;int f=0;
  int j;
  int size=0;
  while(str[0][size] !=0){
    size++;
  }
  for(i=0;i<size;++i){
    int j;
    if(memcmp(str[0],str[1],sizeof(str[0]))==0){
      f=1;
      break;
    }else{
      char temp=str[0][size-1];
      for(j=size-1;j>0;j--){
        str[0][j]=str[0][j-1];
      }
      str[0][0]=temp;
    }
  }
  if(f==0)puts("No");
  if(f==1)puts("Yes");
  return 0;
}


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

int main(){

  char str[2][100]={{0}};int st;
  st=scanf("%s",str[0]);
  st=scanf("%s",str[1]);

  int i;int f=0;int d=0;
  int j;
  int size=0;
  while(str[0][size] !=0){
    size++;
  }
//  printf("%d\n",size);
  for(i=0;i<size+1;i++){
    int j;
    if(memcmp(str[0],str[1],sizeof(str[0]))==0)f=1;
    if(f==1){
      break;
    }
    char temp=str[0][size-1];
    for(j=size-1;j>0;j--){
      str[0][j]=str[0][j-1];
    }
    str[0][0]=temp;
//    printf("%s\n",str[0]);
  }
  if(f==0)puts("No");
  if(f==1)puts("Yes");
  return 0;
}


#include<stdio.h>

int chksame(char *S,char *T,int len){
    for(int i=0;i<len;i++){
        if(S[i]!=T[i]){

            return -1;
        }
    }
    return 1;
}

int main(void){
    char S[100]={};
    char T[100]={};
    scanf("%s",S);
    scanf("%s",T);
    int len=0;
    while(S[len]!='\0'){
        len++;
    }
    for(int i=0;i<len;i++){
        char temp = S[0];
        for(int j=0;j<len-1;j++){
            S[j]=S[j+1];
        }
        S[len-1]=temp;
        if(chksame(S,T,len)==1){
            printf("Yes\n");
            return 0;
        }
    }
    printf("No\n");
    return 0;
}


#include <stdio.h>
int main ()
{
	long long int n,a[3001],i,sum;
	scanf("%lld",&n);
	for(i=0;i<n;i++)
	{
		scanf("%lld",&a[i]);
	}
	for(i=0;i<n;i++)
	{
		sum=sum+a[i];
	}
	printf("%lld\n",sum-n);
	return 0;
}

Score : 300 points

Problem Statement

You are given N positive integers a_1, a_2, ..., a_N.

For a non-negative integer m, let f(m) = (m\ mod\ a_1) + (m\ mod\ a_2) + ... + (m\ mod\ a_N).

Here, X\ mod\ Y denotes the remainder of the division of X by Y.

Find the maximum value of f.

Constraints

All values in input are integers.

2 \leq N \leq 3000

2 \leq a_i \leq 10^5

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the maximum value of f.

Sample Input 1

3
3 4 6

Sample Output 1

10

f(11) = (11\ mod\ 3) + (11\ mod\ 4) + (11\ mod\ 6) = 10 is the maximum value of f.

Sample Input 2

5
7 46 11 20 11

Sample Output 2

90

Sample Input 3

7
994 518 941 851 647 2 581

Sample Output 3

4527


#include<stdio.h>
int gcd(int a, int b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
int lcm( int a, int b)
{
    return a*b/(gcd(a,b));
}
int main()
{
    int n,i;
    scanf("%d",&n);
    long long int a[n],sum=0,l;
    for(i=0;i<n;i++)
        scanf("%lld",&a[i]);
    l=a[0];
    for(i=0;i<n;i++)
        l=lcm(l,a[i]);
    l=l-1;
    for(i=0;i<n;i++)
        sum+=l%a[i];
    printf("%lld",sum);
}


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define F(i,a,n) for(int i=a; i<n; i++)

int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    int a[m],b[m],tmp_a,tmp_b,sum[n-1];
    F(k,0,n-1){
        sum[k]=0;
    }

    F(i,0,m){
        scanf("%d%d",a+i,b+i);
    }

    int iter=1;
    while(iter){
        iter=0;
        F(i,0,m-1){
            if(b[i]>b[i+1]){
                tmp_b=b[i];
                b[i]=b[i+1];
                b[i+1]=tmp_b;
                tmp_a=a[i];
                a[i]=a[i+1];
                a[i+1]=tmp_a;
                iter=1;
            }
        }
    }

    int rem_num=0;
    F(j,0,m){
        if(b[j] != 0){
            F(k,j+1,m){
                if(a[k]<b[j]){
                    a[k]=0;
                    b[k]=0;
                }
            }
            rem_num++;
        }
    }
    printf("%d\n",rem_num);
    return 0;
}

Score : 400 points

Problem Statement

There are N islands lining up from west to east, connected by N-1 bridges.

The i-th bridge connects the i-th island from the west and the (i+1)-th island from the west.

One day, disputes took place between some islands, and there were M requests from the inhabitants of the islands:

Request i: A dispute took place between the a_i-th island from the west and the b_i-th island from the west. Please make traveling between these islands with bridges impossible.

You decided to remove some bridges to meet all these M requests.

Find the minimum number of bridges that must be removed.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq a_i < b_i \leq N

All pairs (a_i, b_i) are distinct.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

Print the minimum number of bridges that must be removed.

Sample Input 1

5 2
1 4
2 5

Sample Output 1

1

The requests can be met by removing the bridge connecting the second and third islands from the west.

Sample Input 2

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

Sample Output 2

2

Sample Input 3

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

Sample Output 3

4


#include <stdio.h>

int main(void) {
	int i, n, a, b = -1, cnt = 0, ans = 0;

	scanf("%d", &n);

	for (i = 0; i < n; i++) {
		scanf("%d", &a);
		if (b == a) {
			cnt++;
		} else {
			ans += (cnt+1) / 2;
			cnt = 0;
		}
		b = a;
	}

	ans += (cnt+1) / 2;

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

	return 0;
}

Score : 200 points

Problem Statement

Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000.

Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i.
If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic.

Takahashi can change the color of one slime to any of the 10000 colors by one spell.
How many spells are required so that no slimes will start to combine themselves?

Constraints

2 \leq N \leq 100

1 \leq a_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of spells required.

Sample Input 1

5
1 1 2 2 2

Sample Output 1

2

For example, if we change the color of the second slime from the left to 4, and the color of the fourth slime to 5, the colors of the slimes will be 1, 4, 2, 5, 2, which satisfy the condition.

Sample Input 2

3
1 2 1

Sample Output 2

0

Although the colors of the first and third slimes are the same, they are not adjacent, so no spell is required.

Sample Input 3

5
1 1 1 1 1

Sample Output 3

2

For example, if we change the colors of the second and fourth slimes from the left to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the condition.

Sample Input 4

14
1 2 2 3 3 3 4 4 4 4 1 2 3 4

Sample Output 4

4


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

int compare_long(const void *p, const void *q){
    return *(long*)p - *(long*)q;
}

int main(){
    int  i,N;
    int  flg;
    int  count=0;
    long A,B,C,D;
    long AB[1000000] = {};
    long noon,night;
    long *ptr;

    scanf("%d", &N);
    for(i=0;i<N;i++){
        scanf("%ld %ld %ld %ld", &A,&B,&C,&D);
        night = A;
        count = 0;
        memset(AB,0,sizeof(AB));

        // 同じA-Bか出現するかA-Bがマイナスになるまでループ
        while(1){
            noon = night-B;
            // マイナスになったらNo終了
            if(noon < 0) {
               flg = 1;
               break;
            }

            // 過去出たリストに同値があるか走査
            ptr = (long*)bsearch(&noon,AB,count,sizeof(long),compare_long);
            // 過去に出てない？
            if( ptr == NULL ){
                // 過去出たリストに追加してソート
                AB[count++] = noon;
                qsort(AB, count, sizeof(long), compare_long);

                // C以下ならD本補充
                if(noon<=C){
                    night = noon + D;
                }
                else{
                    night = noon;
                }
            }
            // 過去と同じA-Bが出現したらYes終了
            else{
                flg=0;
                break;
            }
        }

        if(flg==0){
            printf("Yes\n");
        }
        else{
            printf("No\n");
        }
    }

    return 0;
}

Score : 600 points

Problem Statement

Ringo Mart, a convenience store, sells apple juice.

On the opening day of Ringo Mart, there were A cans of juice in stock in the morning.
Snuke buys B cans of juice here every day in the daytime.
Then, the manager checks the number of cans of juice remaining in stock every night.
If there are C or less cans, D new cans will be added to the stock by the next morning.

Determine if Snuke can buy juice indefinitely, that is, there is always B or more cans of juice in stock when he attempts to buy them.
Nobody besides Snuke buy juice at this store.

Note that each test case in this problem consists of T queries.

Constraints

1 \leq T \leq 300

1 \leq A, B, C, D \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

T
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
:
A_T B_T C_T D_T

In the i-th query, A = A_i, B = B_i, C = C_i, D = D_i.

Output

Print T lines. The i-th line should contain Yes if Snuke can buy apple juice indefinitely in the i-th query; No otherwise.

Sample Input 1

14
9 7 5 9
9 7 6 9
14 10 7 12
14 10 8 12
14 10 9 12
14 10 7 11
14 10 8 11
14 10 9 11
9 10 5 10
10 10 5 10
11 10 5 10
16 10 5 10
1000000000000000000 17 14 999999999999999985
1000000000000000000 17 15 999999999999999985

Sample Output 1

No
Yes
No
Yes
Yes
No
No
Yes
No
Yes
Yes
No
No
Yes

In the first query, the number of cans of juice in stock changes as follows: (D represents daytime and N represents night.)

9
→D 2
→N 11
→D 4
→N 13
→D 6
→N 6
→D x

In the second query, the number of cans of juice in stock changes as follows:

9
→D 2
→N 11
→D 4
→N 13
→D 6
→N 15
→D 8
→N 8
→D 1
→N 10
→D 3
→N 12
→D 5
→N 14
→D 7
→N 7
→D 0
→N 9
→D 2
→N 11
→D …

and so on, thus Snuke can buy juice indefinitely.

Sample Input 2

24
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1

Sample Output 2

No
No
No
No
No
No
Yes
Yes
No
No
No
No
Yes
Yes
Yes
No
No
No
Yes
Yes
Yes
No
No
No


#include <stdio.h>
int main()
{
    int num1,num2;
    scanf("%d%d",&num1,&num2);
    if(num1+num2==15)
        printf("+\n");
    else if(num1*num2==15)
        printf("*\n");
    else
        printf("x\n");
    return 0;
}

Score : 100 points

Problem Statement

You are given two integers a and b.
Determine if a+b=15 or a\times b=15 or neither holds.

Note that a+b=15 and a\times b=15 do not hold at the same time.

Constraints

1 \leq a,b \leq 15

All values in input are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

If a+b=15, print +;
if a\times b=15, print *;
if neither holds, print x.

Sample Input 1

4 11

Sample Output 1

+

4+11=15.

Sample Input 2

3 5

Sample Output 2

*

3\times 5=15.

Sample Input 3

1 1

Sample Output 3

x

1+1=2 and 1\times 1=1, neither of which is 15.


#include <stdio.h>

int main(){
	int a,b;
	scanf("%d%d",&a,&b);
	if(a+b==15)
	printf("+\n");
	else if(a*b==15)
	printf("*\n");
	else
	printf("×\n");
	return 0;
}


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

int main()

{
      char str[1001];
      scanf("%s",str);
      int n, i;
      scanf("%d",&n);
      for(i=0; i<strlen(str);i=i+n)
             {
                   printf("%s",str[i]);
             }
printf("\n");
return 0;
}

Score : 200 points

Problem Statement

You are given a string S consisting of lowercase English letters.
We will write down this string, starting a new line after every w letters. Print the string obtained by concatenating the letters at the beginnings of these lines from top to bottom.

Constraints

1 \leq w \leq |S| \leq 1000

S consists of lowercase English letters.

w is an integer.

Input

Input is given from Standard Input in the following format:

S
w

Output

Print the desired string in one line.

Sample Input 1

abcdefgh
3

Sample Output 1

adg

When we write down abcdefgh, starting a new line after every three letters, we get the following:

abc

def

gh

Concatenating the letters at the beginnings of these lines, we obtain adg.

Sample Input 2

lllll
1

Sample Output 2

lllll

Sample Input 3

souuundhound
2

Sample Output 3

suudon


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

int main(void){
    long long n,m,s,t;
    scanf("%lld%lld%lld%lld",&n,&m,&s,&t);
    s--;
    t--;
    long long u[m],v[m],a[m],b[m];
    for (int i=0; i<m; i++){
        scanf("%lld%lld%lld%lld",&u[i],&v[i],&a[i],&b[i]);
        u[i]--;
        v[i]--;
    }
    long long y[m],k[m];
    bool visited[n];
    for (int i=0; i<n; i++){
        y[i]=LLONG_MAX;
        visited[i]=false;
    }
    y[s]=0;
    for (int i=0; i<n; i++){
        long long tmp=LLONG_MAX,next;
        for (int l=0; l<n; l++){
            if (tmp>y[l] && visited[l]==false){
                tmp=y[l];
                next=l;
            }
        }
        visited[next]=true;
        for (int l=0; l<m; l++){
            if (u[l]==next){
                if (y[v[l]]>y[next]+a[l]){
                    y[v[l]]=y[next]+a[l];
                }
            }
            if (v[l]==next){
                if (y[u[l]]>y[next]+a[l]){
                    y[u[l]]=y[next]+a[l];
                }
            }
        }
    }
    for (int i=0; i<n; i++){
        k[i]=LLONG_MAX;
        visited[i]=false;
    }
    k[t]=0;
    for (int i=0; i<n; i++){
        long long tmp=LLONG_MAX,next;
        for (int l=0; l<n; l++){
            if (tmp>k[l] && visited[l]==false){
                tmp=k[l];
                next=l;
            }
        }
        visited[next]=true;
        for (int l=0; l<m; l++){
            if (u[l]==next){
                if (k[v[l]]>k[next]+b[l]){
                    k[v[l]]=k[next]+b[l];
                }
            }
            if (v[l]==next){
                if (k[u[l]]>k[next]+b[l]){
                    k[u[l]]=k[next]+b[l];
                }
            }
        }
    }
    long long ans[n];
    for (int i=0; i<n; i++){
        ans[i]=y[i]+k[i];
    }
    for (int i=n-1; i>0; i--){
        if (ans[i-1]>ans[i]){
            ans[i-1]=ans[i];
        }
    }
    for (int i=0; i<n; i++){
        printf("%lld\n",1000000000000000-ans[i]);
    }
	return 0 ;
}

Score : 400 points

Problem Statement

Kenkoooo is planning a trip in Republic of Snuke.
In this country, there are n cities and m trains running.
The cities are numbered 1 through n, and the i-th train connects City u_i and v_i bidirectionally.
Any city can be reached from any city by changing trains.

Two currencies are used in the country: yen and snuuk.
Any train fare can be paid by both yen and snuuk.
The fare of the i-th train is a_i yen if paid in yen, and b_i snuuk if paid in snuuk.

In a city with a money exchange office, you can change 1 yen into 1 snuuk.
However, when you do a money exchange, you have to change all your yen into snuuk.
That is, if Kenkoooo does a money exchange when he has X yen, he will then have X snuuk.
Currently, there is a money exchange office in every city, but the office in City i will shut down in i years and can never be used in and after that year.

Kenkoooo is planning to depart City s with 10^{15} yen in his pocket and head for City t, and change his yen into snuuk in some city while traveling.
It is acceptable to do the exchange in City s or City t.

Kenkoooo would like to have as much snuuk as possible when he reaches City t by making the optimal choices for the route to travel and the city to do the exchange.
For each i=0,...,n-1, find the maximum amount of snuuk that Kenkoooo has when he reaches City t if he goes on a trip from City s to City t after i years.
You can assume that the trip finishes within the year.

Constraints

2 \leq n \leq 10^5

1 \leq m \leq 10^5

1 \leq s,t \leq n

s \neq t

1 \leq u_i < v_i \leq n

1 \leq a_i,b_i \leq 10^9

If i\neq j, then u_i \neq u_j or v_i \neq v_j.

Any city can be reached from any city by changing trains.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

n m s t
u_1 v_1 a_1 b_1
:
u_m v_m a_m b_m

Output

Print n lines.
In the i-th line, print the maximum amount of snuuk that Kenkoooo has when he reaches City t if he goes on a trip from City s to City t after i-1 years.

Sample Input 1

4 3 2 3
1 4 1 100
1 2 1 10
1 3 20 1

Sample Output 1

999999999999998
999999999999989
999999999999979
999999999999897

After 0 years, it is optimal to do the exchange in City 1.

After 1 years, it is optimal to do the exchange in City 2.

Note that City 1 can still be visited even after the exchange office is closed.
Also note that, if it was allowed to keep 1 yen when do the exchange in City 2 and change the remaining yen into snuuk, we could reach City 3 with 999999999999998 snuuk, but this is NOT allowed.

After 2 years, it is optimal to do the exchange in City 3.

After 3 years, it is optimal to do the exchange in City 4.
Note that the same train can be used multiple times.

Sample Input 2

8 12 3 8
2 8 685087149 857180777
6 7 298270585 209942236
2 4 346080035 234079976
2 5 131857300 22507157
4 8 30723332 173476334
2 6 480845267 448565596
1 4 181424400 548830121
4 5 57429995 195056405
7 8 160277628 479932440
1 6 475692952 203530153
3 5 336869679 160714712
2 7 389775999 199123879

Sample Output 2

999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994


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

	if(a%2==0) printf("%d\n",a);
	else printf("%d\n",2*a);

	return 0;
}

Score : 100 points

Problem Statement

You are given a positive integer N.
Find the minimum positive integer divisible by both 2 and N.

Constraints

1 \leq N \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum positive integer divisible by both 2 and N.

Sample Input 1

3

Sample Output 1

6

6 is divisible by both 2 and 3.
Also, there is no positive integer less than 6 that is divisible by both 2 and 3.
Thus, the answer is 6.

Sample Input 2

10

Sample Output 2

10

Sample Input 3

999999999

Sample Output 3

1999999998


#include<stdio.h>

int main(void)
{
 int max,min,n;
 int tmp,i,s[100];

 scanf("%d",&n);

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

 for(i=0;i<n;i++){
   if(max<s[i]) max=s[i];
 else if(min>s[i]) max=s[i];
 }

 tmp=max-min;
 printf("%d\n",tmp);

 return 0;
}

Score : 200 points

Problem Statement

You are given an integer sequence A of length N.
Find the maximum absolute difference of two elements (with different indices) in A.

Constraints

2 \leq N \leq 100

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 A_2 ... A_N

Output

Print the maximum absolute difference of two elements (with different indices) in A.

Sample Input 1

4
1 4 6 3

Sample Output 1

5

The maximum absolute difference of two elements is A_3-A_1=6-1=5.

Sample Input 2

2
1000000000 1

Sample Output 2

999999999

Sample Input 3

5
1 1 1 1 1

Sample Output 3

0


#include <stdio.h>
#include <stdint.h>

int main(void){
  int n, i, sum=0, b, sad=0, tmp;
  int l[200000];
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%d",&(l[i]));
    l[i]-=(i+1);
    sum+=l[i];
  }
  printf("%d\n", sum);
  b=(sum-n)/n;
  for(i=0;i<n;i++){
    tmp=((l[i]-b)>0)?l[i]-b:b-l[i];
    printf("%d ", tmp);
    sad+=tmp;
  }
  printf("\n%d\n", sad);
}

Score : 300 points

Problem Statement

Snuke has an integer sequence A of length N.

He will freely choose an integer b.
Here, he will get sad if A_i and b+i are far from each other.
More specifically, the sadness of Snuke is calculated as follows:

abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))

Here, abs(x) is a function that returns the absolute value of x.

Find the minimum possible sadness of Snuke.

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 A_2 ... A_N

Output

Print the minimum possible sadness of Snuke.

Sample Input 1

5
2 2 3 5 5

Sample Output 1

2

If we choose b=0, the sadness of Snuke would be abs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2.
Any choice of b does not make the sadness of Snuke less than 2, so the answer is 2.

Sample Input 2

9
1 2 3 4 5 6 7 8 9

Sample Output 2

0

Sample Input 3

6
6 5 4 3 2 1

Sample Output 3

18

Sample Input 4

7
1 1 1 1 2 3 4

Sample Output 4

6


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
  int i,c=0;
  char s[5];
  scanf("%s",s );
  for(i=0;i<4;i++){
    if(s[i]=='+')
    c++;
    else
    c--;
  }
  printf("%d",c );
}

Score : 100 points

Problem Statement

There is always an integer in Takahashi's mind.

Initially, the integer in Takahashi's mind is 0. Takahashi is now going to eat four symbols, each of which is + or -. When he eats +, the integer in his mind increases by 1; when he eats -, the integer in his mind decreases by 1.

The symbols Takahashi is going to eat are given to you as a string S. The i-th character in S is the i-th symbol for him to eat.

Find the integer in Takahashi's mind after he eats all the symbols.

Constraints

The length of S is 4.

Each character in S is + or -.

Input

Input is given from Standard Input in the following format:

S

Output

Print the integer in Takahashi's mind after he eats all the symbols.

Sample Input 1

+-++

Sample Output 1

2

Initially, the integer in Takahashi's mind is 0.

The first integer for him to eat is +. After eating it, the integer in his mind becomes 1.

The second integer to eat is -. After eating it, the integer in his mind becomes 0.

The third integer to eat is +. After eating it, the integer in his mind becomes 1.

The fourth integer to eat is +. After eating it, the integer in his mind becomes 2.

Thus, the integer in Takahashi's mind after he eats all the symbols is 2.

Sample Input 2

-+--

Sample Output 2

-2

Sample Input 3

----

Sample Output 3

-4


#include<stdio.h>

int main(void)
{
  int ans=0,i;
  char s[5];
  scanf("%s",s);
  for(i=0;i<4;i++)
    {
      if(s[i] == '-')
	{
	  ans--;
	}
      else
	{
	  ans++;
	}
    }
  printf("%d\n",ans);
  return 0;
}


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

int main(void){
    int ct = 0;
    char a[5] = {'\0'};
    int i;

    scanf("%s", a);

    for( i =0 ; a[i] != '\0'; i++){
        if(a[i] == '+'){
            ct++;
        }else{
            ct--;
        }
    }

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


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

int main(void)
{
    int i, total = 0, x, y;
    int n;
    scanf("%d", &n);

    x = n;
    while (x / 10 > 0)
    {
        y = x % 10;
        x = x / 10;
        total += y;
    }

    total += x;

    if (n % total == 0)
    {
        printf("Yes\n");
    }
    else
    {
        printf("No\n");
    }
    return 0;
}

Score : 200 points

Problem Statement

Let S(n) denote the sum of the digits in the decimal notation of n.
For example, S(101) = 1 + 0 + 1 = 2.

Given an integer N, determine if S(N) divides N.

Constraints

1 \leq N \leq 10^9

Input

Input is given from Standard Input in the following format:

N

Output

If S(N) divides N, print Yes; if it does not, print No.

Sample Input 1

12

Sample Output 1

Yes

In this input, N=12.
As S(12) = 1 + 2 = 3, S(N) divides N.

Sample Input 2

101

Sample Output 2

No

As S(101) = 1 + 0 + 1 = 2, S(N) does not divide N.

Sample Input 3

999999999

Sample Output 3

Yes


#include<stdio.h>
int main(void){
  int n,k,ans=1;
  scanf("%d %d",&n,&k);
  while (n>ans*(k-1)+1){
    ans++;
  }
  printf(ans);
  return 0;
}

Score : 300 points

Problem Statement

There is a sequence of length N: A_1, A_2, ..., A_N. Initially, this sequence is a permutation of 1, 2, ..., N.

On this sequence, Snuke can perform the following operation:

Choose K consecutive elements in the sequence. Then, replace the value of each chosen element with the minimum value among the chosen elements.

Snuke would like to make all the elements in this sequence equal by repeating the operation above some number of times.
Find the minimum number of operations required.
It can be proved that, Under the constraints of this problem, this objective is always achievable.

Constraints

2 \leq K \leq N \leq 100000

A_1, A_2, ..., A_N is a permutation of 1, 2, ..., N.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

Print the minimum number of operations required.

Sample Input 1

4 3
2 3 1 4

Sample Output 1

2

One optimal strategy is as follows:

In the first operation, choose the first, second and third elements. The sequence A becomes 1, 1, 1, 4.

In the second operation, choose the second, third and fourth elements. The sequence A becomes 1, 1, 1, 1.

Sample Input 2

3 3
1 2 3

Sample Output 2

1

Sample Input 3

8 3
7 3 1 8 4 6 2 5

Sample Output 3

4


#include<stdio.h>
int main()
{
    int a,b,ck;
    scanf("%d %d",&a,&b);
    if((a<=8 && b<=8) &&(a+b<=16))
        printf("Yay!\n");
    else
        printf(":(\n");

    return 0;
}

Score: 100 points

Problem Statement

E869120's and square1001's 16-th birthday is coming soon.

Takahashi from AtCoder Kingdom gave them a round cake cut into 16 equal fan-shaped pieces.

E869120 and square1001 were just about to eat A and B of those pieces, respectively,

when they found a note attached to the cake saying that "the same person should not take two adjacent pieces of cake".

Can both of them obey the instruction in the note and take desired numbers of pieces of cake?

Constraints

A and B are integers between 1 and 16 (inclusive).

A+B is at most 16.

Input

Input is given from Standard Input in the following format:

A B

Output

If both E869120 and square1001 can obey the instruction in the note and take desired numbers of pieces of cake, print Yay!; otherwise, print :(.

Sample Input 1

5 4

Sample Output 1

Yay!

Both of them can take desired number of pieces as follows:

Sample Input 2

8 8

Sample Output 2

Yay!

Both of them can take desired number of pieces as follows:

Sample Input 3

11 4

Sample Output 3

:(

In this case, there is no way for them to take desired number of pieces, unfortunately.


#include<stdio.h>

int main(){
    int A, B, ans;
    scanf("%d %d", &A, &B);
    if(A <= 8 && B <= 8){
        printf("Yay!");
    }
    else{
        printf(":(");
    }
    return 0;
}


#include<stdio.h>

int main(void)
{
   int a,b;

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

   if(a<=8 && b<=8)
     printf("Yay!\n");
   else
     printf(":(\n");

   return 0;
}


#include<stdio.h>

int main(){
		int a,b;
		scanf("%d%d",&a,&b);
		int sa=b-a-1;
		int sowa=(sa+1)*sa/2;
		printf("%d\n",sowa-a);

		return 0;
}

Score : 200 points

Problem Statement

In some village, there are 999 towers that are 1,(1+2),(1+2+3),...,(1+2+3+...+999) meters high from west to east, at intervals of 1 meter.

It had been snowing for a while before it finally stopped. For some two adjacent towers located 1 meter apart, we measured the lengths of the parts of those towers that are not covered with snow, and the results are a meters for the west tower, and b meters for the east tower.

Assuming that the depth of snow cover and the altitude are the same everywhere in the village, find the amount of the snow cover.

Assume also that the depth of the snow cover is always at least 1 meter.

Constraints

1 \leq a < b < 499500(=1+2+3+...+999)

All values in input are integers.

There is no input that contradicts the assumption.

Input

Input is given from Standard Input in the following format:

a b

Output

If the depth of the snow cover is x meters, print x as an integer.

Sample Input 1

8 13

Sample Output 1

2

The heights of the two towers are 10 meters and 15 meters, respectively.
Thus, we can see that the depth of the snow cover is 2 meters.

Sample Input 2

54 65

Sample Output 2

1


#include <stdio.h>

#define Nmax 110000
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
int memo[Nmax];

int dp(int n) {
    if (memo[n] != -1) return memo[n];
    if (n == 0) return 0;
    int ans = 100000000;
    for (int n6 = 1; n6 <= n; n6 *= 6) {
        ans = MIN(ans, 1 + dp(n - n6));
    }
    for (int n9 = 1; n9 <= n; n9 *= 9) {
        ans = MIN(ans, 1 + dp(n - n9));
    }
    return memo[n] = ans;
}

int main() {
    int N;
    scanf("%d",&N);
    for (int i=0; i<Nmax; i++) memo[i] = -1;
    printf("%d", dp(N));
}

Score : 300 points

Problem Statement

To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation:

1 yen (the currency of Japan)

6 yen, 6^2(=36) yen, 6^3(=216) yen, ...

9 yen, 9^2(=81) yen, 9^3(=729) yen, ...

At least how many operations are required to withdraw exactly N yen in total?

It is not allowed to re-deposit the money you withdrew.

Constraints

1 \leq N \leq 100000

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If at least x operations are required to withdraw exactly N yen in total, print x.

Sample Input 1

127

Sample Output 1

4

By withdrawing 1 yen, 9 yen, 36(=6^2) yen and 81(=9^2) yen, we can withdraw 127 yen in four operations.

Sample Input 2

3

Sample Output 2

3

By withdrawing 1 yen three times, we can withdraw 3 yen in three operations.

Sample Input 3

44852

Sample Output 3

16


#include<stdio.h>

int main(){
    int n,ans=0, i,b;
    scanf("%d", &n);
    for(i=0;i<6;i++){
        b = n%10;
        n = n/10;
        ans += b;
    }
    printf("%d\n", ans);
    return 0;
}

Score : 200 points

Problem Statement

Takahashi has two positive integers A and B.

It is known that A plus B equals N.
Find the minimum possible value of "the sum of the digits of A" plus "the sum of the digits of B" (in base 10).

Constraints

2 ≤ N ≤ 10^5

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum possible value of "the sum of the digits of A" plus "the sum of the digits of B".

Sample Input 1

15

Sample Output 1

6

When A=2 and B=13, the sums of their digits are 2 and 4, which minimizes the value in question.

Sample Input 2

100000

Sample Output 2

10


#include<stdio.h>
int main(){
 int n;
  scanf("%d",&n);
  int c=0;
  if(n%10==0){
    printf("%d",10);
    return 0;
  }
  while(n!=0){
   c+=n%10;
    n/=10;
  }
 printf("%d",c);
  return 0;
}


#include <stdio.h>

int digit_sum(int x) {
    int sum=0;
    while(x>0) {
        sum += x%10;
        x /= 10;
    }
    return sum;
}

int main(void) {
    int n, a, b, k, i, j,l, cnt=0;
    scanf("%d %d %d %d", &n, &a, &b, &k);

    for(i=0; i<=n; i++) {
        for(j=0; j<=n; j++) {
            for(l=0; l<=n; l++) {
                if(a*i+b*j+(a+b)*l==k)
                    cnt++;
            }
        }
    }
    printf("%d\n", cnt);
    return 0;
}

Score : 700 points

Problem Statement

Takahashi has a tower which is divided into N layers.
Initially, all the layers are uncolored. Takahashi is going to paint some of the layers in red, green or blue to make a beautiful tower.
He defines the beauty of the tower as follows:

The beauty of the tower is the sum of the scores of the N layers, where the score of a layer is A if the layer is painted red, A+B if the layer is painted green, B if the layer is painted blue, and 0 if the layer is uncolored.

Here, A and B are positive integer constants given beforehand. Also note that a layer may not be painted in two or more colors.

Takahashi is planning to paint the tower so that the beauty of the tower becomes exactly K.
How many such ways are there to paint the tower? Find the count modulo 998244353.
Two ways to paint the tower are considered different when there exists a layer that is painted in different colors, or a layer that is painted in some color in one of the ways and not in the other.

Constraints

1 ≤ N ≤ 3×10^5

1 ≤ A,B ≤ 3×10^5

0 ≤ K ≤ 18×10^{10}

All values in the input are integers.

Input

Input is given from Standard Input in the following format:

N A B K

Output

Print the number of the ways to paint tiles, modulo 998244353.

Sample Input 1

4 1 2 5

Sample Output 1

40

In this case, a red layer worth 1 points, a green layer worth 3 points and the blue layer worth 2 points. The beauty of the tower is 5 when we have one of the following sets of painted layers:

1 green, 1 blue

1 red, 2 blues

2 reds, 1 green

3 reds, 1 blue

The total number of the ways to produce them is 40.

Sample Input 2

2 5 6 0

Sample Output 2

1

The beauty of the tower is 0 only when all the layers are uncolored. Thus, the answer is 1.

Sample Input 3

90081 33447 90629 6391049189

Sample Output 3

577742975


Score : 800 points

Problem Statement

Takahashi is doing a research on sets of points in a plane.
Takahashi thinks a set S of points in a coordinate plane is a good set when S satisfies both of the following conditions:

The distance between any two points in S is not \sqrt{D_1}.

The distance between any two points in S is not \sqrt{D_2}.

Here, D_1 and D_2 are positive integer constants that Takahashi specified.

Let X be a set of points (i,j) on a coordinate plane where i and j are integers and satisfy 0 ≤ i,j < 2N.

Takahashi has proved that, for any choice of D_1 and D_2, there exists a way to choose N^2 points from X so that the chosen points form a good set.
However, he does not know the specific way to choose such points to form a good set.
Find a subset of X whose size is N^2 that forms a good set.

Constraints

1 ≤ N ≤ 300

1 ≤ D_1 ≤ 2×10^5

1 ≤ D_2 ≤ 2×10^5

All values in the input are integers.

Input

Input is given from Standard Input in the following format:

N D_1 D_2

Output

Print N^2 distinct points that satisfy the condition in the following format:

x_1 y_1
x_2 y_2
:
x_{N^2} y_{N^2}

Here, (x_i,y_i) represents the i-th chosen point.
0 ≤ x_i,y_i < 2N must hold, and they must be integers.
The chosen points may be printed in any order.
In case there are multiple possible solutions, you can output any.

Sample Input 1

2 1 2

Sample Output 1

0 0
0 2
2 0
2 2

Among these points, the distance between 2 points is either 2 or 2\sqrt{2}, thus it satisfies the condition.

Sample Input 2

3 1 5

Sample Output 2

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


#include<stdio.h>

int main()
{
 	int a,b,c;
	scanf("%d%d%d",a,b,c);
	int max=a;
	if(a<b){
		max=b;
	}
	if(b<c){
		max=c;
 	}
	printf("%d\n",10*max+a+b+c-max);
	return 0;

}

Score : 100 points

Problem Statement

You are given two integers A and B.
Find the largest value among A+B, A-B and A \times B.

Constraints

-1000 \leq A,B \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the largest value among A+B, A-B and A \times B.

Sample Input 1

3 1

Sample Output 1

4

3+1=4, 3-1=2 and 3 \times 1=3. The largest among them is 4.

Sample Input 2

4 -2

Sample Output 2

6

The largest is 4 - (-2) = 6.

Sample Input 3

0 0

Sample Output 3

0


#include<stdio.h>
int main()
{
    int A,B,x,y,z;

    scanf("%d %d",&A,&B);

    x=A+B;
    y=A-B;
    z=A*B;

    if(x>y && x>z)
    {
        printf("%d\n",x);
        printf("%d + %d = %d,%d - %d = %d,%d * %d = %d The largest is %d.\n",A,B,x,A,B,y,A,B,z,x);

    }
    else if(y>x && y>z)
    {
        printf("%d\n",y);
        printf(" The largest is %d - (%d) = %d.\n",A,B,y);
    }

    else
    {

        printf("%d\n",z);
    }

    return 0;
}


#include<stdio.h>

int main(){
  int a,b,c;
  scanf("%d%d",&a,&b);
  if((a+b)>(a-b)){
   c=a+b;
  }else{
  c=a-b;

  }
  if(c>(a*b)){
    printf("%d",c);
    return 0;
  }else{
	printf("%d",a*b);
  }

 return 0;
}


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

int main(){
  int len;
  char s[100];
  int c;
  scanf("%d", &len);
  int p;
  char ls[100];
  char rs[100];
  char matchs[100];
  char ts[2];

  p = 1;
  int lp, rp;
  int i,j;

  int maxc = 0;
  lp = 1; // ひだりの長さ
  while(lp < len){
  	c = 0;

    for(i = 0; i < 100; i++)
    matchs[i] = '\0';

    strcpy(rs, &s[lp]);

    for(j = 0; j < lp; j++){
      ts[0] = s[j];
      ts[1] = '\0';
    	if((strstr(rs,ts) != NULL) && (strstr(matchs, ts) == NULL)){ // 左の文字列があるとき

      	strcat(matchs, ts);
      	c++;

   		}

    }
    if(maxc < c)
        maxc = c;
  lp++;
}

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

Score : 200 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.
We will cut this string at one position into two strings X and Y.
Here, we would like to maximize the number of different letters contained in both X and Y.
Find the largest possible number of different letters contained in both X and Y when we cut the string at the optimal position.

Constraints

2 \leq N \leq 100

|S| = N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the largest possible number of different letters contained in both X and Y.

Sample Input 1

6
aabbca

Sample Output 1

2

If we cut the string between the third and fourth letters into X = aab and Y = bca, the letters contained in both X and Y are a and b.
There will never be three or more different letters contained in both X and Y, so the answer is 2.

Sample Input 2

10
aaaaaaaaaa

Sample Output 2

1

However we divide S, only a will be contained in both X and Y.

Sample Input 3

45
tgxgdqkyjzhyputjjtllptdfxocrylqfqjynmfbfucbir

Sample Output 3

9


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

int check(char str2[], char str[]);
void move(char str3[]);

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

	char str[n+1];
	scanf("%s", str);

	int i;
	int c_max;
	int max;
	char str2[50] = {'\0'};
	char str3[50] = {'\0'};
	str2[0] = str[0];
	for(i=0;i<n;i++){
		str3[i] = str[i+1];
	}
	max = check(str2, str3);
	for(i=1;i<=(n-1)/2;i++){
		str2[i] = str[i];
		move(str3);
		c_max = check(str2, str3);
		if(max <= c_max){
			max = c_max;
		}
	}
	printf("%d", max);
	return 0;
}

int check(char str2[], char str[]){
	int i;
	int j;
	int cnt = 0;
	int flag = 0;
	for(i=0;str2[i]!='\0';i++){
		for(j=0;j<i;j++){
			if(str2[j]==str2[i]){
				flag=1;
				break;
			}
		}
		if(flag==0){
			if(strchr(str, str2[i]) != NULL){
				cnt+=1;
			}
		}
		flag = 0;
	}
	return cnt;
}

void move(char str3[]){
	int i;
	for(i=1;str3[i]!='\0';i++){
		str3[i-1] = str3[i];
	}
}


#if 0
cat <<EOF >mistaken-paste
#endif
// thx Ebi-chan!

#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
#define _USE_MATH_DEFINES

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

#define BIG 2000000007
#define VERYBIG 20000000000000007LL

#define MOD 1000000007
#define FOD  998244353
typedef uint64_t ull;
typedef  int64_t sll;

#define N_MAX 1048576

#ifdef __cplusplus
#include <queue>
#include <stack>
#include <tuple>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <functional>
#include <array>

using std::queue;
using std::priority_queue;
using std::stack;
using std::tuple;
using std::set;
using std::map;
using std::vector;
using std::greater;
using std::pair;
using std::string;
using std::get;

template <typename T, typename U>
pair<T, U> operator+(pair<T, U> l, pair<T, U> r) {
	return pair<T, U>(l.first + r.first, l.second + r.second);
}

#endif

#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950
#endif

typedef struct {
	sll a;
	sll b;
} hwll;

typedef struct {
	sll a;
	sll b;
	sll c;
} hwllc;

typedef struct {
	hwll a;
	hwll b;
} linell;

typedef struct {
	double a;
	double b;
} hwreal;

sll n, m;
sll h, w;
sll k;
sll q;
sll va, vb, vc, vd, ve, vf;
ull ua, ub, uc, ud, ue, uf;
long double vra, vrb, vrc;
double vda, vdb, vdc;
char ch, dh;

ull umin (ull x, ull y) {
	return (x < y) ? x : y;
}

ull umax (ull x, ull y) {
	return (x > y) ? x : y;
}

sll smin (sll x, sll y) {
	return (x < y) ? x : y;
}

sll smax (sll x, sll y) {
	return (x > y) ? x : y;
}

ull gcd (ull x, ull y) {
	if (y == 0) {
		return x;
	} else {
		return gcd(y, x % y);
	}
}

ull bitpow (ull a, ull x, ull modulo) {
	ull result = 1;
	while (x) {
		if (x & 1) {
			result *= a;
			result %= modulo;
		}
		x /= 2;
		a = (a * a) % modulo;
	}
	return result;
}

ull divide (ull a, ull b, ull modulo) {
	return (a * bitpow(b, modulo - 2, modulo)) % modulo;
}

ull udiff (ull a, ull b) {
	if (a >= b) {
		return a - b;
	} else {
		return b - a;
	}
}

sll sdiff (sll a, sll b) {
	if (a >= b) {
		return a - b;
	} else {
		return b - a;
	}
}

int bitcount (ull n) {
	int result = 0;
	while (n) {
		if (n & 1) result++;
		n /= 2;
	}
	return result;
}

#define BEGCMP(NAME) int32_t NAME (const void *left, const void *right)
#define DEFLR(TYPE) TYPE l=*(TYPE*)left,r=*(TYPE*)right
#define CMPRET(L, R) if((L)<(R))return-1;if((L)>(R))return+1

// int32_t pullcomp (const void *left, const void *right) {
// 	ull l = *(ull*)left;
// 	ull r = *(ull*)right;
// 	if (l < r) {
// 		return -1;
// 	}
// 	if (l > r) {
// 		return +1;
// 	}
// 	return 0;
// }
BEGCMP(pullcomp){
	DEFLR(ull);
	CMPRET(l, r);
	return 0;
}
BEGCMP(prevcomp){
	DEFLR(sll);
	CMPRET(r, l);
	return 0;
}
BEGCMP(psllcomp){
	DEFLR(sll);
	CMPRET(l, r);
	return 0;
}
BEGCMP(pcharcomp){
	DEFLR(char);
	CMPRET(l, r);
	return 0;
}
BEGCMP(pdoublecomp){
	DEFLR(double);
	CMPRET(l, r);
	return 0;
}

int32_t pstrcomp (const void *left, const void *right) {
	char* l = *(char**)left;
	char* r = *(char**)right;

	return strcmp(l, r);
}

BEGCMP(phwllABcomp){
	DEFLR(hwll);
	CMPRET(l.a, r.a);
	CMPRET(l.b, r.b);
	return 0;
}
BEGCMP(phwllREVcomp){
	DEFLR(hwll);
	CMPRET(l.b, r.b);
	CMPRET(l.a, r.a);
	return 0;
}
BEGCMP(ptriplecomp){
	DEFLR(hwllc);
	CMPRET(l.a, r.a);
	CMPRET(l.b, r.b);
	CMPRET(l.c, r.c);
	return 0;
}
BEGCMP(ptripleREVcomp){
	DEFLR(hwllc);
	CMPRET(l.b, r.b);
	CMPRET(l.a, r.a);
	CMPRET(l.c, r.c);
	return 0;
}
BEGCMP(ptripleCABcomp){
	DEFLR(hwllc);
	CMPRET(l.c, r.c);
	CMPRET(l.a, r.a);
	CMPRET(l.b, r.b);
	return 0;
}
BEGCMP(phwrealcomp){
	DEFLR(hwreal);
	CMPRET(l.a, r.a);
	CMPRET(l.b, r.b);
	return 0;
}

int32_t pquadcomp (const void *left, const void *right) {
	linell l = *(linell*)left;
	linell r = *(linell*)right;

	sll ac = phwllABcomp(&(l.a), &(r.a));
	if (ac) return ac;
	sll bc = phwllABcomp(&(l.b), &(r.b));
	if (bc) return bc;

	return 0;
}
int32_t pfracomp (const void *left, const void *right) {
	hwllc l = *(hwllc*)left;
	hwllc r = *(hwllc*)right;

	CMPRET(l.a * r.b, l.b * r.a);
	return 0;
}
bool isinrange (sll left, sll x, sll right) {
	return (left <= x && x <= right);
}

bool isinrange_soft (sll left, sll x, sll right) {
	return (left <= x && x <= right) || (left >= x && x >= right);
}

void sw (sll *l, sll *r) {
	if (*l == *r) return;
	sll t = *l;
	*l = *r;
	*r = t;
}

ull frac[N_MAX * 3], invf[N_MAX * 3];
ull ncr (sll n, sll r, ull m) {
	if (n < 0 || r < 0 || n < r) return 0;
	return frac[n] * (invf[r] * invf[n - r] % m) % m;
}

sll a[N_MAX + 5];
// ull a[N_MAX + 5];
// sll a[3001][3001];
sll b[N_MAX + 5];
// ull b[N_MAX + 5];
// sll b[3001][3001];
sll c[N_MAX + 5];
sll d[N_MAX + 5];
sll e[N_MAX * 4];
char s[N_MAX + 1];
// char s[3010][3010];
char t[N_MAX + 1];
// char t[3010][3010];
char u[N_MAX + 1];
hwll xy[N_MAX + 5];
hwllc tup[N_MAX + 5];
sll table[3005][3005];
ull gin[N_MAX];
// here we go

hwll sorted[N_MAX];
hwll tg[N_MAX];
ull tin[N_MAX];

ull rank[N_MAX];

ull dp[N_MAX];
ull bsum[N_MAX];
void dfs (ull v) {
	dp[v] = smax(a[v] - b[v], 0);

	for (sll i = tin[v]; i < tin[v + 1]; i++) {
		ull u = tg[i].b;

		dfs(u);
		if (dp[u] + bsum[u] + b[v] >= a[v]) {
			dp[v] = smin(dp[v], dp[u]);
		} else {
			dp[v] = smin(dp[v], a[v] - bsum[u] - b[v]);
		}

		bsum[v] += bsum[u];
	}
	bsum[v] += b[v];

	// printf("%lld: %lld + %lld\n", v, dp[v], bsum[v]);
}

ull parent[N_MAX], size[N_MAX];
void init (ull n) {
	for (sll i = 0; i < n; i++) {
		parent[i] = i;
		size[i] = 1;
	}
}
ull find (ull x) {
	if (parent[x] == x) return x;
	return parent[x] = find(parent[x]);
}
bool unite (ull p, ull v) {
	parent[v] = p;
	return true;
}

ull solve () {
	sll i, j, ki, li;
	// ull result = 0;
	sll result = 0;
	double dresult = 0;
	// ull maybe = 0;
	sll maybe = 0;
	// ull sum = 0;
	sll sum = 0;
	sll item;
	sll *dpcell;

	for (i = 0; i < m; i++) {
		xy[i + m] = (hwll){xy[i].b, xy[i].a};
	}
	qsort(xy, m * 2, sizeof(hwll), phwllABcomp);
	i = j = 0;
	while (i <= n) {
		gin[i] = j;
		while (j < m * 2 && xy[j].a == i) j++;
		i++;
	}

	for (i = 0; i < n; i++) {
		sorted[i] = (hwll){smax(a[i] - b[i], 0), i};
	}
	qsort(sorted, n, sizeof(hwll), phwllABcomp);
	for (i = 0; i < n; i++) {
		rank[sorted[i].b] = i;
		// printf("%lld..\n", sorted[i].b);
	}

	init(n);

	ki = 0;
	for (i = 0; i < n; i++) {
		ull v = sorted[i].b;

		for (j = gin[v]; j < gin[v + 1]; j++) {
			ull u = xy[j].b;

			if (rank[u] >= rank[v]) continue;
			if (find(u) == find(v)) continue;

			ull up = find(u);
			// printf("%lld->%lld\n", v, up);
			tg[ki++] = (hwll){v, up};
			unite(v, up);
		}
	}
	if (ki != n - 1) abort();

	qsort(tg, n - 1, sizeof(hwll), phwllABcomp);
	i = j = 0;
	while (i <= n) {
		tin[i] = j;
		while (j < n - 1 && tg[j].a == i) j++;
		i++;
	}

	ull root = sorted[n - 1].b;
	dfs(root);

	result = dp[root] + bsum[root];

	printf("%lld\n", result);
	// printf("%.15f\n", dresult);
	// puts(s);

	return 0;

	success:
	// puts("YES");
	puts("Yes");
	// printf("%llu\n", result);
	// puts("0");
	// puts("First");
	return 0;

	fail:
	// puts("NO");
	// puts("No");
	// puts("0");
	puts("-1");
	// puts("-1 -1 -1");
	// puts("Second");
	return 1;
}

int32_t main (int argc, char *argv[]) {
	int32_t i, j;

	n = 4;
	m = 0;

	// scanf("%llu", &m);
	// scanf("%llu%llu", &h, &w);
	scanf("%llu%llu", &n, &m);
	// scanf("%llu", &k, &n, &m);
	// scanf("%llu%llu", &h, &w);
	// scanf("%llu", &q);
	// scanf("%lld%lld", &va, &vb, &vc, &vd);
	// va--;
	// vb--;
	// scanf("%llu%llu%llu%llu", &ua, &ub, &uc, &ud, &ue);
	// scanf("%s", s);
	// scanf("%s", t);
	// scanf("%s", u);
	// scanf("%llu", &k);
	// scanf("%lld", &m);
	// for (i = 0; i < n; i++) {
	// 	// scanf("%lld", &a[i]);
	// 	scanf("%lld", &d[i]);
	// }
	// scanf("%llu", &q);
	for (i = 0; i < n; i++) {
		// scanf("%lld%lld", &xy[i].a, &xy[i].b);
		// scanf("%lld%lld%lld", &tup[i].a, &tup[i].b, &tup[i].c);
		// scanf("%lld", &c[i]);

		scanf("%lld", &a[i]);
		scanf("%lld", &b[i]);
		// scanf("%lld", &c[i]);
		// scanf("%lld", &d[i]);
		// a[i]--;
		// b[i]--;
		// c[i]--;
		// d[i]--;
		// xy[i].a--;
		// xy[i].b--;
		// tup[i].a--;
		// tup[i].b--;
	}
	// scanf("%llu", &m);
	// scanf("%llu", &q);
	for (i = 0; i < m; i++) {
		scanf("%lld%lld", &xy[i].a, &xy[i].b);
		xy[i].a--;
		xy[i].b--;
		// scanf("%lld%lld", &a[i], &b[i]);
		// scanf("%lld", &b[i]);
		// a[i]--;
		// b[i]--;
		// scanf("%lld", &c[i]);
		// scanf("%lld", &d[i]);
		// scanf("%lld", &e[i]);
		// c[i]--;
		// d[i]--;
	}

	// for (i = 0; i < q; i++) {
	// 	// scanf("%lld%lld", &xy[i].a, &xy[i].b);
	// 	scanf("%lld", &c[i]);
	// 	// xy[i].a--;
	// 	// xy[i].b--;
	// }

	// for (i = 0; i < h; i++) {
	// 	for (j = 0; j < w; j++) {
	// 		scanf("%lld", &table[i][j]);
	// 		// table[i][j]--;
	// 	}
	// }
	// for (i = 0; i < h; i++) {
	// 	scanf("%s", s[i]);
	// }
	// scanf("%llu", &q);

	solve();

	return 0;
}

Score : 1000 points

Problem Statement

There is a simple undirected graph with N vertices and M edges.
The vertices are numbered 1 through N, and the edges are numbered 1 through M.
Edge i connects Vertex U_i and V_i.
Also, Vertex i has two predetermined integers A_i and B_i.
You will play the following game on this graph.

First, choose one vertex and stand on it, with W yen (the currency of Japan) in your pocket.
Here, A_s \leq W must hold, where s is the vertex you choose.
Then, perform the following two kinds of operations any number of times in any order:

Choose one vertex v that is directly connected by an edge to the vertex you are standing on, and move to vertex v. Here, you need to have at least A_v yen in your pocket when you perform this move.

Donate B_v yen to the vertex v you are standing on. Here, the amount of money in your pocket must not become less than 0 yen.

You win the game when you donate once to every vertex.
Find the smallest initial amount of money W that enables you to win the game.

Constraints

1 \leq N \leq 10^5

N-1 \leq M \leq 10^5

1 \leq A_i,B_i \leq 10^9

1 \leq U_i < V_i \leq N

The given graph is connected and simple (there is at most one edge between any pair of vertices).

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
:
A_N B_N
U_1 V_1
U_2 V_2
:
U_M V_M

Output

Print the smallest initial amount of money W that enables you to win the game.

Sample Input 1

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

Sample Output 1

6

If you have 6 yen initially, you can win the game as follows:

Stand on Vertex 4. This is possible since you have not less than 6 yen.

Donate 2 yen to Vertex 4. Now you have 4 yen.

Move to Vertex 3. This is possible since you have not less than 4 yen.

Donate 1 yen to Vertex 3. Now you have 3 yen.

Move to Vertex 2. This is possible since you have not less than 1 yen.

Move to Vertex 1. This is possible since you have not less than 3 yen.

Donate 1 yen to Vertex 1. Now you have 2 yen.

Move to Vertex 2. This is possible since you have not less than 1 yen.

Donate 2 yen to Vertex 2. Now you have 0 yen.

If you have less than 6 yen initially, you cannot win the game. Thus, the answer is 6.

Sample Input 2

5 8
6 4
15 13
15 19
15 1
20 7
1 3
1 4
1 5
2 3
2 4
2 5
3 5
4 5

Sample Output 2

44

Sample Input 3

9 10
131 2
98 79
242 32
231 38
382 82
224 22
140 88
209 70
164 64
6 8
1 6
1 4
1 3
4 7
4 9
3 7
3 9
5 9
2 5

Sample Output 3

582


#include<stdio.h>
int main(void){
  long long int a,b,c,x;
  scanf("%d %d %d %d",&a,&b,&c,&x);
  long long ans=a-b;
  if(x&1){
    ans=-1*ans;
  }
  printf("%lld\n",ans);
  return 0;
}

Score : 300 points

Problem Statement

Takahashi, Nakahashi and Hikuhashi have integers A, B and C, respectively.
After repeating the following operation K times, find the integer Takahashi will get minus the integer Nakahashi will get:

Each of them simultaneously calculate the sum of the integers that the other two people have, then replace his own integer with the result.

However, if the absolute value of the answer exceeds 10^{18}, print Unfair instead.

Constraints

1 \leq A,B,C \leq 10^9

0 \leq K \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C K

Output

Print the integer Takahashi will get minus the integer Nakahashi will get, after repeating the following operation K times.
If the absolute value of the answer exceeds 10^{18}, print Unfair instead.

Sample Input 1

1 2 3 1

Sample Output 1

1

After one operation, Takahashi, Nakahashi and Hikuhashi have 5, 4 and 3, respectively. We should print 5-4=1.

Sample Input 2

2 3 2 0

Sample Output 2

-1

Sample Input 3

1000000000 1000000000 1000000000 1000000000000000000

Sample Output 3

0


//ABC097A
#include<stdio.h>
main() {
	int a, b, c, d, i, j, k;

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

	i = abs(c - a);
	j = abs(b - a);
	k = abs(c - b);

	if (i <= d) {
		printf("YES");

	}else if (j <= d && k <= d){
		printf("YES");

	}
	else {
		printf("NO");
	}
	return 0;
}

Score : 100 points

Problem Statement

Three people, A, B and C, are trying to communicate using transceivers.
They are standing along a number line, and the coordinates of A, B and C are a, b and c (in meters), respectively.
Two people can directly communicate when the distance between them is at most d meters.
Determine if A and C can communicate, either directly or indirectly.
Here, A and C can indirectly communicate when A and B can directly communicate and also B and C can directly communicate.

Constraints

1 ≤ a,b,c ≤ 100

1 ≤ d ≤ 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

a b c d

Output

If A and C can communicate, print Yes; if they cannot, print No.

Sample Input 1

4 7 9 3

Sample Output 1

Yes

A and B can directly communicate, and also B and C can directly communicate, so we should print Yes.

Sample Input 2

100 10 1 2

Sample Output 2

No

They cannot communicate in this case.

Sample Input 3

10 10 10 1

Sample Output 3

Yes

There can be multiple people at the same position.

Sample Input 4

1 100 2 10

Sample Output 4

Yes


#include <stdio.h>
int main(){
  double i,j,x,r=1;
  scanf("%lf",&x);
  for(i=2;i<=x;i++)for(j=2;pow(i,j)<=x;j++)if(pow(i,j)>r)r=pow(i,j);
  printf("%d",(int)r);
  return 0;
}

Score : 200 points

Problem Statement

You are given a positive integer X.
Find the largest perfect power that is at most X.
Here, a perfect power is an integer that can be represented as b^p, where b is an integer not less than 1 and p is an integer not less than 2.

Constraints

1 ≤ X ≤ 1000

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the largest perfect power that is at most X.

Sample Input 1

10

Sample Output 1

9

There are four perfect powers that are at most 10: 1, 4, 8 and 9.
We should print the largest among them, 9.

Sample Input 2

1

Sample Output 2

1

Sample Input 3

999

Sample Output 3

961


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

int main()
{
    double numX;
    scanf("%lf", &numX);

    double outnum;

    for (double numA = 1; numA <= 31; numA++)
    {
        for (double pownum = 2; pownum <= 10; pownum++)
        {
            double ans = pow(numA, pownum);
            if (ans < numX)
            {
                outnum = ans;
            }
        }
    }

    printf("%lf\n", outnum);

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int comparewhether(int x,int y,int z);
int main()
{
    int i,x,k=2;
    double result, max=1.0;
    double ii,kk;
    scanf("%d",&x);

    kk=(double)k;

    if(x==1)
    {
        printf("%d",x);
    }
    else
    {

        for(i=2; i<32; i++)
        {
            ii=(double)i;
            while(pow(ii,kk)<x&&k<10)
            {

                if(pow(ii,kk)>max)
                {
                    max=pow(ii,kk);
                }
                printf("result=%f\n",pow(ii,kk));
                printf("max=%f\n",max);

                 k++;
                  kk=(double)k;
            }

            k=2;
             kk=(double)k;

        }
        printf("%d",(int)max);
    }

    return 0;

}


#include <stdio.h>

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

    for(i = 1; i < X; i++) {
        if(X == 1) {
            printf("X\n");
            break;
        }
        else if(i * i > X) {
            printf("%d\n", (i-1) * (i-1));
            break;
        }
    }

    return 0;
}


#include <stdio.h>
int main(void){
    int t[2];
    scanf("%d %d",&t[0],&t[1]);
    if(t[0]<t[1]){printf("%d\n",t[0]);}else{printf("%d\n",t[0]-1);}
    return 0;
}

Score: 100 points

Problem Statement

In AtCoder Kingdom, Gregorian calendar is used, and dates are written in the "year-month-day" order, or the "month-day" order without the year.

For example, May 3, 2018 is written as 2018-5-3, or 5-3 without the year.

In this country, a date is called Takahashi when the month and the day are equal as numbers. For example, 5-5 is Takahashi.

How many days from 2018-1-1 through 2018-a-b are Takahashi?

Constraints

a is an integer between 1 and 12 (inclusive).

b is an integer between 1 and 31 (inclusive).

2018-a-b is a valid date in Gregorian calendar.

Input

Input is given from Standard Input in the following format:

a b

Output

Print the number of days from 2018-1-1 through 2018-a-b that are Takahashi.

Sample Input 1

5 5

Sample Output 1

5

There are five days that are Takahashi: 1-1, 2-2, 3-3, 4-4 and 5-5.

Sample Input 2

2 1

Sample Output 2

1

There is only one day that is Takahashi: 1-1.

Sample Input 3

11 30

Sample Output 3

11

There are eleven days that are Takahashi: 1-1, 2-2, 3-3, 4-4, 5-5, 6-6, 7-7, 8-8, 9-9, 10-10 and 11-11.


#include <stdio.h>

int main(void)
{
  int a, b;
  scanf("%d %d", &a, &b);
  if(a <= b) printf("%d\n", a);
  else printf("%d\n", a - 1);

  return 0;
}


#include <stdio.h>
int main() {
int a, b, x = 0;
scanf("%d", &a);
scanf("%d", &b);
if (a <= b) {
x = a;
} else if(a > b) {
x = a - 1;
} printf("%d\n", x);
return 0;
}


Score: 300 points

Problem Statement

We have a canvas divided into a grid with H rows and W columns. The square at the i-th row from the top and the j-th column from the left is represented as (i, j).

Initially, all the squares are white. square1001 wants to draw a picture with black paint. His specific objective is to make Square (i, j) black when s_{i, j}= #, and to make Square (i, j) white when s_{i, j}= ..

However, since he is not a good painter, he can only choose two squares that are horizontally or vertically adjacent and paint those squares black, for some number of times (possibly zero). He may choose squares that are already painted black, in which case the color of those squares remain black.

Determine if square1001 can achieve his objective.

Constraints

H is an integer between 1 and 50 (inclusive).

W is an integer between 1 and 50 (inclusive).

For every (i, j) (1 \leq i \leq H, 1 \leq j \leq W), s_{i, j} is # or ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1} s_{1, 2} s_{1, 3} ... s_{1, W}
s_{2, 1} s_{2, 2} s_{2, 3} ... s_{2, W}
: :
s_{H, 1} s_{H, 2} s_{H, 3} ... s_{H, W}

Output

If square1001 can achieve his objective, print Yes; if he cannot, print No.

Sample Input 1

3 3
.#.
###
.#.

Sample Output 1

Yes

One possible way to achieve the objective is shown in the figure below. Here, the squares being painted are marked by stars.

Sample Input 2

5 5
#.#.#
.#.#.
#.#.#
.#.#.
#.#.#

Sample Output 2

No

square1001 cannot achieve his objective here.

Sample Input 3

11 11
...#####...
.##.....##.
#..##.##..#
#..##.##..#
#.........#
#...###...#
.#########.
.#.#.#.#.#.
##.#.#.#.##
..##.#.##..
.##..#..##.

Sample Output 3

Yes


#include<stdio.h>
int main(void){
  int N,sum=0,b=0,ans=0,i,j,count=0;
  scanf("%d",&N);
  int A[N],Asum[N];
  for(i=0;i<N;i++){
    scanf("%d",&A[i]);
  }
  for(i=0;i<N;i++){
    sum=sum+A[i];
    Asum[i]=sum;
  }
  for(i=0;i<N;i++){
    b=Asum[i];
    for(j=0;j<=i;j++){
      if(b==0){
        count=count+1;
      }
      b=b-A[j];
    }
  }
  printf("%d",count);
    return 0;
}

Score : 200 points

Problem Statement

We have an integer sequence A, whose length is N.

Find the number of the non-empty contiguous subsequences of A whose sums are 0.
Note that we are counting the ways to take out subsequences.
That is, even if the contents of some two subsequences are the same, they are counted individually if they are taken from different positions.

Constraints

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

-10^9 \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 A_2 ... A_N

Output

Find the number of the non-empty contiguous subsequences of A whose sum is 0.

Sample Input 1

6
1 3 -4 2 2 -2

Sample Output 1

3

There are three contiguous subsequences whose sums are 0: (1,3,-4), (-4,2,2) and (2,-2).

Sample Input 2

7
1 -1 1 -1 1 -1 1

Sample Output 2

12

In this case, some subsequences that have the same contents but are taken from different positions are counted individually.
For example, three occurrences of (1, -1) are counted.

Sample Input 3

5
1 -2 3 -4 5

Sample Output 3

0

There are no contiguous subsequences whose sums are 0.