hdu 2576 同余公式

题目蛮简单的,但是纠结了好久。原因倒不是推不出公式,而是推出公式以后不好办,因为公式是(n+2)*(n+1)*n/6,而n最大为10000000,这样的话即使用long long 也存不下。记得同余公式里关于除法的好像只有逆元的一些,上网找了一下,确定没有。于是只有自己继续推了。最后,自己终于证明了如下公式:

不妨设a > b,r为a、b的公约数,则有

(a/r)%(b/r) = (a%b)/r。

不知道这个公式之前有没有人推出来过,反正自己挺有成就感的。其实也很好证明,如下:

设(a/r)%(b/r) = z,则一定有且仅有一个k满足(a/r) = k(b/r) + z。

从而得a = kb + zr

于是有a%b=(kb+zr)%b=((kb)%b+zr%b)%b=(zr%b)%b

由上述假设易知z <(b/r),从而得zr<b

故(zr%b)%b = zr

故a%b=zr

从而

(a%b)/r = z = (a/r)%(b/r)

证毕。

有了公式,这题就是水题了。

/*
* hdu2576/win.cpp
* Created on: 2011-10-28
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
const int MOD = 20090524 * 6;

int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
int T, n;
long long ans;
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
ans = n;
ans *= n + 1;
ans %= MOD;
ans *= n + 2;
ans %= MOD;
ans /= 6;
printf("%d\n", (int) ans);
}
return 0;
}



转载于:https://www.cnblogs.com/moonbay/archive/2011/10/28/2228125.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值