A - Addition and Subtraction Easy

実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 100

問題文

joisinoお姉ちゃんは、A op B という式の値を計算したいと思っています。 ここで、A,B は整数で、op は、+ または - の記号です。 あなたの仕事は、joisinoお姉ちゃんの代わりにこれを求めるプログラムを作ることです。

制約

  • 1≦A,B≦10^9
  • op は、+ または - の記号である。

入力

入力は以下の形式で標準入力から与えられる。

A op B

出力

式の値を出力せよ。


入力例 1

1 + 2

出力例 1

3

1 + 2 = 3 なので 3 を出力します。


入力例 2

5 - 7

出力例 2

-2

Score : 100 points

Problem Statement

Joisino wants to evaluate the formula "A op B". Here, A and B are integers, and the binary operator op is either + or -. Your task is to evaluate the formula instead of her.

Constraints

  • 1≦A,B≦10^9
  • op is either + or -.

Input

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

A op B

Output

Evaluate the formula and print the result.


Sample Input 1

1 + 2

Sample Output 1

3

Since 1 + 2 = 3, the output should be 3.


Sample Input 2

5 - 7

Sample Output 2

-2
B - Contest with Drinks Easy

実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 200

問題文

joisinoお姉ちゃんは、あるプログラミングコンテストの決勝を控えています。 このコンテストでは、N 問の問題が用意されており、それらには 1~N の番号がついています。 joisinoお姉ちゃんは、問題 i(1≦i≦N) を解くのにかかる時間が T_i 秒であることを知っています。

また、このコンテストでは、M 種類のドリンクが提供されており、1~M の番号がついています。 そして、ドリンク i(1≦i≦M) を飲むと、脳が刺激され、問題 P_i を解くのにかかる時間が X_i 秒になります。 他の問題を解くのにかかる時間に変化はありません。

コンテスタントは、コンテスト開始前にいずれかのドリンクを 1 本だけ飲むことができます。 joisinoお姉ちゃんは、それぞれのドリンクについて、それを飲んだ際に、全ての問題を解くのに何秒必要なのかを知りたくなりました。 全ての問題を解くのに必要な時間とは、それぞれの問題を解くのにかかる時間の合計です。 あなたの仕事は、joisinoお姉ちゃんの代わりにこれを求めるプログラムを作成することです。

制約

  • 入力は全て整数である
  • 1≦N≦100
  • 1≦T_i≦10^5
  • 1≦M≦100
  • 1≦P_i≦N
  • 1≦X_i≦10^5

入力

入力は以下の形式で標準入力から与えられる。

N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M

出力

それぞれのドリンクについて、それを飲んだ際に全ての問題を解くのに必要な時間を求め、順番に 1 行ずつ出力せよ。


入力例 1

3
2 1 4
2
1 1
2 3

出力例 1

6
9

一つ目のドリンクを飲んだ場合、それぞれの問題を解くのに要する時間は、1 秒、1 秒、4 秒になります。 なので、それらを合計した 6 秒が答えになり、6 を出力します。

二つ目のドリンクを飲んだ場合、それぞれの問題を解くのに要する時間は、2 秒、3 秒、4 秒になります。 なので、それらを合計した 9 秒が答えになり、9 を出力します。


入力例 2

5
7 2 3 8 5
3
4 2
1 7
4 13

出力例 2

19
25
30

Score : 200 points

Problem Statement

Joisino is about to compete in the final round of a certain programming competition. In this contest, there are N problems, numbered 1 through N. Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).

Also, there are M kinds of drinks offered to the contestants, numbered 1 through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and the time it takes for her to solve problem P_i will become X_i seconds. It does not affect the time to solve the other problems.

A contestant is allowed to take exactly one of the drinks before the start of the contest. For each drink, Joisino wants to know how many seconds it takes her to solve all the problems if she takes that drink. Here, assume that the time it takes her to solve all the problems is equal to the sum of the time it takes for her to solve individual problems. Your task is to write a program to calculate it instead of her.

Constraints

  • All input values are integers.
  • 1≦N≦100
  • 1≦T_i≦10^5
  • 1≦M≦100
  • 1≦P_i≦N
  • 1≦X_i≦10^5

Input

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

N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M

Output

For each drink, calculate how many seconds it takes Joisino to solve all the problems if she takes that drink, and print the results, one per line.


Sample Input 1

3
2 1 4
2
1 1
2 3

Sample Output 1

6
9

If Joisino takes drink 1, the time it takes her to solve each problem will be 1, 1 and 4 seconds, respectively, totaling 6 seconds.

If Joisino takes drink 2, the time it takes her to solve each problem will be 2, 3 and 4 seconds, respectively, totaling 9 seconds.


Sample Input 2

5
7 2 3 8 5
3
4 2
1 7
4 13

Sample Output 2

19
25
30
C - Lining Up

実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 300

問題文

