PROGRAM ATCODER

implicit none
integer(8) :: n,s,f,n_d,t,i,j,v1,v10,b
real(8) :: n_root

read(*,*) n,s

if(n<s)then
  write(*,'(i0)') -1
  stop
end if

if(n == s)then
  write(*,'(i0)') n+1
  stop
end if

n_root = real(n)**(0.5)
n_d = int(n_root)

do i = 2, n_d
  f = 0
  t = n
  do
    f = f + mod(t,i)
    if(t < i)exit
    t = t/i
  end do
  if(f == s)then
    write(*,'(i0)') i
    stop
  end if
end do

do v10 = 1, n_d
  if(mod(n-s,v10) /= 0)cycle
  b = (n-s)/v10 + 1
  f = mod(n,b) + n/b
  if(f == s)then
    write(*,'(i0)') b
    stop
  end if
end do

write(*,'(i0)') -1

contains
!!!!!! サブルーチンは↓

!!!!!!

END PROGRAM ATCODER

Score : 500 points

Problem Statement

For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:

f(b,n) = n, when n < b

f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b

Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b,
and n \ {\rm mod} \ b denotes the remainder of n divided by b.

Less formally, f(b,n) is equal to the sum of the digits of n written in base b.
For example, the following hold:

f(10,\,87654)=8+7+6+5+4=30

f(100,\,87654)=8+76+54=138

You are given integers n and s.
Determine if there exists an integer b (b \geq 2) such that f(b,n)=s.
If the answer is positive, also find the smallest such b.

Constraints

1 \leq n \leq 10^{11}

1 \leq s \leq 10^{11}

n,\,s are integers.

Input

The input is given from Standard Input in the following format:

n
s

Output

If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b.
If such b does not exist, print -1 instead.

Sample Input 1

87654
30

Sample Output 1

10

Sample Input 2

87654
138

Sample Output 2

100

Sample Input 3

87654
45678

Sample Output 3

-1

Sample Input 4

31415926535
1

Sample Output 4

31415926535

Sample Input 5

1
31415926535

Sample Output 5

-1


program main
  implicit none
  integer(16)::n,i,s
  integer(16),allocatable::a(:),m(:)
  read*,n
  allocate(a(n),m(n))
  read*,a
  m=iand(a,1)
  s=0
  do i=2,n-1
    if(m(i)+m(i-1).eq.2)then
        s=s+1
        m(i)=0
        m(i-1)=0
    else if(m(i)+m(i+1).eq.2)then
        s=s+1
        m(i)=0
        m(i+1)=0
     endif
  enddo
  print*,sum(a/2)+s
end program main

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

Score : 200 points

Problem Statement

Snuke lives on an infinite two-dimensional plane. He is going on an N-day trip.
At the beginning of Day 1, he is at home. His plan is described in a string S of length N.
On Day i(1 ≦ i ≦ N), he will travel a positive distance in the following direction:

North if the i-th letter of S is N

West if the i-th letter of S is W

South if the i-th letter of S is S

East if the i-th letter of S is E

He has not decided each day's travel distance. Determine whether it is possible to set each day's travel distance so that he will be back at home at the end of Day N.

Constraints

1 ≦ | S | ≦ 1000

S consists of the letters N, W, S, E.

Input

The input is given from Standard Input in the following format:

S

Output

Print Yes if it is possible to set each day's travel distance so that he will be back at home at the end of Day N. Otherwise, print No.

Sample Input 1

SENW

Sample Output 1

Yes

If Snuke travels a distance of 1 on each day, he will be back at home at the end of day 4.

Sample Input 2

NSNNSNSN

Sample Output 2

Yes

Sample Input 3

NNEW

Sample Output 3

No

Sample Input 4

W

Sample Output 4

No

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.


