UVa 11916 (discrete logarithm) emoogle Grid

Source: Internet
Author: User

Because the topic requires two different colors to be adjacent to the same column, there is no effect between columns and columns, which can be dyed on a column-by-row basis.

If a lattice above the adjacent lattice, has been dyed the lattice when dyed, a total of k-1 selected.

Conversely, if a lattice is in the first column, or the adjacent lattice above is a lattice that cannot be dyed, then there is a total of k selected.

Although, the number of rows in the matrix varies, but at least the maximum value of m for all rows that cannot be stained by the grid.

Test the number of scenarios (mod 100000007) for M-and m+1-lines respectively for R

Otherwise, the number of per-line schemes is multiplied by P = (k-1) n, assuming that the number of m+1 line schemes previously calculated is CNT

The following task is to solve px * cnt = r (mod mod), on both sides multiply CNT inverse, get px = R * cnt-1 (mod mod)

Finally add the previous m+1 line, the answer is x + M + 1

Finally Spit groove I think the pit point =_=, see that mold take for granted that is 1e9+7, but the last kind of example tune for a long time, and later only found the model is 1e8+7

1#include <cstdio>2#include <map>3#include <Set>4#include <cmath>5#include <algorithm>6 using namespacestd;7 #defineMP Make_pair8 #defineINS Insert9typedefLong LongLL;Ten  One Const intMOD =100000007; A Const intMAXB = -+Ten; - intN, M, K, B, R, X[maxb], Y[MAXB], Fir_row; - Set<pair<int,int> >Set; the  - intMul_mod (intAintb) -{return(LL) A * b%MOD;} -  + intPow_mod (intA, LL N) - { +     intAns =1,Base=A; A      while(n) at     { -         if(N &1) ans = mul_mod (ans,Base); -         Base= Mul_mod (Base,Base); -N >>=1; -     } -     returnans; in } -  to intInvinta) +{returnPow_mod (A, mod-2); } -  the intLog_mod (intAintb) *{//a^x=b (mod mod) $     intM, V, e =1, I;Panax Notoginsengm = (int) sqrt (MOD +0.5); -v =INV (Pow_mod (A, M)); themap<int,int>x; +x[1] =0; A      for(i =1; I < m; i++) the     { +E =Mul_mod (E, a); -         if(E = = b)returni; $         if(!x.count (e)) x[e] =i; $     } -      for(i =0; I < m; i++) -     { the         if(X.count (b))returnI*m +X[b]; -b =Mul_mod (b, v);Wuyi     } the     return-1; - } Wu  - intSolve () About { $     intc =0;//number of squares of k colors applied to the first m line -      for(inti =0; I < b; i++) -         if(X[i]! = M &&! Set.count (MP (x[i) +1, Y[i]))) C + +; -c + = n-Fir_row; A     intCNT = Mul_mod (Pow_mod (k, c), Pow_mod-K1, (LL) M*n-b-c)); +     if(cnt = = r)returnm; the  -c =0;//section m+1 the number of squares coated with K colors $      for(inti =0; I < b; i++)if(X[i] = = m) C + +; theCNT = Mul_mod (Mul_mod (CNT, Pow_mod (k, c)), Pow_mod (K-1, N-c)); the     if(cnt = = r)returnM +1; the  the     intp = Pow_mod (K-1, n); -     intv =INV (CNT); in     returnLog_mod (P, Mul_mod (R, v)) + M +1; the } the  About intMain () the { the     //freopen ("In.txt", "R", stdin); the  +     intT; -scanf"%d", &T); the      for(intKase =1; Kase <= T; kase++)Bayi     { thescanf"%d%d%d%d", &n, &k, &b, &R); the set.clear (); -m =1; Fir_row =0; -          for(inti =0; I < b; i++) the         { thescanf"%d%d", &x[i], &y[i]); the Set.ins (MP (X[i], y[i])); the             if(X[i] > m) m =X[i]; -             if(X[i] = =1) fir_row++;//the number of the first line that cannot be painted the         } theprintf"Case %d:%d\n", Kase, Solve ()); the     }94  the     return 0; the}
code June

UVa 11916 (discrete logarithm) emoogle Grid

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.