CF39G.Inverse Function

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Petya wrote a programme on C++ that calculated a very interesting function f(n)f(n) . Petya ran the program with a certain value of nn and went to the kitchen to have some tea. The history has no records concerning how long the program had been working. By the time Petya returned, it had completed the calculations and had the result. However while Petya was drinking tea, a sly virus managed to destroy the input file so that Petya can't figure out for which value of nn the program was run. Help Petya, carry out the inverse function!

Mostly, the program consists of a function in C++ with the following simplified syntax:

  • functionfunction ::= int f(int n) { operatorSequenceoperatorSequence }
  • operatorSequenceoperatorSequence ::= operator  operator operatorSequenceoperator | operator operatorSequence
  • operatoroperator ::= return arithmExprarithmExpr ; | if ( logicalExprlogicalExpr ) return arithmExprarithmExpr ;
  • logicalExprlogicalExpr ::= arithmExpr>arithmExpr | arithmExpr<arithmExpr | arithmExprarithmExpr == arithmExprarithmExpr
  • arithmExprarithmExpr ::= sumsum
  • sumsum ::= productproduct | sum+productsum+product | sumproductsum-product
  • productproduct ::= multipliermultiplier | productmultiplierproduct*multiplier | product/multiplierproduct/multiplier
  • multipliermultiplier ::= n | numbernumber | f( arithmExprarithmExpr )
  • numbernumber ::= 012... 327670|1|2|...\ |32767

The whitespaces in a operatorSequenceoperatorSequence are optional.

Thus, we have a function, in which body there are two kinds of operators. There is the operator "return arithmExprarithmExpr ;" that returns the value of the expression as the value of the function, and there is the conditional operator "if ( logicalExprlogicalExpr ) return arithmExprarithmExpr ;" that returns the value of the arithmetical expression when and only when the logical expression is true. Guaranteed that no other constructions of C++ language — cycles, assignment operators, nested conditional operators etc, and other variables except the nn parameter are used in the function. All the constants are integers in the interval [0..32767][0..32767] .

The operators are performed sequentially. After the function has returned a value other operators in the sequence are not performed. Arithmetical expressions are performed taking into consideration the standard priority of the operations. It means that first all the products that are part of the sum are calculated. During the calculation of the products the operations of multiplying and division are performed from the left to the right. Then the summands are summed, and the addition and the subtraction are also performed from the left to the right. Operations ">" (more), "<" (less) and "==" (equals) also have standard meanings.

Now you've got to pay close attention! The program is compiled with the help of 1515 -bit Berland C++ compiler invented by a Berland company BerSoft, that's why arithmetical operations are performed in a non-standard way. Addition, subtraction and multiplication are performed modulo 3276832768 (if the result of subtraction is negative, then 3276832768 is added to it until the number belongs to the interval [0..32767][0..32767] ). Division "/" is a usual integer division where the remainder is omitted.

Examples of arithmetical operations:

Guaranteed that for all values of nn from 00 to 3276732767 the given function is performed correctly. That means that:

1. Division by 00 never occures.

2. When performing a function for the value n=Nn=N recursive calls of the function ff may occur only for the parameter value of 0,1,...,N10,1,...,N-1 . Consequently, the program never has an infinite recursion.

3. As the result of the sequence of the operators, the function always returns a value.

We have to mention that due to all the limitations the value returned by the function ff is independent from either global variables or the order of performing the calculations of arithmetical expressions as part of the logical one, or from anything else except the value of nn parameter. That's why the ff function can be regarded as a function in its mathematical sense, i.e. as a unique correspondence between any value of nn from the interval [0..32767][0..32767] and a value of f(n)f(n) from the same interval.

Given the value of f(n)f(n) , and you should find nn . If the suitable nn value is not unique, you should find the maximal one (from the interval [0..32767][0..32767] ).

输入格式

The first line has an integer f(n)f(n) from the interval [0..32767][0..32767] . The next lines have the description of the function ff . In the description can be found extra spaces and line breaks (see the examples) which, of course, can’t break key words int, if, return and numbers. The size of input data can’t exceed 100100 bytes.

输出格式

Output a single number — the answer to the problem. If there’s no answer, output "-1" (without quotes).

输入输出样例

  • 输入#1

    17
    int f(int n)
    {
    if (n &lt; 100) return 17;
    if (n &gt; 99) return 27;
    }
    

    输出#1

    99
    
  • 输入#2

    13
    int f(int n)
    {
    if (n == 0) return 0;
    return f(n - 1) + 1;
    }
    

    输出#2

    13
  • 输入#3

    144
    int f(int n)
    {
    if (n == 0) return 0;
    if (n == 1) return n;
    return f(n - 1) + f(n - 2);
    }

    输出#3

    24588
    
首页