C. Plus and Square Root time limit per test 2 seconds memory limit per test megabytes input standard input output Stan Dard output
ZS The coder is playing a game. There is a number displayed on the screens and there are the buttons, ' + ' (plus) and ' (square root). Initially, the number 2 is displayed on the screen. There is n + 1 levels in the game and ZS the coder start at the Level 1.
When ZS the coder was at level K, he can:press the ' + ' button. This increases the number on the screens by exactly K. So, if the number is on the screen is x, it becomes x + K press the "button. The number on the screen is x. After pressing this button, the number becomes. After that, the ZS-the coder levels up, so he current level becomes K + 1. The This button can was pressed when X was a perfect square, i.e. x = m2 for some positive integer m.
Additionally, after each move, if ZS the coder are at level K, and the number on the screen was m, then M must be a multiple of K. Note that this condition was only checked after performing the press. For example, if ZS the coder are at level 4 and current number is, he presses the "button and the number turns into 1 0. Note that at this moment, divisible to 4, but this press is still valid, and because after it, ZS the coder was at Level 5, and are divisible by 5.
ZS The coder needs your help on beating the Game-he wants to reach level n + 1. In and words, he needs to press the "button n times. Help him determine the number of times he should press the ' + ' button before pressing the "button at each level.
Please note this ZS the coder wants to find just any sequence of presses allowing him to reach level n + 1, but not necess Arily a sequence minimizing the number of presses. Input
The first and only line of the input contains a single integer n (1≤n≤100), denoting. ZS The coder wants to re ACH level n + 1. Output
Print n non-negative integers, one per line. I-th of them should is equal to the number of times that ZS the coder needs to press the ' + ' button before pressing the "Button at level I."
Each number in the output should is not exceed 1018. However, the number on the screen can greater than 1018.
It is guaranteed, at least one solution exists. If There is multiple solutions, print any of them. Examples input
3
Output
46
Input
2
Output
999999999999999998
44500000000
Input
4
Output
2
97
Note
In the first sample case:
On the first level, ZS the coder pressed the ' + ' button (and the number on screen is initially 2), so the Numbe R became 2 + 14 1 = 16. Then, ZS the coder pressed the "button, and the number became.
After then, on the second level, ZS pressed the ' + ' button for the Times, so the number becomes 4 + 16 2 = 36. Then, ZS pressed the "button, levelling up and changing the number into.
After this, on the third level, ZS pressed the ' + ' button, the number becomes 6 + 46 3 = 144. Then, ZS pressed the "button, levelling up and changing the number into.
Note that's indeed divisible by 4, so ZS the coder can reach level 4.
Also, note that pressing the ' + ' button ten times on the third level before levelling up does don't work, because the Numbe R becomes 6 + 10 3 = $, and when the "button is pressed, the number becomes and ZS the coder are at level 4. However, 6 is not divisible by 4 now, so the is not a valid solution.
In the second sample case:
On the first level, ZS the coder pressed the ' + ' button 999999999999999998 times (and the number on screen is initially 2), so the number became 2 + 999999999999999998 1 = 1018. Then, ZS the coder pressed the "button, and the number became.
After then, on the second level, ZS pressed the ' + ' button 44500000000 times, so the number becomes 109 + 44500000000 2 = 9 1010. Then, ZS pressed the "button, levelling up and changing the number into.
Note that 300000 are a multiple of 3, so ZS the coder can reach level 3.
Source
Codeforces Round #372 (Div. 2)
My Solution
Number theory, equation, simplification, push formula
First, at level I, RES is the number on the current screen, i+1 is the target, K is the answer
Then there is res + k * i = (i + 1) * (i + 1) * x *x
K * i = (i + 1) * (i + 1) * x *x-res;
This is O (n^2), obviously not.
K= ((i + 1) * (i + 1) * x *x-res)/I;
Later found res can be divisible by I, then I want to put in
K = (i + 1) * (i + 1) * x *x/i-res/i;
This is finally understood x = = I just fine
Ans[i] = k = (i + 1) * (i + 1) * i-res/i;
and update res = (i + 1) * I;
Complexity O (N)
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 8;
LL ANS[MAXN];
int main ()
{
#ifdef LOCAL
freopen ("C.txt", "R", stdin);
Freopen ("C.out", "w", stdout);
int T = 1;
while (t--) {
#endif//LOCAL
Ios::sync_with_stdio (false); Cin.tie (0);
LL res = 2;
for (LL i = 1; i < MAXN; i++) {
if (((i + 1) * (i + 1)-res) > 0 && ((i + 1) * (i + 1)-res)% i = = 0) {
ans[i] = ((i + 1) * (i + 1)-res)/I;
res = i + 1;
}
else{
Ans[i] = (i + 1) * (i + 1) * i-res/i;
res = (i + 1) * I;
cout << res << endl;
}
}
cout << Endl;
int n;
CIN >> N;
for (int i = 1; I <= n; i++) cout << ans[i] << "\ n";
#ifdef LOCAL
cout << endl;
}
#endif//LOCAL
return 0;
}
Thank you!
&n