目录

P8692-蓝桥杯-2019-国-C-数正方形-输出取模余数

P8692 [蓝桥杯 2019 国 C] 数正方形–输出取模余数

题目

https://i-blog.csdnimg.cn/direct/b62b85283e1d4b4bb765e4664e3971e6.png

解析

这是数学题 T^T

不过对于这种题目都说了结果非常大的题,得开long long【 不开long long 见ZZ

然后还有点要说的就是,为了缩减计算量,这种要求结果输出模后余数的题,我们可以在计算过程中就对其取模。【如:下列代码中在for循环中的ans+=时取模】

代码

#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <algorithm>
#include <math.h>
#include <queue>
#include <climits>  // 包含INT_MAX常量
#include <cctype>
using namespace std;
int n;
long long ans;

int main() {
	cin >> n;
	for (int i = 1; i < n; i++) {
		ans += i*(long long) (n - i) * (n - i) ;
	//这一步是最巧妙的,得学!
		ans%=1000000007;
	}
	cout << ans;
	return 0;
}