Official

A - Weird Function Editorial by en_translator


If you are new to learning programming and do not know where to start, please try Problem A “Welcome to AtCoder” from practice contest. There you can find a sample code for each language.
Also, if you are not familiar to problems in programming contests, we recommend you to try some problems in “AtCoder Beginners Selection” (https://atcoder.jp/contests/abs).
「競プロ典型 90 問」(Typical 90 Problems of Competitive Programming) (https://atcoder.jp/contests/typical90) is a collection of typical 90 competitive programming problems; unfortunately, currently the problem statements are all Japanese. 「C++入門 AtCoder Programming Guide for beginners (APG4b)」(https://atcoder.jp/contests/APG4b) is a C++ tutorial for competitive programmers. Sadly, this is only in Japanese too.


Let’s implement the “function” given in the problem statement as a “function” of a program.

The function \(f(x)=x^2+2x+3\) in the problem statement is something that receives \(x\) and returns \(x^2+2x+3\). Therefore, we want to implement a function that for given \(x\) returns \(x^2+2x+3\).

While the answer can be computed step-by-step as the description of Sample Input, it is simpler to compute it at once and output it right away, just as the following sample code.

Sample code (C++):

#include<bits/stdc++.h>

using namespace std;

int f(int x){return x*x+2*x+3;}

int main(){
  int t;
  cin >> t;
  cout << f(f(f(t)+t)+f(f(t))) << '\n';
  return 0;
}

Note that there are only \(11\) patterns of possible input \(t\) in this problem, so we can compute the answers for them beforehand and directly embed them in the code.

Sample code (C++)

#include<bits/stdc++.h>

using namespace std;

vector<int> res={1371,13926,119027,722502,3286971,11985446,36881331,99400902,241025627,536292966,1111355571};

int main(){
  int t;
  cin >> t;
  cout << res[t] << '\n';
  return 0;
}

posted:
last update: