【HDU】5307 He is Flying【分别统计+NTT】

传送门:【HDU】5307 He is Flying

PS:神奇的姿势可以用double或long double过(FFT),我是过不了

my  code:

#include <bits/stdc++.h>
using namespace std ;

typedef long long LL ;

#define clr(a,x) memset ( a , x , sizeof a )

const int MAXN = 131072 ;
const int MAXM = 2000005 ;
const LL mod = ( 1LL << 47 ) * 7 * 4451 + 1 ;
const LL g = 3 ;

struct Node {
    LL x , y ;
} ;

LL x1[MAXN] , x2[MAXN] ;
LL ans[MAXN] ;
Node a[MAXN] ;
int n ;

LL mul ( LL x , LL y ) {
    return ( x * y - ( long long ) ( x / ( long double ) mod * y + 1e-3 ) * mod + mod ) % mod ;
}

LL power ( LL a , LL b ) {
    LL res = 1 , tmp = a ;
    while ( b ) {
        if ( b & 1 ) res = mul ( res , tmp ) ;
        tmp = mul ( tmp , tmp ) ;
        b >>= 1 ;
    }
    return res ;
}

void DFT ( LL y[] , int n , bool rev ) {
    for ( int i = 1 , j , t , k ; i < n ; ++ i ) {
        for ( k = n >> 1 , t = i , j = 0 ; k ; k >>= 1 , t >>= 1 ) {
            j = j << 1 | t & 1 ;
        }
        if ( i < j ) swap ( y[i] , y[j] ) ;
    }
    for ( int s = 2 , ds = 1 ; s <= n ; ds = s , s <<= 1 ) {
        LL wn = power ( g , ( mod - 1 ) / s ) ;
        if ( !rev ) wn = power ( wn , mod - 2 ) ;
        for ( int k = 0 ; k < n ; k += s ) {
            LL w = 1 , t ;
            for ( int i = k ; i < k + ds ; ++ i , w = mul ( w , wn ) ) {
                y[i + ds] = ( y[i] - ( t = mul ( y[i + ds] , w ) ) + mod ) % mod ;
                y[i] = ( y[i] + t ) % mod ;
            }
        }
    }
}

void FFT ( LL x1[] , LL x2[] , int n ) {
    DFT ( x1 , n , 1 ) ;
    DFT ( x2 , n , 1 ) ;
    for ( int i = 0 ; i < n ; ++ i ) x1[i] = mul ( x1[i] , x2[i] ) ;
    DFT ( x1 , n , 0 ) ;
    LL vn = power ( n , mod - 2 ) ;
    for ( int i = 0 ; i < n ; ++ i ) x1[i] = mul ( x1[i] , vn ) ;
}

void solve () {
    int x , L = 0 , s = 0 ;
    scanf ( "%d" , &n ) ;
    clr ( a , 0 ) ;
    ans[0] = 0 ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        scanf ( "%d" , &x ) ;
        if ( x ) L = i ;
        ans[0] += ( LL ) ( i - L ) * ( i - L + 1 ) / 2 ;
        s += x ;
        a[s].x += i ;
        a[s].y ++ ;
    }
    a[0].y ++ ;
    int n1 = 1 ;
    while ( n1 <= 2 * s ) n1 <<= 1 ;
    for ( int i = 0 ; i <= s ; ++ i ) {
        x1[i] = a[i].x ;
        x2[i] = a[s - i].y ;
    }
    for ( int i = s + 1 ; i < n1 ; ++ i ) x1[i] = x2[i] = 0 ;
    FFT ( x1 , x2 , n1 ) ;
    for ( int i = 1 ; i <= s ; ++ i ) {
        ans[i] = x1[i + s] ;
    }
    for ( int i = 0 ; i <= s ; ++ i ) {
        x1[i] = a[i].y ;
        x2[i] = a[s - i].x ;
    }
    for ( int i = s + 1 ; i < n1 ; ++ i ) x1[i] = x2[i] = 0 ;
    FFT ( x1 , x2 , n1 ) ;
    for ( int i = 1 ; i <= s ; ++ i ) {
        ans[i] -= x1[i + s] ;
    }
    for ( int i = 0 ; i <= s ; ++ i ) {
        printf ( "%I64d\n" , ans[i] ) ;
    }
}

int main () {
    int T ;
    scanf ( "%d" , &T ) ;
    for ( int i = 1 ; i <= T ; ++ i ) {
        solve () ;
    }
    return 0 ;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值