Logo
(追記) (追記ここまで)

4274번 - Eventually periodic sequence 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 128 MB222100.000%

문제

Given is a function f: 0..N --> 0..N for a non-negative N and a non-negative integer n ≤ N. One can construct an infinite sequence F = f1(n), f2(n), ... fk(n) ... , where fk(n) is defined recursively as follows: f1(n) = f(n) and fk+1(n) = f(fk(n)).

It is easy to see that each such sequence F is eventually periodic, that is periodic from some point onwards, e.g 1, 2, 7, 5, 4, 6, 5, 4, 6, 5, 4, 6 ... . Given non-negative integer N ≤ 11000000 , n ≤ N and f, you are to compute the period of sequence F.

입력

Each line of input contains N, n and the a description of f in postfix notation, also known as Reverse Polish Notation (RPN). The operands are either unsigned integer constants or N or the variable x. Only binary operands are allowed: + (addition), * (multiplication) and % (modulo, i.e. remainder of integer division). Operands and operators are separated by whitespace. The operand % occurs exactly once in a function and it is the last (rightmost, or topmost if you wish) operator and its second operand is always N whose value is read from input. The following function:

2 x * 7 + N %

is the RPN rendition of the more familiar infix (2*x+7)%N. All input lines are shorter than 100 characters. The last line of input has N equal 0 and should not be processed.

출력

For each line of input, output one line with one integer number, the period of F corresponding to the data given in the input line.

제한

예제 입력 1

10 1 x N %
11 1 x x 1 + * N %
1728 1 x x 1 + * x 2 + * N %
1728 1 x x 1 + x 2 + * * N %
100003 1 x x 123 + * x 12345 + * N %
0 0 0 N %

예제 출력 1

1
3
6
6
369

힌트

출처

Contest > Waterloo's local Programming Contests > 27 May, 2006 C번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /