[BZOJ3771] Triple
Question Description
Let's tell a sad story. Once upon a while a poor woodcutter was chopping wood from the river. At this time a water god appeared in the river, took his axe, and said, "This axe is not yours?" The woodcutter saw: "Yes, yes!" The God of the gods threw the axe aside and picked up a thing to ask: "This axe, isn't it yours?" "The woodcutter could not see clearly, but was afraid to really be his own axe, had to answer again:" Yes Ah! The God of the gods threw something on one side of his hand and picked up a third thing and asked, "This axe, isn't it yours?" The woodcutter could not see it clearly, but he felt that he could not cut the wood. So he once again replied: "Yes Ah!" That's true! The God of the gods looked at him and laughed, "Look at you now, it's ugly!" "Then it disappeared. The woodcutter felt that he had not cut the firewood today, but he had lost an axe to the god of the gods. So he was going to go home and change an axe. When he came home, he found out that the real thing about the pit-daddy was just beginning. The God of the gods was holding his axe. But it's not necessarily the one he took out, it's probably the god of the gods who didn't know how to sneak away from his house. In other words, the god of the gods may have taken one of his, two or three axes. The woodcutter thought it was a bad day, but no matter how long it took. He wants to count his losses. Every axe of the woodcutter has a value, and different axes have different values. The total loss is the value of the axe lost and. He wanted to calculate a few possible scenarios for each possible total loss. Note: If the god of the gods took two axes A and B, (A, A, a) and (B,a) as a scheme. When the three axes are taken, (A,b,c), (B,c,a), (C,a,b), (C,b,a), (B,a,c), (a,c,b) as a scheme.
Input
The first line is an integer n, which indicates that there are n axes. The next n rows are entered in ascending order of n digits AI, indicating the value of each axe.
Output
Several rows, in ascending order for all possible total losses, output one row x Y,x as the loss value, and Y is the scheme number.
Input example
4 4 5 6 7
Output example
4 15 16 17 19 1Ten 1 One 2 A 1 - 1 the 1 - 1 - 1 - 1
Data size and conventions
All data meet: ai<=40000
Exercises
This problem uses a function called the mother of the thing. In fact, is to make a polynomial a, where the coefficient of Xi is the number I occurrences, then it is easy to find that two a multiplication can be obtained two number plus and the composition of each number is not considered the order and repetition of the method, specifically, two numbers plus and N for the scheme is bn the number of occurrences of n is , it is easy to find that this is a convolution form, so it is two polynomial a multiplied. Note the need to eliminate duplication and improper order, so you also need to set up a polynomial A2, where xi represents the number of occurrences of I/2, then (A*A-A2)/2 is the number of two to consider the sequence and the repetition of the scheme count. The same three number can also be self-yy to launch.
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <stack > #include <vector> #include <queue> #include <cstring> #include <string> #include <map > #include <set>using namespace std; const int buffersize = 1 << 16;char buffer[buffersize], *head, *tail;inline char Getchar () {if (Head = = Tail) { int L = fread (buffer, 1, buffersize, stdin); Tail = (Head = buffer) + L; } return *head++;} int read () {int x = 0, f = 1; char c = Getchar (); while (!isdigit (c)) {if (c = = '-') f =-1; c = Getchar ();} while (IsDigit (c)) {x = x * + C-' 0 '; c = Getchar ();} return x * f;} #define MAXN 240010#define LL long longconst double pi = ACOs ( -1.0); struct Complex {double A, B; Complex () {a = b = 0.0;} Complex operator + (const complex& t) const {Complex ans; ANS.A = a + t.a; ans.b = b + t.b; return ans; } Complex operator + = (ConSt complex& t) {*this = *this + t; return *this; } Complex Operator-(const complex& t) const {Complex ans; ANS.A = A-T.A; ans.b = b-t.b; return ans; } Complex operator * (const complex& t) const {Complex ans; ANS.A = A * t.a-b * T.B; ans.b = A * t.b + b * T.A; return ans; } Complex operator * (const double& t) const {Complex ans; ANS.A = A * t; ans.b = b * t; return ans; } Complex operator *= (const complex& t) {*this = *this * t; return *this; }} A[MAXN], A2[MAXN], A3[MAXN], Ans[maxn];int N, M; int ord[maxn];void FFT (Complex a[], int n, int tp) {for (int i = 0; i < n; i++) if (I < ord[i]) swap (A[i], a[ord[i ]]); for (int i = 1; i < n; I <<= 1) {Complex W, wn; wn.a = cos (pi/i); wn.b = sin (pi/i) * TP; for (int j = 0; J < n; j + = (I << 1)) {W.A = 1.0; w.b = 0.0; for (int k = 0; k < i; k++) {Complex T1 = a[j+k], t2 = w * A[j+k+i]; A[J+K] = t1 + T2; A[j+k+i] = t1-t2; W *= WN; }}} if (TP < 0) for (int i = 0; I <= N; i++) a[i].a = (int) (a[i].a/n +. 5); return;} int main () {int t = read (); for (int i = 1; I <= t; i++) {int x = read (); A[X].A + = 1.0; A2[X<<1].A + = 1.0; A3[X*3].A + = 1.0; n = Max (n, x); } m = n * 3; int L = 0; for (n = 1; n <= m; n <<= 1) l++; for (int i = 0; i < n; i++) ord[i] = (ord[i>>1] >> 1) | ((I & 1) << L-1); FFT (A, N, 1); FFT (A2, N, 1); FFT (A3, N, 1); for (int i = 0; I <= N; i++) ans[i] = A[i] + (a[i] * a[i]-a2[i]) *. 5 + (A[i] * a[i] * A[i]-a[i] * A2[i] * 3.0 + a3[ I] * 2.0) * (1.0/6.0); FFT (ans, N,-1); for (int i = 1; I <= m; i++) if ((int) ans[i].a) printf ("%d%d\n", I, (int) ans[i].a); ReTurn 0;}
[BZOJ3771] Triple