zoukankan      html  css  js  c++  java
  • 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;
    }



  • 相关阅读:
    《Java面向对象编程》
    大学计算机基础考试系统(CS)
    企业物资管理系统
    IP.21出现的错误
    假如你的年龄超过了23···
    如何经营婚姻
    一张舞女图测试你的左右脑切换能力【我看到左右都转呢~】
    无法加载DLL(oci.dll)
    mongodb安装信息及有关命令
    loaded the "controller" nib but the view outlet was not set.问题解决
  • 原文地址:https://www.cnblogs.com/moonbay/p/2228125.html
Copyright © 2011-2022 走看看