program sequential_operations_on_sequence
  implicit none
  integer :: n, q, i, j, u = 1, l, r, m, x
  integer(8) :: p(0:100000) = 0_8, k, t(100000) = 0_8, ans(100001) = 0_8
  read(*,*) n, q
  p(1) = int(n,8)
  do i = 1, q
    read(*,*) k
    do while (k <= p(u))
      u = u-1
    end do
    u = u+1
    p(u) = k
  end do
  t(u) = 1_8
  do i = u, 1, -1
    j = i
    k = p(i)
    do while (k > p(1))
      r = j
      l = 1
      do while (r-l > 1)
        m = (l+r)/2
        if (p(m) > k) then
          r = m
        else
          l = m
        end if
      end do
      j = l
      t(j) = t(j)+t(i)*(k/p(j))
      k = mod(k,p(j))
    end do
    x = int(k,4)
    ans(x) = ans(x)+t(i)
  end do
  do i = n, 1, -1
    ans(i) = ans(i)+ans(i+1)
  end do
  do i = 1, n
    write(*,'(i0)') ans(i)
  end do
end program sequential_operations_on_sequence

Score : 1400 points

Problem Statement

Snuke got an integer sequence from his mother, as a birthday present. The sequence has N elements, and the i-th of them is i.
Snuke performs the following Q operations on this sequence. The i-th operation, described by a parameter q_i, is as follows:

Take the first q_i elements from the sequence obtained by concatenating infinitely many copy of the current sequence, then replace the current sequence with those q_i elements.

After these Q operations, find how many times each of the integers 1 through N appears in the final sequence.

Constraints

1 ≦ N ≦ 10^5

0 ≦ Q ≦ 10^5

1 ≦ q_i ≦ 10^{18}

All input values are integers.

Input

The input is given from Standard Input in the following format:

N Q
q_1
:
q_Q

Output

Print N lines. The i-th line (1 ≦ i ≦ N) should contain the number of times integer i appears in the final sequence after the Q operations.

Sample Input 1

5 3
6
4
11

Sample Output 1

3
3
3
2
0

After the first operation, the sequence becomes: 1,2,3,4,5,1.

After the second operation, the sequence becomes: 1,2,3,4.

After the third operation, the sequence becomes: 1,2,3,4,1,2,3,4,1,2,3.

In this sequence, integers 1,2,3,4,5 appear 3,3,3,2,0 times, respectively.

Sample Input 2

10 10
9
13
18
8
10
10
9
19
22
27

Sample Output 2

7
4
4
3
3
2
2
2
0
0


program main
	implicit none
    integer::n,t,i
    integer(8)::ans1,ans2
    integer,allocatable::a(:)
    real::ave
    read(*,*)n
    allocate(a(n))
    read(*,*)a
    ans1=0
    ans2=0
    ave=sum(a)/real(n,8)
    t=int(ave)
    do i=1,n
    	ans1=ans1+(a(i)-t)**2
    end do
    do i=1,n
    	ans2=ans2+(a(i)-(t+1))**2
    end do
    write(*,*) min(ans1,ans2)

    deallocate(a)
    stop
end program main

Score : 200 points

Problem Statement

Evi has N integers a_1,a_2,..,a_N. His objective is to have N equal integers by transforming some of them.

He may transform each integer at most once. Transforming an integer x into another integer y costs him (x-y)^2 dollars. Even if a_i=a_j (i≠j), he has to pay the cost separately for transforming each of them (See Sample 2).

Find the minimum total cost to achieve his objective.

Constraints

1≦N≦100

-100≦a_i≦100

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum total cost to achieve Evi's objective.

Sample Input 1

2
4 8

Sample Output 1

8

Transforming the both into 6s will cost (4-6)^2+(8-6)^2=8 dollars, which is the minimum.

Sample Input 2

3
1 1 3

Sample Output 2

3

Transforming the all into 2s will cost (1-2)^2+(1-2)^2+(3-2)^2=3 dollars. Note that Evi has to pay (1-2)^2 dollar separately for transforming each of the two 1s.

