In a carnival game, there is a huge pyramid of cans with 20232023 rows, numbered in a regular pattern as shown.
If can 9292 is hit initially, then all cans colored red in the picture above would fall.
You throw a ball at the pyramid, and it hits a single can with number n2n2. This causes all cans that are stacked on top of this can to fall (that is, can n2n2 falls, then the cans directly above n2n2 fall, then the cans directly above those cans, and so on). For example, the picture above shows the cans that would fall if can 9292 is hit.
What is the sum of the numbers on all cans that fall? Recall that n2=n×nn2=n×n.
Input
The first line contains an integer tt (1≤t≤10001≤t≤1000) — the number of test cases.
The only line of each test case contains a single integer nn (1≤n≤1061≤n≤106) — it means that the can you hit has label n2n2.
Output
For each test case, output a single integer — the sum of the numbers on all cans that fall.
Please note, that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++). For all valid inputs, the answer will always fit into 64-bit integer type.
Example
input
Copy
10
9
1
2
3
4
5
6
10
1434
1000000
output
Copy
156 1 5 10 21 39 46 146 63145186 58116199242129511
Note
The first test case is pictured in the statement. The sum of the numbers that fall is
12+22+32+52+62+92=1+4+9+25+36+81=156.12+22+32+52+62+92=1+4+9+25+36+81=156.
In the second test case, only the can labeled 1212 falls, so the answer is 12=112=1.
In the third test case, the cans labeled 1212 and 2222 fall, so the answer is 12+22=1+4=512+22=1+4=5.
In the fourth test case, the cans labeled 1212 and 3232 fall, so the answer is 12+32=1+9=1012+32=1+9=10.
In the fifth test case, the cans labeled 1212, 2222, and 4242 fall, so the answer is 12+22+42=1+4+16=21
这个比赛时就差1分钟就做完了很烦】
说一下思路:
先搞一个数组a用来确定这个里面的数字在第几行
1 2 3 4 5 6 7 。。
1 2 2 3 3 3 4 。。
然后你找他们的规律
用一个数组s存放当前的值
比如2在第二行开头他上面是1
4在第三行的看头上面是2
这是你就会发现当i在开头也就是a[i]!=a[i-1]时s[i]=s[i-a[i]+1]+i*i
同理当一个数在行的末尾即a[i]!=a[i+1]是你也可以找到如下规律s[i]=s[i-a[i]]+i*i
之中比较重要的就是一个数既不在开头也不再末尾时
比如5上边是2 和3
二和三上边都是1 那么s[2]=s[1]+2*2 s[3]=s[1]+3*3
如果s[3]=s[2]+s[3]+3*3的话那么s[1]就会算两次所以我们得减去s[1]
推广过来就是s[i]=s[i-a[i]+1]+s[i-a[i]]+i*i-s[i-a[i]-a[i-a[i]]+1]
最后s[i-a[i]-a[i-a[i]]+1]你就先把它看成s[x-a[x]+1]令其中x=i-a[i]然后带进去就行了
#include<iostream>
#include<algorithm>
#include<cstring>//find("string"),rfind("string")
#include<string>//to_string(value)
#include<cstdio>
#include<cmath>
#include<vector>//res.erase(unique(res.begin(), res.end()), res.end())
#include<queue>
#include<stack>
#include<map>
#include<set>//iterator,insert(),erase(),lower/upper_bound(value)/find()return end()
#define int unsigned long long
using namespace std;
int a[1000005],s[1000005];
void wc()
{
int k=1;
for(int i=1;i<=1000000;i++){
for(int j=1;j<=i;j++){
a[k++]=i;
if(k==1000002) return;//1000002时return时因为后面要判断1000000和1000001是否相同
}
}
}
void nm()
{
for(int i=1;i<=1000000;i++){
if(i==1) s[i]=1;
else{
if(a[i-1]!=a[i]) s[i]=s[i-a[i]+1]+i*i;
else if(a[i]!=a[i+1]) s[i]=s[i-a[i]]+i*i;
else s[i]=s[i-a[i]+1]+s[i-a[i]]+i*i-s[i-a[i]-a[i-a[i]]+1];
}
}
}
signed main()
{
int t;
cin>>t;
wc();
nm();
while(t--){
int n;
cin>>n;
cout<<s[n]<<endl;
}
}