解題:CQOI 2013 和諧矩陣

來源:互聯網
上載者:User

標籤:檢查   play   技術分享   hide   amp   org   name   \n   cstring   

題面

踩踩時間複雜度不正確的高斯消元

首先可以發現第一行確定後就可以確定整個矩陣,所以可以枚舉第一行的所有狀態然後$O(n)$遞推檢查是否合法

$O(n)$遞推的方法是這樣的:設$pre$為上一行,$now$為當前行,$nxt$為遞推出的下一行,$all$為列的全集,則可以直接用位元運算完成遞推:

$nxt=all\&((now<<1)xor(now>>1)xor$ $now$ $xor$ $pre)$

遞推後第$n+1$行為空白則說明可行

問題來了,第一行的狀態有$O(2^{40})$種,會$T$。但是有一個鬼畜的性質是如果存在合法解一定有一個對稱的合法解,然後就可以$O(n*2^{\frac{n}{2}})$出解了=。=

 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int N=50; 6 int mapp[N][N]; 7 long long n,m,mid,all,half; 8 long long fir,las,noww,tmp; 9 int check(int a,int b)10 {11     return mapp[a][b]^mapp[a-1][b]^mapp[a][b-1]^mapp[a][b+1];12 }13 int main ()14 {15     scanf("%lld%lld",&n,&m);16     mid=(m+1)/2,all=(1ll<<m)-1,half=(1ll<<mid)-1;17     for(int i=1;i<=half;i++)18     {19         fir=0;20         for(int j=1;j<=mid;j++)21             if(i&(1ll<<(j-1))) fir|=(1ll<<(j-1))|(1ll<<(m-j));22         las=noww=all&(fir^(fir<<1)^(fir>>1)),tmp=fir;23         for(int j=2;j<=n;j++)24             las=noww,noww=all&(noww^(noww<<1)^(noww>>1)^tmp),tmp=las;25         if(!noww) break;26     }27     for(int i=1;i<=m;i++)28         mapp[1][i]=(fir&(1ll<<(i-1)))?1:0;29     for(int i=2;i<=n;i++)30         for(int j=1;j<=m;j++)31             mapp[i][j]=check(i-1,j);32     for(int i=1;i<=n;i++)33     {34         for(int j=1;j<=m;j++)35             printf("%d ",mapp[i][j]);36         printf("\n");37     }38     return 0;39 }
View Code

 

解題:CQOI 2013 和諧矩陣

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.