Sample Input 3

3
4 2 5

Sample Output 3

5

Leaving the 4 as it is and transforming the 2 and the 5 into 4s will achieve the total cost of (2-4)^2+(5-4)^2=5 dollars, which is the minimum.

Sample Input 4

4
-100 -100 -100 -100

Sample Output 4

0

Without transforming anything, Evi's objective is already achieved. Thus, the necessary cost is 0.


program unhappy_hacking
  implicit none
  integer(8), parameter :: md = 1000000007_8
  integer :: n, m, i, j
  character(5000) :: s
  integer(8) :: dp(0:5000,0:5000) = 0_8, p = 1_8, t = 500000004_8
  read(*,*) n
  read(*,*) s
  s = trim(adjustl(s))
  m = len_trim(s)
  dp(0,0) = 1_8
  do i = 1, n
    dp(i,0) = mod(dp(i-1,0)+dp(i-1,1),md)
    do j = 1, i-1
      dp(i,j) = mod(2_8*dp(i-1,j-1)+dp(i-1,j+1),md)
    end do
    dp(i,i) = mod(2_8*dp(i-1,i-1),md)
  end do
  do i = 0, 15
    if (btest(m,i)) p = mod(t*p,md)
    t = mod(t*t,md)
  end do
  write(*,'(i0)') mod(dp(n,m)*p,md)
  stop
end program unhappy_hacking

Score : 800 points

Problem Statement

Sig has built his own keyboard. Designed for ultimate simplicity, this keyboard only has 3 keys on it: the 0 key, the 1 key and the backspace key.

To begin with, he is using a plain text editor with this keyboard. This editor always displays one string (possibly empty). Just after the editor is launched, this string is empty. When each key on the keyboard is pressed, the following changes occur to the string:

The 0 key: a letter 0 will be inserted to the right of the string.

The 1 key: a letter 1 will be inserted to the right of the string.

The backspace key: if the string is empty, nothing happens. Otherwise, the rightmost letter of the string is deleted.

Sig has launched the editor, and pressed these keys N times in total. As a result, the editor displays a string s. Find the number of such ways to press the keys, modulo 10^9 + 7.

Constraints

1 ≦ N ≦ 5000

1 ≦ |s| ≦ N

s consists of the letters 0 and 1.

Partial Score

400 points will be awarded for passing the test set satisfying 1 ≦ N ≦ 300.

Input

The input is given from Standard Input in the following format:

N
s

Output

Print the number of the ways to press the keys N times in total such that the editor displays the string s in the end, modulo 10^9+7.

Sample Input 1

3
0

Sample Output 1

5

We will denote the backspace key by B. The following 5 ways to press the keys will cause the editor to display the string 0 in the end: 00B, 01B, 0B0, 1B0, BB0. In the last way, nothing will happen when the backspace key is pressed.

Sample Input 2

300
1100100

Sample Output 2

519054663

Sample Input 3

5000
01000001011101000100001101101111011001000110010101110010000

Sample Output 3

500886057


integer n
read*,n
print"(i0)",n*(n+1)/2
end

Score : 100 points

Problem Statement

There are N children in AtCoder Kindergarten. Mr. Evi will arrange the children in a line, then give 1 candy to the first child in the line, 2 candies to the second child, ..., N candies to the N-th child. How many candies will be necessary in total?

Constraints

1≦N≦100

Input

The input is given from Standard Input in the following format:

N

Output

Print the necessary number of candies in total.

Sample Input 1

3

Sample Output 1

6

The answer is 1+2+3=6.

Sample Input 2

10

Sample Output 2

55

The sum of the integers from 1 to 10 is 55.

Sample Input 3

1

Sample Output 3

1

Only one child. The answer is 1 in this case.


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    character(10):: s
    integer(int32):: i

    read*, s
    i=1
    do while(i <= len_trim(s))
        if (s(i:i) == 'B') then
            s(i:) = s(i+1:) // ' '
            if (i>1) then
                s(i-1:) = s(i:) // ' '
                i=i-1
            end if
        else
            i=i+1
        end if
    end do
    print'(a)', s
