HDU 4342

先确定M的大致范围后即可求。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define LL __int64
#define N 60000

LL square[N];
LL spre[N];

void init(){
	spre[0]=0; spre[0]=0;
	for(LL i=1;i<N;i++){
		square[i]=i*i;
		spre[i]=spre[i-1]+i*(2*i+1);
	}
}

int main(){
	init();
	int T,n;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		LL p=(LL)sqrt(n*1.0);
		if(square[p+1]-p-1<n)
		p++;
		LL g=n-(square[p]-p)+square[p];
		LL su=spre[p-1]+p*(g-square[p]+1);
		printf("%I64d %I64d
",g,su);
	}
}

  

原文地址:https://www.cnblogs.com/jie-dcai/p/4148786.html