1~N までの番号がついた、N 人の人がいます。 彼らは昨日、ある順番で左右一列に並んでいましたが、今日になってその並び方が分からなくなってしまいました。 しかし、彼らは全員、「自分の左に並んでいた人数と自分の右に並んでいた人数の差の絶対値」を覚えています。 彼らの報告によると、人 i の、「自分の左に並んでいた人数と自分の右に並んでいた人数の差の絶対値」は A_i です。

彼らの報告を元に、元の並び方が何通りあり得るかを求めてください。 ただし、答えは非常に大きくなることがあるので、10^9+7 で割った余りを出力してください。 また、彼らの報告が間違っており、ありうる並び方がないこともありえます。 その際は 0 を出力してください。

制約

  • 1≦N≦10^5
  • 0≦A_i≦N-1

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 A_2 ... A_N

出力

元の並び順としてありうるものが何通りあるか求め、10^9+7 で割った余りを出力せよ。


入力例 1

5
2 4 4 0 2

出力例 1

4

ありうる並び方は、人の番号で書くと、

  • 2,1,4,5,3
  • 2,5,4,1,3
  • 3,1,4,5,2
  • 3,5,4,1,2

4 通りです。


入力例 2

7
6 4 0 2 4 0 2

出力例 2

0

どのような並び方でも、報告と矛盾するので、0 が答えになります。


入力例 3

8
7 5 1 1 7 3 5 3

出力例 3

16

Score : 300 points

Problem Statement

There are N people, conveniently numbered 1 through N. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person. According to their reports, the difference above for person i is A_i.

Based on these reports, find the number of the possible orders in which they were standing. Since it can be extremely large, print the answer modulo 10^9+7. Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print 0.

Constraints

  • 1≦N≦10^5
  • 0≦A_i≦N-1

Input

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

N
A_1 A_2 ... A_N

Output

Print the number of the possible orders in which they were standing, modulo 10^9+7.


Sample Input 1

5
2 4 4 0 2

Sample Output 1

4

There are four possible orders, as follows:

  • 2,1,4,5,3
  • 2,5,4,1,3
  • 3,1,4,5,2
  • 3,5,4,1,2

Sample Input 2

7
6 4 0 2 4 0 2

Sample Output 2

0

Any order would be inconsistent with the reports, thus the answer is 0.


Sample Input 3

8
7 5 1 1 7 3 5 3

Sample Output 3

16
D - Xor Sum

実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 600

問題文

正の整数 N が与えられます。 2 つの整数 u,v(0≦u,v≦N) であって、ある非負整数 a,b が存在して、a xor b=ua+b=v となるようなものが何通りあるかを求めてください。 ここで、xor はビットごとの排他的論理和を表します。 なお、答えは非常に大きくなることがあるので、10^9+7 で割った余りを求めてください。

制約

  • 1≦N≦10^{18}

入力

入力は以下の形式で標準入力から与えられる。

N

出力

ありうる 2 数の組が何通りあるかを求め、10^9+7 で割った余りを出力せよ。


入力例 1

3

出力例 1

5

u,v としてありうるものは、以下の 5 通りです。

  • u=0,v=0a=0,b=0 とすると、0 xor 0=00+0=0 となります。)

  • u=0,v=2a=1,b=1 とすると、1 xor 1=01+1=2 となります。)

  • u=1,v=1a=1,b=0 とすると、1 xor 0=11+0=1 となります。)

  • u=2,v=2a=2,b=0 とすると、2 xor 0=22+0=2 となります。)

  • u=3,v=3a=3,b=0 とすると、3 xor 0=33+0=3 となります。)


入力例 2

1422

出力例 2

52277

入力例 3

1000000000000000000

出力例 3

787014179

Score : 600 points

Problem Statement

You are given a positive integer N. Find the number of the pairs of integers u and v (0≦u,v≦N) such that there exist two non-negative integers a and b satisfying a xor b=u and a+b=v. Here, xor denotes the bitwise exclusive OR. Since it can be extremely large, compute the answer modulo 10^9+7.

Constraints

  • 1≦N≦10^{18}

Input

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

N

Output

Print the number of the possible pairs of integers u and v, modulo 10^9+7.


Sample Input 1

3

Sample Output 1

5

The five possible pairs of u and v are:

  • u=0,v=0 (Let a=0,b=0, then 0 xor 0=0, 0+0=0.)

  • u=0,v=2 (Let a=1,b=1, then 1 xor 1=0, 1+1=2.)

  • u=1,v=1 (Let a=1,b=0, then 1 xor 0=1, 1+0=1.)

  • u=2,v=2 (Let a=2,b=0, then 2 xor 0=2, 2+0=2.)

  • u=3,v=3 (Let a=3,b=0, then 3 xor 0=3, 3+0=3.)


Sample Input 2

1422

Sample Output 2

52277

Sample Input 3

1000000000000000000

Sample Output 3

787014179