end program name

Score : 200 points

Problem Statement

Sig has built his own keyboard. Designed for ultimate simplicity, this keyboard only has 3 keys on it: the 0 key, the 1 key and the backspace key.

To begin with, he is using a plain text editor with this keyboard. This editor always displays one string (possibly empty). Just after the editor is launched, this string is empty. When each key on the keyboard is pressed, the following changes occur to the string:

The 0 key: a letter 0 will be inserted to the right of the string.

The 1 key: a letter 1 will be inserted to the right of the string.

The backspace key: if the string is empty, nothing happens. Otherwise, the rightmost letter of the string is deleted.

Sig has launched the editor, and pressed these keys several times. You are given a string s, which is a record of his keystrokes in order. In this string, the letter 0 stands for the 0 key, the letter 1 stands for the 1 key and the letter B stands for the backspace key. What string is displayed in the editor now?

Constraints

1 ≦ |s| ≦ 10 (|s| denotes the length of s)

s consists of the letters 0, 1 and B.

The correct answer is not an empty string.

Input

The input is given from Standard Input in the following format:

s

Output

Print the string displayed in the editor in the end.

Sample Input 1

01B0

Sample Output 1

00

Each time the key is pressed, the string in the editor will change as follows: 0, 01, 0, 00.

Sample Input 2

0BB1

Sample Output 2

1

Each time the key is pressed, the string in the editor will change as follows: 0, (empty), (empty), 1.


program main

  implicit none
  character(10) :: s,a
  integer :: i,j

  read(*,*) s
  a = ""
  j = 1
  do i = 1, len_trim( s )
    if( s(i:i) == "0" ) then
      a(j:j) = "0"
      j = j + 1
    else if( s(i:i) == "1" ) then
      a(j:j) = "1"
      j = j + 1
    else if( s(i:i) == "B" ) then
      if ( j > 1 )j = j - 1
    end if
  end do

  print*, trim( a )

end program main


program abc043c
  implicit none
  integer :: n, i, t, ans = 1000000
  integer,allocatable :: a(:)
  read *, n
  allocate(a(n))
  read *, a
  do i = 1,100
     t = sum(abs(a-i)**2)
     if (t < ans) ans = t;
  end do
  print *, ans
end program abc043c


program ABC043D
    implicit none
    integer(8)::i
    integer(8)::flg=0
    character(100000)::s
    read(5,*)s

    do i=1,len_trim(s)-1
        if(s(i:i)==s(i+1:i+1))then
            flg=1
            print'(i0,1x,i0)',i,i+1
            exit
        else if(i<=len_trim(s)-2)then
            if(s(i:i)==s(i+2:i+2))then
                flg=1
                print'(i0,1x,i0)',i,i+2
                exit
            end if
        end if
    end do

    if(flg==0)then
        print'(i0,1x,i0)',-1,-1
    end if
end program ABC043D

Score : 400 points

Problem Statement

Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both voodoo and melee are unbalanced, while neither noon nor a is.

You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.

Constraints

2 ≦ |s| ≦ 10^5

s consists of lowercase letters.

Partial Score

200 points will be awarded for passing the test set satisfying 2 ≦ N ≦ 100.

Input

The input is given from Standard Input in the following format:

s

Output

If there exists no unbalanced substring of s, print -1 -1.

If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print a b. If there exists more than one such substring, any of them will be accepted.

Sample Input 1

needed

Sample Output 1

2 5

The string s_2 s_3 s_4 s_5 = eede is unbalanced. There are also other unbalanced substrings. For example, the output 2 6 will also be accepted.

Sample Input 2

atcoder

Sample Output 2

-1 -1

