Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <cstring>
  4.  
  5. #include <vector>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9. typedef long double ld;
  10. typedef long long llint;
  11.  
  12. const ld eps = 1e-9;
  13. const int deg = (1 << 17)+1;
  14. const int shift = 10001;
  15.  
  16. struct comp {
  17.    ld x, y;
  18.    comp() { x = y = 0.0; }
  19.    comp( ld X, ld Y ) { x = X; y = Y; }
  20.    comp( ld phi ) { x = cos( phi ); y = sin( phi ); }
  21. };
  22.  
  23. int N;
  24. comp p[ deg ]; int q[ deg ], r[ deg ];
  25. comp s[ deg ], c[ deg ];
  26. comp tmp[ deg ];
  27.  
  28. inline comp operator+( comp a, comp b ) { return comp( a.x+b.x, a.y+b.y ); }
  29. inline comp operator-( comp a, comp b ) { return comp( a.x-b.x, a.y-b.y ); }
  30. inline comp operator*( comp a, comp b ) { return comp( a.x*b.x-a.y*b.y, a.x*b.y+a.y*b.x ); }
  31.  
  32. void fft( comp *a, int x, int n, int t ) {
  33.    if( n == 1 ) return;
  34.  
  35.    int h = ( n >> 1 );
  36.    comp w( 1.0, 0.0 ), cw;
  37.    comp W( t*2.0*M_PI/n );
  38.  
  39.    for( int i = 0; i < n; ++i ) tmp[i] = a[x+i];
  40.    for( int i = 0; i < n; i += 2 ) a[x+(i>>1)] = tmp[i];
  41.    for( int i = 1; i < n; i += 2 ) a[x+h+(i>>1)] = tmp[i];
  42.  
  43.    fft( a, x, h, t );
  44.    fft( a, x+h, h, t );
  45.  
  46.    for( int i = 0; i < n; ++i ) tmp[i] = a[x+i];
  47.    for( int i = 0; i < h; ++i ) {
  48.       cw = w*tmp[i+h];
  49.       a[x+i] = tmp[i] + cw;
  50.       a[x+h+i] = tmp[i] - cw;
  51.       w = w*W;
  52.    }
  53. }
  54.  
  55. inline void conv( comp *a, comp *b, int n ) {
  56.    for( int i = 0; i < n; ++i ) c[i] = b[i];
  57.  
  58.    fft( a, 0, n, +1 );
  59.    fft( c, 0, n, +1 );
  60.  
  61.    for( int i = 0; i < n; ++i )
  62.       a[i] = a[i]*c[i];
  63.  
  64.    fft( a, 0, n, -1 );
  65.  
  66.    for( int i = 0; i < n; ++i ) {
  67.       a[i].y = 0.0;
  68.       a[i].x = round( a[i].x/n+0.2 );
  69.    }
  70. }
  71.  
  72. inline void solve( comp *p, int *q, int *r, int n ) {
  73.    for( int i = 0; i < n; ++i ) s[i] = p[i];
  74.  
  75.    conv( s, p, n );
  76.  
  77.    for( int i = 0; i < n; ++i )
  78.       s[i].x -= 3*q[i];
  79.  
  80.    conv( s, p, n );
  81.  
  82.    for( int i = 0; i < n; ++i )
  83.       s[i].x += 2*r[i];
  84. }
  85.  
  86. int main( void )
  87. {
  88.    scanf( "%d", &N );
  89.  
  90.    for( int i = 0; i < N; ++i ) {
  91.       int a; scanf( "%d", &a );
  92.       p[a+shift] = comp( 1.0, 0.0 );
  93.       q[2*a+2*shift] = 1;
  94.       r[3*a+3*shift] = 1;
  95.    }
  96.  
  97.    solve( p, q, r, 1 << 16 );
  98.  
  99.    for( int i = 0; i < deg; ++i ) {
  100.       if( fabs( s[i].x ) < eps ) continue;
  101.       printf( "%d : %lld\n", i-3*shift, (llint)( s[i].x+0.1 )/6 );
  102.    }
  103.  
  104.    return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement