nefu519
来源:互联网 发布:文本导入mysql 乱码 编辑:程序博客网 时间:2024/06/05 18:55
兴安黑熊在高中学习数学时,曾经知道这样一个公式:f(n)=1^2+2^2+3^2+.......+n^2,这个公式是可以化简的,化简后的结果是啥它却忘记了,也许刚上大二的你能记得。现在的问题是想要计算f(n)对1007取余的值,你能帮帮他吗?
Input
输入数据有多组,每组一个数n. (1<=n <=1,000,000,000).
Output
输出f(n)对1007取余的值。
Sample Input
34100
Sample Output
14301005
2.主要的问题在于除了个6
第一种方法是 n*(n+1)必然可以除二 如果他能除三就除 不能的话 (2n+1)必然可以除
第二种方法是下面这个用逆元做
#include <iostream>
using namespace std;
const long long mod=1007;
void exgcd(long long a,long long b,long long &x,long long &y)
{
if(b==0)
{
x=1;
y=0;
return ;
}
long long x1,y1;
exgcd(b,a%b,x1,y1);
x=y1;
y=x1-(a/b)*y1;
}
int main()
{
int n;
while(cin>>n)
{
long long ans=1;
ans=((n%mod)*((n+1)%mod)*((2*n+1)%mod));
long long x,y;
long long a=6;
exgcd(a,mod,x,y);
ans*=x;
ans%=mod;
cout<<ans<<endl;
}
return 0;
}
阅读全文
0 0
- nefu519
- 10_基本数据类型自动转换_强制转型_类型提升问题
- AttributeError: 'module' object has no attribute 'reader'
- mysql根据经纬度计算距离
- easyui 弹框显示列表
- Linux基础知识-1
- nefu519
- Golang的switch和select用法
- [Err] 1022
- POJ 1308 Is It A Tree? 笔记
- iOS开发算法--希尔排序
- 全志R16的android4.4.2平台点亮gc2145(evb30)版本:V1.1(分色排版)
- Hbase原理、基本概念、基本架构
- python-opencv:图像的位运算
- 快速选择合适的机器学习算法