Uvalive 4978 fields and farmers

Source: Internet
Author: User

Uvalive_4978

The difficulty of this question lies in how to convert it into a mathematical model. For detailed conversion ideas, refer to the official question: http://cepc10.ii.uni.wroc.pl/solutions.html.

After the transformation of the mathematical model, the problem becomes that a number of 8-bit binary numbers are given, and a number of numbers and these numbers are calculated as 11111111, this can be solved using the exclusion principle.

First, we agree that a [I] indicates that the final result has only the number of solutions where I bit is 0, so what we need at last is a [0]. then, we agree that f [I] indicates the total number of methods in which any one of the eight bits in the result is 0, and the other BITs do not matter. In this way, F [0] is 2 ^ n. When calculating f [I], first enumerate which I bit is 0, and then find that the number of I bit 0 has a total of X, so the result of the last or the number of I-Bit 0 is 2 ^ X-1, accumulate these 2 ^ X-1 values obtained when I bit is 0 and then observe the features of the expression f [I.

Obviously f [0] contains all the situations, so f [0] = A [0] + A [1] + A [2] +... + A [8] And then f [1] are considered. First, there is only one number of solutions whose bit is 0 in the final result, after we calculate the f [I] method, each scheme is calculated only once. Therefore, one of F [1] is a [1], then, for the number of solutions with only two bits 0 in the final result, we calculate the number once when the first bit is 0, and the number is calculated once when the second bit is 0, then, in F [1], there will be a C () * A [2], that is, 2 * A [2]. Then, the expression f [1] = C () * A [1] + C () is obtained) * A [2] +... + C (8, 1) * A [8].

Similarly, we can get f [2] = C () * A [2] + C () * A [3] +... + C (8, 2) * A [8], you can push the remaining f [I.

In this case, we can find that a [0] = f [0]-f [1] + F [2]-f [3] +... -F [7] + F [8]. Then, F [I] is calculated based on the above method to obtain a [0.

# Include <stdio. h> # Include < String . H> # Include <Algorithm># Define Maxd 1000010 # Define INF 0x3f3f3f # Define Mod4 1000000007 Typedef  Long   Long  Ll;  Int DX [] = { 1 , 1 , 0 , 0 , 1 , 1 ,1 , 1 }, Dy [] = { 0 , 0 , 1 , 1 , 1 , 1 ,- 1 ,- 1 }, D [ 8  ];  Int N, H [ 260 ], X [maxd], Y [maxd];  Char  Ch;  Void  Init (){  Int  I, J; scanf (  "  % D  " ,& N); D [  0 ] = D [ 2 ] = D [ 4 ] = D [ 6 ] = Inf, d [1 ] = D [ 3 ] = D [ 5 ] = D [ 7 ] =- INF;  For (I = 0 ; I <n; I ++ ) {Scanf (  "  % D  " , & X [I], & Y [I]); D [  0 ] = STD: min (d [ 0 ], X [I]), d [1 ] = STD: max (d [ 1  ], X [I]); D [  2 ] = STD: min (d [ 2 ], Y [I]), d [ 3 ] = STD: max (d [ 3  ], Y [I]); D [  4 ] = STD: min (d [ 4 ], X [I] + Y [I]), d [ 5 ] = STD: max (d [ 5 ], X [I] + Y [I]); D [ 6 ] = STD: min (d [ 6 ], X [I]-y [I]), d [ 7 ] = STD: max (d [ 7 ], X [I]- Y [I]);} memset (H,  0 , Sizeof  (H ));  For (I = 0 ; I <n; I ++ ){  Int T = 0  ; For (J = 0 ; J < 8 ; J ++ ){  If (Dx [J] * X [I] + dy [J] * Y [I] = d [J]) t = T < 1 | 1  ;  Else T <= 1  ;} ++ H [T] ;}} ll powmod (ll,  Int N) {ll ans = 1  ;  While  (N ){  If (N & 1 ) Ans = ans * A % MOD; n >>= 1 , A = A * A % MOD ;}  Return  Ans ;}  Void DFS ( Int Cur,Int St, Int N, Int & Ans ){  If (Cur = 8  ){  Int I, num = 0  ;  For (I = 0 ; I < 256 ; I ++ ) If ((~ St & I) = 0 ) Num + = H [I];  If (N & 1 ) Ans = (ANS-powmod ( 2 , Num) % MOD;  Else Ans = (ANS + powmod ( 2 , Num) % MOD;  Return  ;} DFS (cur + 1 , St, N +1  , ANS); DFS (cur + 1 , St | 1 < Cur, N, ANS );}  Void  Solve (){  Int Ans = 0  ; DFS (  0 , 0 , 0  , ANS );  If (ANS <0 ) Ans + = MOD; printf (  "  % D \ n  "  , ANS );}  Int  Main (){  Int  T; scanf (  "  % D  " ,& T );  While (T --) {Init (); solve ();}  Return   0  ;} 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.