HDU 4466 Triangle

传送门
我们枚举 x=a+b+c ,枚举 a a<=b<=c
c=xab 代入下面的不等式中,可以得到 b 的范围
a+b>c
a+c>b
b+c>a
记上面的方案数为 f(x)
a,b,c 互质的方案数为 g(x)
g(x)=f(x)(g(y)y|x, y<x

#include<bits/stdc++.h>
using namespace std;
#define prt(k) cout<<#k" = "<<k<<endl
typedef long long ll;
const int mod = 1e9+7;

const int N = 5000500;
int n;
int p2[N];
ll dp[N];
int count(int x)
{
    int tot = 0;
    for (int a=1;a<=x/3;a++) {
        int l = max(x/2 - a + 1, a);
        int r = (x-a)/2;
        if (l <= r) tot = (tot + r-l+1) % mod;
    }
    return tot;
}
ll get(int x)
{
    if (x < 3) return 0;
    if (dp[x] != -1) return dp[x];
    ll tot = count(x);
    for (int i=2;i<=sqrt(x+0.5);i++) if (x%i==0) {
        tot = (tot - get(i) + mod) % mod;
        if (i - x / i && x / i != n)
            tot = (tot - get(x / i) + mod) % mod;
    }
    return dp[x] = tot;
}
int main()
{
    int ca = 1;
    p2[0] = 1;
    for (int i=1;i<N;i++) p2[i] = p2[i-1] * 2 % mod;
    memset(dp, -1, sizeof dp);
    while (scanf("%d", &n)==1)
    {
        ll ans = 0;
        for (int p=1;p<=sqrt(n+0.5);p++) if (n%p==0) {
            ans = (ans + 1ll * p2[n/p-1]*get(p)) % mod;
            if (p != n/p)
                ans = (ans + 1ll * p2[p-1]*get(n / p) % mod) % mod;
        }
        printf("Case %d: %I64d\n", ca++, ans);
    }
    return 0;
}

你可能感兴趣的:(数学,C语言)