这个公式推导过程是看的这位大牛的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. iOS:翻页效果

    // // main.m // Hello // // Created by lishujun on 14-8-28. // Copyright (c) 2014年 lishujun. All rig ...

  2. BZOJ 3563 DZY Loves Chinese

    Description 神校XJ之学霸兮,Dzy皇考曰JC. 摄提贞于孟陬兮,惟庚寅Dzy以降. 纷Dzy既有此内美兮,又重之以修能. 遂降临于OI界,欲以神力而凌♂辱众生. 今Dzy有一魞歄图,其上 ...

  3. mysql表分区、查看分区

    原文地址:http://blog.csdn.net/feihong247/article/details/7885199 一.       mysql分区简介 数据库分区 数据库分区是一种物理数据库设 ...

  4. lc面试准备:Number of 1 Bits

    1 题目 Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...

  5. WPF 界面布局DockPanel stackPanel WrapPanel 元素内容以及位置控制

    1 DockPanel 1) 默认充满整个窗口. 2) 最后一个出现的部分,默认充满剩余空间. 3) 非最后一个出现的部分,根据其中内容,进行分配空间s 2 StackPanel 实现居左,居右,居中 ...

  6. Volley框架支持HTTPS请求。

    第一次写帖子,嘿嘿. 最近了解到google2013IO大会出了个网络框架,正好项目也需要用到,就看了下. 最后发现接口都是HTTPS的,但是Volley默认是不支持HTTPS,网上找了好久,都没有对 ...

  7. BZOJ1614: [Usaco2007 Jan]Telephone Lines架设电话线

    1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 892  Solved: ...

  8. 出现 HTTP Error 503. The service is unavailable 错误

    今天修改一些配置后重启了IIS,兴高采烈的按下了回车.duang一下,HTTP Error 503. The service is unavailable,蹦出这行字,网站挂了. 没动别的竟然这样了. ...

  9. CSU 1505 酷酷的单词 湖南省赛第十届题目

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1505 题意:技巧题,就是一行字符串中,每个字母出现的次数互不相同,复即为酷的单词. 解题 ...

  10. NDK的安装和下载

    从官网下载NDK 下载页面:https://developer.android.com/ndk/downloads/index.html 从镜像站点下载NDK "大师兄"是一个由腾 ...