zoukankan      html  css  js  c++  java
  • SPOJ 8372. Triple Sums

    8372. Triple Sums

    Problem code: TSUM

    You're given a sequence s of N distinct integers.
    Consider all the possible sums of three integers from the sequence at three different indicies.
    For each obtainable sum output the number of different triples of indicies that generate it.

    Constraints:

    N <= 40000, |si| <= 20000

    Input

    The first line of input contains a single integer N.
    Each of the next N lines contain an element of s.

    Output

    Print the solution for each possible sum in the following format:
    sum_value : number_of_triples

    Smaller sum values should be printed first.

    Example

    Input:

    5
    -1
    2
    3
    0
    5
    Output:
    1 : 1
    2 : 1
    4 : 2
    5 : 1
    6 : 1
    7 : 2
    8 : 1
    10 : 1

    Explanation:
    4 can be obtained using triples ( 0, 1, 2 ) and ( 0, 3, 4 ).
    7 can be obtained using triples ( 0, 2, 4 ) and ( 1, 3, 4 ).

    Note: a triple is considered the same as any of its permutations.


    用一个系数为该位指数在S中出现次数的多项式(P)来表示某位上指数的一次表示方式,那么三次表示方式就是(P^3),然后由于要求三元组的元素在S中的序数不同,容斥原理判重即可。

    #include <bits/stdc++.h>
    using namespace std;
    typedef complex<long double> Complex;
    
    const int maxn = 1 << 17;
    const int base = 20000;
    void DFT(Complex *a, int N, int flag) {
        for(int i = (N >> 1), j = 1, k; j < N; i ^= k, ++j) {
            if(i < j) swap(a[i], a[j]);
            for(k = (N >> 1); i & k; i ^= k, k >>= 1);
        }
        for(int n = 2; n <= N; n <<= 1) {
            Complex Wn = Complex(cos(flag * M_PI * 2 / n), sin(flag * M_PI * 2 / n));
            for(int i = 0; i < N; i += n) {
                Complex W = Complex(1.0, 0.0);
                for(int j = i; j < i + (n >> 1); W = W * Wn, ++j) {
                    Complex u = a[j], v = W * a[j + (n >> 1)];
                    a[j] = u + v;
                    a[j + (n >> 1)] = u - v;
                }
            }
        }
    }
    int a[maxn], b[maxn], c[maxn];
    Complex A[maxn], B[maxn], C[maxn];
    int main() {
        int n;
        scanf("%d", &n);
        for(int i = 1, x; i <= n; ++i) {
            scanf("%d", &x);
            x += base;
            ++a[x];
            ++b[x + x];
            ++c[x + x + x];
        }
        int N = maxn;
        for(int i = 0; i < N; ++i) {
            A[i] = a[i], B[i] = b[i];
        }
        DFT(A, N, 1);
        DFT(B, N, 1);
        for(int i = 0; i < N; ++i) {
            C[i] = A[i] * (A[i] * A[i] - 3.0l * B[i]);
        }
        DFT(C, N, -1);
        for(int i = 0; i < N; ++i) {
            long long ans = ((long long)(C[i].real() / N + 0.5) + 2 * c[i]) / 6;
            if(ans != 0) {
                printf("%d : %lld
    ", i - 60000, ans);
            }
        }
        return 0;
    }

  • 相关阅读:
    HDU 1828 Picture (线段树:扫描线周长)
    [USACO18OPEN] Multiplayer Moo (并查集+维护并查集技巧)
    NOIP2016 天天爱跑步 (树上差分+dfs)
    NOIP2013 华容道 (棋盘建图+spfa最短路)
    NOIP2015 运输计划 (树上差分+二分答案)
    NOIP2018 前流水账
    luogu P2331 [SCOI2005]最大子矩阵
    luogu P2627 修剪草坪
    CF101D Castle
    luogu P2473 [SCOI2008]奖励关
  • 原文地址:https://www.cnblogs.com/hzf-sbit/p/4009455.html
Copyright © 2011-2022 走看看