XIAOHAOYU1 @ 2024-06-28 14:53:45
1.将n拆解成1+2+3+4.。。。。。+x+y(如14=1+2+3+4+4)
2.将前面的一段改成1^2+2^2+3^2.。。。+x^2
3.将剩下的y变成x+1乘上y
4.运用平方和公式,算出步骤二的结果
5.将两个结果相加输出
举个栗子:
14=1+2+3+4+4
1^2+2^2+3^2+4^2=30
4×(4+1)=20
20+30=50
问下大佬这思路行不行QAQ
by XIAOHAOYU1 @ 2024-06-28 14:57:10
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int sum=0,g=0;
while(n>g){
g++;
sum+=g*g;
n-=g;
}
sum+=n*(g+1);
cout<<sum;
return 0;
}
代码附上(已过)
by Andy_hpy @ 2024-08-09 11:03:48
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[100005];
ll f1(ll n,ll s){
if(n<s)return n*s;
if(a[n]!=0)return a[n];
a[n]=s*s+f1(n-s,s+1);
return a[n];
}
ll f(ll n){return f1(n,1);}
int main(){
ll n;
cin>>n;
cout<<f(n);
return 0;
}