【HDU】5307 He is Flying【分别统计+NTT】
来源:互联网 发布:单片机波形发生器设计 编辑:程序博客网 时间:2024/05/22 03:23
传送门:【HDU】5307 He is Flying
PS:神奇的姿势可以用double或long double过(FFT),我是过不了
#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 0
- 【HDU】5307 He is Flying【分别统计+NTT】
- hdu 5307 He is Flying
- hdu 5307 He is Flying
- HDU 5307 He is Flying【FFT】
- hdu 5307 He is Flying [FFT]
- HDU 5307 He is Flying 构造多项式+FFT
- HDU5307 He is Flying
- hdu 5307 He is Flying 2015 Multi-University Training Contest 2 快速傅里叶变换
- hdu5307 He is Flying FFT
- [FFT] [HDU5307] He is Flying
- 多校第二场1008 He is flying
- Hdu 5307 He is Fly 2015ACM多校对抗赛第二场
- hdu 1800 Flying to the Mars(字典树统计)
- HDU 5829 (NTT)
- hdu 6061 NTT
- hdu 6036 NTT
- hdu 4656(NTT
- Time is flying!!!
- 判断浏览器是否支持CSS3 已经初步探索JS 惰性加载
- linux 逻辑卷创建、增加与减少
- python-re模块-使用案例
- [Android]ToolBar使用详解(一)——项目配置
- c语言之cgi
- 【HDU】5307 He is Flying【分别统计+NTT】
- keepalived的安装
- Scala练习(六)
- <s:if>使用
- UVA12563 Jin Ge Jin Qu hao(DP, 背包+技巧)
- I帧 B帧 p帧 IDR帧的区别
- LeetCode Maximal Square
- 我的学习日志009:“密码生成器”
- 使crs_stat -t显示完整的resource name