

Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 点
問題文
正整数 に対し, 縦の辺の長さが , 横の辺の長さが であるような長方形を と表すことにします. なお, 本問では長方形を回転する操作は考えず, のとき長方形 と長方形 は異なるものとして扱います.
長方形の列 が 長方形生成列 であるとは, 次の手順が成功するような方法が存在することを言います.
- 長方形 を とする. 以下では, 各時点での長方形 の縦の辺の長さと横の辺の長さをそれぞれ と表す.
- の順に次のいずれか一方の操作を行う. いずれも行うことができないとき手順は失敗とし, 手順を終了する.
- の縦の辺の長さが に等しいとき, に長方形 を横向きに結合する. 正確には, その時点で のとき を長方形 に置き換える.
- の横の辺の長さが に等しいとき, に長方形 を縦向きに結合する. 正確には, その時点で のとき を長方形 に置き換える.
- 上記の一連の操作が失敗しなかった場合は手順は成功とし, 手順を終了する.
個の長方形が与えられます. 番目の長方形は, 縦の辺の長さが , 横の辺の長さが の長方形です.
を満たす正整数の組 であって次の条件を満たすものの個数を求めてください.
- 長方形の列 が長方形生成列である.
制約
- 入力される値はすべて整数.
入力
入力は以下の形式で標準入力から与えられる.
出力
答えを出力せよ.
入力例 1Copy
4 1 2 1 3 2 3 3 1
出力例 1Copy
7
条件を満たす は の つです.
例えば, については, 結合を縦向き 横向きの順に行うと手順が成功します.
入力例 2Copy
5 2 1 2 1 1 2 3 2 1 4
出力例 2Copy
10
入力例 3Copy
1 1000000 1000000
出力例 3Copy
1
入力例 4Copy
10 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
出力例 4Copy
55
Score : points
Problem Statement
For positive integers and , let denote a rectangle with height and width . In this problem, we do not consider rotating the rectangles, and the rectangles and are distinguished when .
A sequence of rectangles is called a rectangle generation sequence if there exists a method that successfully follows the steps below:
- Let the rectangle be . Hereafter, let and respectively denote the height and width of the rectangle at each step.
- For , perform one of the following operations. If neither can be performed, the procedure unsuccessfully terminates.
- If the height of is equal to , attach the rectangle horizontally to . Formally, if at that time, replace with the rectangle .
- If the width of is equal to , attach the rectangle vertically to . Formally, if at that time, replace with the rectangle .
- If the above series of operations does not fail, the procedure successfully terminates.
You are given rectangles. The -th rectangle has a height of and a width of .
Find the number of pairs of positive integers that satisfy and the following condition:
- The sequence of rectangles is a rectangle generation sequence.
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1Copy
4 1 2 1 3 2 3 3 1
Sample Output 1Copy
7
The pairs that satisfy the condition are ; there are seven.
For example, for , the procedure succeeds if the first attachment is done vertically and the second is done horizontally.
Sample Input 2Copy
5 2 1 2 1 1 2 3 2 1 4
Sample Output 2Copy
10
Sample Input 3Copy
1 1000000 1000000
Sample Output 3Copy
1
Sample Input 4Copy
10 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Sample Output 4Copy
55