ZOJ 3903 Ant(公式推导)

这个公式推导过程是看的这位大牛的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 + 7;
ll exgcd(ll a, ll b, ll &x, ll &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        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(2, mod, inv2, y);
    exgcd(4, mod, inv4, y);
    exgcd(6, 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 + 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);
        printf("%lld\n", ans);
    }
    return 0;
}

费马小定理求模的逆元法

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
ll power_mod(ll a, ll b, ll mod)
{
    ll ans = 1;
    while (b)
    {
        if (b & 1) ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}
int main()
{
    ll inv2 = power_mod(2, mod - 2, mod);
    ll inv4 = power_mod(4, mod - 2, mod);
    ll inv6 = power_mod(6, mod - 2, 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 + 1) % mod * n % mod * n % mod * inv2 % mod;
        ans = (ans + n * (n + 1) % mod * n % mod * (n + 1) % mod * inv2 % mod) % mod;
        ans = (ans + n * (n + 1) % mod * (n + 2) % mod * (2 * n + 1) % mod * inv6 % mod) % mod;
        ans = (ans - n * n % mod * (n + 1) % mod * (n + 1) % mod * inv4 % mod + mod) % mod;
        printf("%lld\n", ans);
    }
    return 0;
}
时间: 2024-12-07 15:05:18

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

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

Ant Time Limit: 1 Second      Memory Limit: 32768 KB There is an ant named Alice. Alice likes going hiking very much. Today, she wants to climb a cuboid. The length of cuboid's longest edge is n, and the other edges are all positive integers. Alice's

zoj 3903 Ant(推公式,逆元)

Ant Time Limit: 1 Second      Memory Limit: 32768 KB There is an ant named Alice. Alice likes going hiking very much. Today, she wants to climb a cuboid. The length of cuboid's longest edge is n, and the other edges are all positive integers. Alice's

ZOJ 3905 Ant ZOJ Monthly, October 2015 - C

Cake Time Limit: 4 Seconds      Memory Limit: 65536 KB Alice and Bob like eating cake very much. One day, Alice and Bob went to a bakery and bought many cakes. Now we know that they have bought n cakes in the bakery. Both of them like delicious cakes

ZOJ 1671:Walking Ant【bfs】

Walking Ant Time Limit: 2 Seconds      Memory Limit: 65536 KB Ants are quite diligent. They sometimes build their nests beneath flagstones. Here, an ant is walking in a rectangular area tiled with square flagstones, seeking the only hole leading to h

zoj 1671 Walking Ant

Walking Ant Time Limit: 2 Seconds      Memory Limit: 65536 KB Ants are quite diligent. They sometimes build their nests beneath flagstones. Here, an ant is walking in a rectangular area tiled with square flagstones, seeking the only hole leading to h

Zoj 1671 Walking Ant(BFS+优先队列||记忆化搜索)

Walking Ant Time Limit: 2 Seconds Memory Limit: 65536 KB 点击打开链接 Ants are quite diligent. They sometimes build their nests beneath flagstones. Here, an ant is walking in a rectangular area tiled with square flagstones, seeking the only hole leading to

zoj 3203 Light Bulb(公式推导|三分法)(简单)

Light Bulb Time Limit: 1 Second      Memory Limit: 32768 KB Compared to wildleopard's wealthiness, his brother mildleopard is rather poor. His house is narrow and he has only one light bulb in his house. Every night, he is wandering in his incommodio

zoj 1671 Walking Ant【简单bfs】

Walking Ant Time Limit: 2 Seconds      Memory Limit: 65536 KB Ants are quite diligent. They sometimes build their nests beneath flagstones. Here, an ant is walking in a rectangular area tiled with square flagstones, seeking the only hole leading to h

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i