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;
}



posted @ 2011-10-28 20:55  moonbay  阅读(177)  评论(0编辑  收藏  举报