Ultraviolet A 11916-Emoogle Grid (STRIDE small step algorithm)

Source: Internet
Author: User

Question connection: Ultraviolet A 11916-Emoogle Grid

One problem is that K colors are applied to the grid of N columns in the M row, where B grids do not need to be colored, and each other lattice is colored, two Adjacent grids of the same column cannot be painted with the same color. The positions of M, N, K, and B grids are given, and the result R of the overall solution is calculated based on the mathematical model of 1e8 + 7. Now we know R, and find the smallest M.

Solution: Make sure that the area without colored grids is used as the unchanged part. The total number is calculated as tmp, and the first line of the variable part is added. The number of solutions is cnt. The variable part is not included in the first line, the total number of rows added is multiplied by (K? 1) N, existing

  • Cnt? PM = Rmod (1e8 + 7)
  • PM = cnt? 1? Rmod (1e8 + 7)
    That is, the stride small step algorithm is used to calculate M.
    #include 
        
         #include 
         
          #include 
          
           #include 
           
            #include 
            #include using namespace std;typedef long long ll;const ll MOD = 1e8+7;const int maxn = 505;int N, M, K, R, B, X[maxn], Y[maxn];set
             
               > bset;inline ll mul_mod (ll a, ll b) { return (ll)a * b % MOD;}inline ll pow_mod (ll a, ll n) { ll ans = 1; while (n) { if (n&1) ans = mul_mod(ans, a); a = mul_mod(a, a); n /= 2; } return ans;}void gcd (ll a, ll b, ll& d, ll& x, ll& y) { if (!b) { d = a; x = 1; y = 0; } else { gcd (b, a%b, d, y, x); y -= x * (a/b); }}inline ll inv (ll a) { ll d, x, y; gcd(a, MOD, d, x, y); return d == 1 ? (x+MOD)%MOD : -1;}void init () { scanf("%d%d%d%d", &N, &K, &B, &R); bset.clear(); M = 1; for (int i = 0; i < B; i++) { scanf("%d%d", &X[i], &Y[i]); M = max(M, X[i]); bset.insert(make_pair(X[i], Y[i])); }}int count () { int c = 0; for (int i = 0; i < B; i++) { if (X[i] != M && !bset.count(make_pair(X[i]+1, Y[i]))) c++; } c += N; for (int i = 0; i < B; i++) if (X[i] == 1) c--; return mul_mod(pow_mod(K, c), pow_mod(K-1, (ll)M*N-B-c));}int log_mod (ll a, ll b) { ll m = (ll)sqrt(MOD+0.5), v, e = 1; v = inv(pow_mod(a, m)); map
              
                g; g[1] = 0; for (ll i = 1; i < m; i++) { e = mul_mod(e, a); if (!g.count(e)) g[e] = i; } for (ll i = 0; i < m; i++) { if (g.count(b)) return i*m+g[b]; b = mul_mod(b, v); } return -1;}int doit () { int cnt = count(); if (cnt == R) return M; int c = 0; for (int i = 0; i < B; i++) if (X[i] == M) c++; M++; cnt = mul_mod(cnt, pow_mod(K, c)); cnt = mul_mod(cnt, pow_mod(K-1, N-c)); if (cnt == R) return M; return log_mod(pow_mod(K-1, N), mul_mod(R, inv(cnt))) + M;}int main () { int cas; scanf("%d", &cas); for (int i = 1; i <= cas; i++) { init(); printf("Case %d: %d\n", i, doit()); } return 0;}
              
             
           
          
         
        

Related Article

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.