The string atcoder contains no unbalanced substring.


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,m,i,x,y,ans
    integer(int32), allocatable:: ball_num(:)
    logical,allocatable:: possible(:)

    read*, n,m
    allocate(ball_num(n), source=1)
    allocate(possible(n))
    possible(1) = .true.; possible(2:) = .false.

    do i=1,m
        read*, x,y
        ball_num(x)=ball_num(x)-1
        ball_num(y)=ball_num(y)+1
        if (possible(x)) possible(y)=.true.
        if (ball_num(x) == 0) possible(x) = .false.
    end do

    ans=0
    do i=1,n
        if(possible(i)) ans=ans+1
    end do

    print'(i0)', ans
end program name

Problem Statement

We have N boxes, numbered 1 through N. At first, box 1 contains one red ball, and each of the other boxes contains one white ball.

Snuke will perform the following M operations, one by one. In the i-th operation, he randomly picks one ball from box x_i, then he puts it into box y_i.

Find the number of boxes that may contain the red ball after all operations are performed.

Constraints

2≤N≤10^5

1≤M≤10^5

1≤x_i,y_i≤N

x_i≠y_i

Just before the i-th operation is performed, box x_i contains at least 1 ball.

Input

The input is given from Standard Input in the following format:

N M
x_1 y_1
:
x_M y_M

Output

Print the number of boxes that may contain the red ball after all operations are performed.

Sample Input 1

3 2
1 2
2 3

Sample Output 1

2

Just after the first operation, box 1 is empty, box 2 contains one red ball and one white ball, and box 3 contains one white ball.

Now, consider the second operation. If Snuke picks the red ball from box 2, the red ball will go into box 3. If he picks the white ball instead, the red ball will stay in box 2.
Thus, the number of boxes that may contain the red ball after all operations, is 2.

Sample Input 2

3 3
1 2
2 3
2 3

Sample Output 2

1

All balls will go into box 3.

Sample Input 3

4 4
1 2
2 3
4 1
3 4

Sample Output 3

3


program main
  implicit none
  integer::n,m,x,y,i
  integer,allocatable::a(:),c(:)
  read*,n,m
  allocate(a(n),c(n))
  a(:)=0
  a(1)=1
  c(:)=1
  do i=1,m
     read*,x,y
     if (a(x).eq.0)then
        if(c(x).ne.0)then
           c(x)=c(x)-1
           c(y)=c(y)+1
        endif
     else
        if(c(x).lt.2)a(x)=0
        c(x)=c(x)-1
        a(y)=1
        c(y)=c(y)+1
     endif
  enddo
  print*,count(a.ne.0)
end program main


integer a, b, c
read*, a, b, c
if ((a * b * c) == 175) then
	print*, "YES"
else
	print*, "NO"
end if
end

Score : 100 points

Problem Statement

Iroha loves Haiku. Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order.

To create a Haiku, Iroha has come up with three different phrases. These phrases have A, B and C syllables, respectively. Determine whether she can construct a Haiku by using each of the phrases once, in some order.

Constraints

1≦A,B,C≦10

Input

The input is given from Standard Input in the following format:

A B C

Output

If it is possible to construct a Haiku by using each of the phrases once, print YES (case-sensitive). Otherwise, print NO.

Sample Input 1

5 5 7

Sample Output 1

YES

Using three phrases of length 5, 5 and 7, it is possible to construct a Haiku.

Sample Input 2

7 7 5

Sample Output 2

NO


PROGRAM ATCODER

implicit none
integer :: n, k, cost, i, j, check
integer,allocatable :: d(:)

read(*,*) n, k
allocate (d(k))
read(*,*) (d(i), i=1,k)

