其他分享
首页 > 其他分享> > UVA11554 Hapless Hedonism【数学计算+大数】

UVA11554 Hapless Hedonism【数学计算+大数】

作者:互联网

Bob is a world-renowned stick collector. His most prized stick possessions include:
• an Arctic Redwood branch from a hike near Dawson City,
• a Desert Pine stick from a visit to the Grand Canyon, and
• a Chinese Arbour twig from an adventure into Tibet.
    Bob collects sticks in a peculiar way. He will only accept a new stick into his collection if its length is exactly length n + 1 cm where n is the number of sticks currently in his collection. This implies his collection of n sticks contains exactly one stick of length 1 cm through n cm.
    One day Alice visited Bob to inspect his stick collection (upon Bob’s insistence of course). Alice wasn’t particularly interested in Bob’s excessive descriptions and needed a quick conversation changer. Cleverly, she posed the following question to Bob: “If you are allowed to take any 3 sticks from your collection, how many different triangles can you make?”
    Can you help Bob answer the question so he can get back to telling Alice about his sticks?
Input
The input will begin with t (1 ≤ t ≤ 1000), the number of test cases. Each test case will contain an integer n (3 ≤ n ≤ 1000000), the number of sticks in Bob’s collection. (Recall if Bob has n sticks, then he has exactly one stick of each of the lengths from 1 cm through n cm.)
Output
For each test case, output on a line the number of different triangles you can make with Bob’s sticks. Triangles X and Y are different if there is at least one stick in X that is not in Y . A triangle has area strictly greater than 0.
Sample Input
3
3
4
10
Sample Output
0
1
50

问题链接UVA11554 Hapless Hedonism
问题简述:(略)
问题分析:数学计算中的大数问题。经过推导得fx=(x-2)*(x-2)/4,gx=0(x=1,2,3),gx=gx-1+fx(x>3)。需要用到128位整数计算。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA11554 Hapless Hedonism */

#include <bits/stdc++.h>

using namespace std;

typedef __int128 INT128;
const int N = 1000000;
INT128 g[N + 1];

void printi128(INT128 n)
{
    if (n < 0) {putchar('-'); printi128(-n);}
    else if (n < 10) {putchar(n % 10 + '0');}
    else {printi128(n / 10); putchar(n % 10 + '0');}
}

int main()
{
    g[0] = g[1] = g[2] = g[3] = 0;
    for (INT128 i = 4; i <= N; i++)
        g[i] = g[i - 1] + (i - 2) * (i - 2) / 4;

    int t, n;
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        printi128(g[n]);
        putchar('\n');
    }

    return 0;
}

AC的C++语言程序如下:

/* UVA11554 Hapless Hedonism */

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    scanf("%d", &t);
    while (t--) {
        int n;
        unsigned long long ans = 0;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) {
            double p = n - (i + i + 1);
            if (p > 0) ans += (unsigned long long)(p * (p + 1)) / 2;
        }
        printf("%lld\n", ans);
    }

    return 0;
}

标签:his,sticks,int,UVA11554,stick,collection,Hedonism,Bob,Hapless
来源: https://blog.csdn.net/tigerisland45/article/details/116539471