BZOJ3160萬徑人蹤滅

來源:互聯網
上載者:User

標籤:color   down   卡常數   com   多項式   fft   code   src   record   

 

題解:

題意即求不連續但間隔長度對稱的迴文串個數。

若si=sj,則這對字元可以作為以(i+j)/2為中心的迴文串的一部分。

用F[i]來表示可以做為以i/2為中心的迴文串的一部分的字元對數,則以i/2為中心的迴文串數為2^F[i]。

則這就成了多項式乘法:先做一次a的,把字元為a的位置值賦為1,其餘為0,進行一次FFT;同理做一次b的。

因為完全連續是不可以的,所以用Manacher求出這樣的迴文串的個數並減去。

 

代碼:

(BZOJ上PASCAL跑得不夠快,再加上這題時限只有10s,並沒有AC,不知道那些用PASCAL通過的人是怎麼卡常數的)

uses math;const  mo=1000000007;type  xs=record    x,y:double;  end;  arr=array[0..270001]of xs;var  e,t:arr;  a:array[1..5]of arr;  n,m,i,ll,ans:longint;  ch:array[-2..270001]of char;  b:array[-2..270001]of longint;  z,ksm:array[0..270001]of longint;  s:ansistring;operator -(a,b:xs)c:xs;begin c.x:=a.x-b.x; c.y:=a.y-b.y; end;operator +(a,b:xs)c:xs;begin c.x:=a.x+b.x; c.y:=a.y+b.y; end;operator *(a,b:xs)c:xs;begin c.x:=a.x*b.x-a.y*b.y; c.y:=a.x*b.y+a.y*b.x; end;procedure manacher;var k,l,i:longint;begin  k:=-1; l:=-1; b[-1]:=1;  for i:=0 to m*2-1 do  begin    if l>=i then    b[i]:=min(b[2*k-i],l-i+1)else b[i]:=1;    while true do    begin      if ch[i+b[i]]=ch[i-b[i]] then inc(b[i])      else break;    end;    ans:=(ans+mo-(b[i]shr 1))mod mo;    if i+b[i]-1>l then begin l:=i+b[i]-1; k:=i; end;  end;end;procedure fft(xx:longint);var i,j,q,k,l,c:longint;  t:xs;begin  for i:=0 to n-1 do a[xx+2,z[i]]:=a[xx,i];  xx:=xx+2;  k:=n; l:=1;  for i:=ll downto 1 do  begin    k:=k shr 1;    for j:=0 to k-1 do    begin      c:=j*2*l;      for q:=0 to l-1 do      begin        t:=e[q*k]*a[xx,c+l];        a[xx,c+l]:=a[xx,c]-t;        a[xx,c]:=a[xx,c]+t;        inc(c);      end;    end;    l:=l*2;  end;end;begin  readln(s); m:=length(s);  ch[-2]:=‘+‘;  for i:=0 to m-1 do ch[i*2]:=s[i+1];  ch[m*2+1]:=‘-‘;  manacher;  for i:=0 to m-1 do if ch[i*2]=‘a‘ then a[1,i].x:=1;  for i:=0 to m-1 do if ch[i*2]=‘b‘ then a[2,i].x:=1;  n:=1;  while n<m*2 do begin n:=n*2; inc(ll); end;  for i:=1 to n-1 do z[i]:=(z[i shr 1]shr 1)or((i and 1)shl(ll-1));  ksm[0]:=1; for i:=1 to 100000 do ksm[i]:=(ksm[i-1]*2)mod mo;  for i:=0 to n-1 do e[i].x:=cos(pi*2*i/n);  for i:=0 to n-1 do e[i].y:=sin(pi*2*i/n);  fft(1); fft(2);  for i:=0 to n-1 do a[3,i]:=a[3,i]*a[3,i]+a[4,i]*a[4,i];  for i:=0 to n-1 do e[i].y:=-e[i].y;  fft(3);  for i:=0 to m-1 do a[5,i*2].x:=a[5,i*2].x+n;  for i:=0 to n-1 do ans:=(ans+ksm[round(a[5,i].x/2/n)]-1)mod mo;  writeln(ans);end.

BZOJ3160萬徑人蹤滅

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.