这个公式推导过程是看的这位大牛的http://blog.csdn.net/bigbigship/article/details/49123643

扩展欧几里德求模的逆元方法:

#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
typedef long long ll;
const ll mod = 1e9 + ;
ll exgcd(ll a, ll b, ll &x, ll &y)
{
if (b == )
{
x = ;
y = ;
return a;
}
ll r = exgcd(b, a % b, x, y);
ll t = x % mod;
x = y % mod;
y = ((t - a / b * y) % mod + mod) % mod;
return r;
}
int main()
{
ll x, y;
ll inv2, inv4, inv6;
exgcd(, mod, inv2, y);
exgcd(, mod, inv4, y);
exgcd(, mod, inv6, y);
ll T, n;
printf("%lld, %lld, %lld\n", inv2, inv4, inv6);
scanf("%lld", &T);
while (T--)
{
scanf("%lld", &n);
n %= mod;
ll ans = (n * n % mod + n) % mod * n % mod * n % mod * inv2 % mod;
ans += ((n * (n + ) % mod) * n % mod * (n + ) % mod * inv2 % mod) % mod;
ans += (n + ) * n % mod * (n + ) % mod * ( * n + ) % mod * inv6 % mod;
ans =(((ans - n * n % mod * (n + ) % mod * (n + ) % mod * inv4 % mod + mod) % mod + mod) % mod);
printf("%lld\n", ans);
}
return ;
}

费马小定理求模的逆元法

#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
typedef long long ll;
const ll mod = 1e9 + ;
ll power_mod(ll a, ll b, ll mod)
{
ll ans = ;
while (b)
{
if (b & ) ans = ans * a % mod;
a = a * a % mod;
b >>= ;
}
return ans;
}
int main()
{
ll inv2 = power_mod(, mod - , mod);
ll inv4 = power_mod(, mod - , mod);
ll inv6 = power_mod(, mod - , mod);
ll T, n;
scanf("%lld", &T);
while (T--)
{
scanf("%lld", &n);
n %= mod;
/*ll ans = (n * n % mod + n) % mod * n % mod * n % mod * inv2 % mod;
ans += ((n * (n + 1) % mod) * n % mod * (n + 1) % mod * inv2 % mod) % mod;
ans += (n + 2) * n % mod * (n + 1) % mod * (2 * n + 1) % mod * inv6 % mod;
ans =(((ans - n * n % mod * (n + 1) % mod * (n + 1) % mod * inv4 % mod + mod) % mod + mod) % mod);*/
ll ans = n * (n + ) % mod * n % mod * n % mod * inv2 % mod;
ans = (ans + n * (n + ) % mod * n % mod * (n + ) % mod * inv2 % mod) % mod;
ans = (ans + n * (n + ) % mod * (n + ) % mod * ( * n + ) % mod * inv6 % mod) % mod;
ans = (ans - n * n % mod * (n + ) % mod * (n + ) % mod * inv4 % mod + mod) % mod;
printf("%lld\n", ans);
}
return ;
}

ZOJ 3903 Ant(公式推导)的更多相关文章

  1. ZOJ 3903 Ant(数学,推公示+乘法逆元)

    Ant Time Limit: 1 Second      Memory Limit: 32768 KB There is an ant named Alice. Alice likes going ...

  2. ZOJ 3903 Ant ZOJ Monthly, October 2015 - A

    Ant Time Limit: 1 Second      Memory Limit: 32768 KB There is an ant named Alice. Alice likes going ...

  3. zoj 1199 几何公式推导

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=199 Point of Intersection Time Limit:  ...

  4. HZNU Training 1 for Zhejiang Provincial Collegiate Programming Contest

    赛后总结: TJ:今天我先到实验室,开始看题,一眼就看了一道防AK的题目,还居然觉得自己能做wwww.然后金姐和彭彭来了以后,我和他们讲了点题目.然后金姐开始搞dfs,我和彭彭看榜研究F题.想了很久脑 ...

  5. zoj 1671 Walking Ant【简单bfs】

    Walking Ant Time Limit: 2 Seconds      Memory Limit: 65536 KB Ants are quite diligent. They sometime ...

  6. zoj 1671 Walking Ant

    Walking Ant Time Limit: 2 Seconds      Memory Limit: 65536 KB Ants are quite diligent. They sometime ...

  7. HDU 4870 Rating(概率、期望、推公式) && ZOJ 3415 Zhou Yu

    其实zoj 3415不是应该叫Yu Zhou吗...碰到ZOJ 3415之后用了第二个参考网址的方法去求通项,然后这次碰到4870不会搞.参考了chanme的,然后重新把周瑜跟排名都反复推导(不是推倒 ...

  8. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  9. ZOJ 3279-Ants(线段树)

    传送门:zoj 3279 Ants Ants Time Limit: 2 Seconds      Memory Limit: 32768 KB echo is a curious and cleve ...

随机推荐

  1. 通过javascript实现1~100内能同时被2和3整除的数并生成如下表格

    请通过javascript实现1~100内能同时被2和3整除的数并生成如下表格: <!DOCTYPE html><html lang="en"><he ...

  2. bzoj 1191: [HNOI2006]超级英雄Hero 并查集 || 匈牙利算法

    1191: [HNOI2006]超级英雄Hero Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1804  Solved: 850[Submit][S ...

  3. new 与override 区别

    using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace Newover ...

  4. 转 -- MVC+EF easyui dataGrid 动态加载分页表格

    首先上javascript的代码 <script type="text/javascript"> $(function () { LoadGrid(); }) //加载 ...

  5. 从 3 个 IT 公司里学到的 57 条经验

    自1999年起我就开始发掘一些科技公司,并帮助它们运营.下面是从干这行中得到的57条经验.我可以列出更多,但恐怕会令你厌烦. 1.做你个人有热情的事情.你是你自己最好的民意代表. 2.用户体验很重要. ...

  6. 曾经的岁月之maya

  7. 解决外部机器通过VM内ubuntu IP 无法访问vm内web服务的问题

    1. 2.i root@ubuntu:/home/udapeng# ifconfig root@ubuntu:/www/sentry# nano uwsgi.ini root@ubuntu:/www/ ...

  8. Bzoj 1222: [HNOI2001]产品加工 动态规划

    1222: [HNOI2001]产品加工 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 486  Solved: 298[Submit][Status ...

  9. 386. Lexicographical Numbers

    用DFS来做,先弄开头是1的,再弄开头是1的里面开头是1的,再开头是1的里面开头是1的里的开头是1的,再... 是吧-- 比N大了BREAK就行. 注意第一个循环是1-9,往后的循环是0-9. pub ...

  10. Oracle RAC中的投票算法

    RAC集群中有三台机器,A,B,C A,B,C都会有3票,假设这是A的心跳线出现问题,整个RAC集群就划分为两个paritition, 一个是只有A的partition,一个是B,C组成的partit ...