cost = n - 1
do
	check = 0
    cost = cost + 1
	do j = 1, k
        if(mod(cost,10) == d(j) .and. cost >= 1)then
	        check = 1
		end if
        if(mod(cost,100)/10 == d(j) .and. cost >= 10)then
	        check = 1
		end if
		if(mod(cost,1000)/100 == d(j) .and. cost >= 100)then
            check = 1
		end if
		if(mod(cost,10000)/1000 == d(j) .and. cost >= 1000)then
            check = 1
		end if
        if(cost/10000 == d(j) .and. cost >= 10000)then
            check = 1
		end if
	end do
    if (check == 0) then
		write(*,'(i0)') cost
		stop
	end if
end do

END PROGRAM ATCODER

Score : 300 points

Problem Statement

Iroha is very particular about numbers. There are K digits that she dislikes: D_1, D_2, ..., D_K.

She is shopping, and now paying at the cashier.
Her total is N yen (the currency of Japan), thus she has to hand at least N yen to the cashier (and possibly receive the change).

However, as mentioned before, she is very particular about numbers. When she hands money to the cashier, the decimal notation of the amount must not contain any digits that she dislikes. Under this condition, she will hand the minimum amount of money.

Find the amount of money that she will hand to the cashier.

Constraints

1 ≦ N < 10000

1 ≦ K < 10

0 ≦ D_1 < D_2 < … < D_K≦9

\{D_1,D_2,...,D_K\} ≠ \{1,2,3,4,5,6,7,8,9\}

Input

The input is given from Standard Input in the following format:

N K
D_1 D_2 … D_K

Output

Print the amount of money that Iroha will hand to the cashier.

Sample Input 1

1000 8
1 3 4 5 6 7 8 9

Sample Output 1

2000

She dislikes all digits except 0 and 2.

The smallest integer equal to or greater than N=1000 whose decimal notation contains only 0 and 2, is 2000.

Sample Input 2

9999 1
0

Sample Output 2

9999


program main
  implicit none
  integer(8),parameter::M=1000000007
  integer(8) h,w,a,b,i,asize
  integer(8),allocatable::fact(:),invf(:)
  integer(16)::i16=0

  read*,h,w,a,b
  asize=h+w
  allocate(fact(asize+1),invf(asize+1))

  fact(1:2)=1
  do i=1,asize
     fact(i+1)=mod(fact(i)*i,M)
  enddo
  invf(asize+1)=powmod(fact(asize+1))
  do i=asize,1,-1
     invf(i)=mod(invf(i+1)*i,M)
  enddo

  print*,mod(dot_product(&
       i16+mod(fact(h-a+b:h-a+w-1)*invf(b+1:w),M),mod(fact(w-b+a-1:a:-1)*invf(w-b:1:-1),M))*mod(invf(h-a)*invf(a),M),M)

  !end program main
contains
  function powmod(t) result(r)
    integer(8),intent(in)::t
    integer(8)x,i,r
    i=M-2
    x=t
    r=1
    do
       if ( i .eq. 0 ) exit
       if ( iand(i,1).eq.1 )then
          r=mod(r*x,M)
       endif
       x=mod(x*x,M)
       i=ishft(i,-1)
    end do
  end function powmod
end program main

Score : 400 points

Problem Statement

We have a large square grid with H rows and W columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.

However, she cannot enter the cells in the intersection of the bottom A rows and the leftmost B columns. (That is, there are A×B forbidden cells.) There is no restriction on entering the other cells.

Find the number of ways she can travel to the bottom-right cell.

Since this number can be extremely large, print the number modulo 10^9+7.

Constraints

1 ≦ H, W ≦ 100,000

1 ≦ A < H

1 ≦ B < W

Input

The input is given from Standard Input in the following format:

H W A B

Output

Print the number of ways she can travel to the bottom-right cell, modulo 10^9+7.

Sample Input 1

2 3 1 1

Sample Output 1

2

We have a 2×3 grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".

Sample Input 2

10 7 3 4

Sample Output 2

3570

There are 12 forbidden cells.

Sample Input 3

100000 100000 99999 99999

Sample Output 3

1

Sample Input 4

100000 100000 44444 55555

Sample Output 4

738162020