[Discrete logarithm] ultraviolet A 11916 Emoogle Grid

Source: Internet
Author: User

[Discrete logarithm] ultraviolet A 11916 Emoogle Grid

Question:

Apply K colors to an M * N matrix.

There are B cells that cannot be colored, and each cell cannot be the same color as the cell above.

If N and total result R are known, find the row M with the minimum satisfaction.

All data has a modulo value of 100,000,007.

Ideas:

Because the X coordinates of the input vertex that cannot be colored must be within M. So record maxX.

Divide the entire matrix into three parts.

The first part is maxX * N. The answer in this part can be used to determine whether it is satisfied.

The second part is (maxX + 1) * N. This part has one more row. In order to remove the influence of some grids that cannot be entered.

The third part is M * N, followed by (M-maxX-1) line each lattice filled in color is only K-1.

At this time set the previous answer to ans, set M-maxX-1 = T

Then ans * (K-1) ^ (T * N) = R (mod 100000007)

At this time, we need to request an X ^ X = B (mod M)

An enumeration method is required.

Code:

 

#include"cstdlib"#include"cstdio"#include"cstring"#include"cmath"#include"queue"#include"algorithm"#include"iostream"#include"map"#define ll long longusing namespace std;int mark[502][502];map
 
  id;ll mod=100000007;int n,k,b,idcnt,maxx,maxcnt;ll r;ll power(ll a,ll b){    ll ans=1;    while(b)    {        if(b&1) ans=(ans*a)%mod;        a=(a*a)%mod;        b/=2;    }    return ans%mod;}ll inv(ll x){    return power(x,mod-2);}ll log_mod(ll a,ll b){    ll m,v,e=1,i;    m=(ll)sqrt(mod+0.5);    v=inv(power(a,m));    map
  
   x;    x.clear();    x[1]=0;    for(i=1;i
   
    =mod) e%=mod;        if(x[e]==0) x[e]=i;    }    for(i=0;i
    
     =mod) b%=mod;    }    return -1;}int solve(){    //1~maxx    ll ans=1;    if(maxx!=0)    {        for(int i=1;i<=idcnt;i++)        {            int cur=0;            mark[i][++mark[i][0]]=maxx+1;            for(int j=1;j<=mark[i][0];j++)            {                int sum=mark[i][j]-cur-1;                cur=mark[i][j];                if(sum==0) continue;                ans*=k;                if(ans>=mod) ans%=mod;                ans*=power(k-1,sum-1);                if(ans>=mod) ans%=mod;            }        }        ans*=power(k,n-idcnt);        if(ans>=mod) ans%=mod;        ans*=power(k-1,(maxx-1LL)*(n-idcnt));        if(ans>=mod) ans%=mod;        if(ans==r) return maxx;    }    //maxx+1    ans*=power(k,maxcnt);    if(ans>=mod) ans%=mod;    ans*=power(k-1,n-maxcnt);    if(ans>=mod) ans%=mod;    if(ans==r) return maxx+1;    ll tep=inv(ans);    ll A,B;    B=(tep*r)%mod;    A=power(k-1,n);    return log_mod(A,B)+maxx+1;}int main(){    int t,cas=1;    cin>>t;    while(t--)    {        scanf("%d%d%d%lld",&n,&k,&b,&r);        memset(mark,0,sizeof(mark));        id.clear();        idcnt=0;        maxx=0;        maxcnt=n;        for(int i=0;i
     
      maxx)            {                maxx=x;                maxcnt=1;            }            else if(x==maxx) maxcnt++;        }        printf("Case %d: %d\n",cas++,solve());    }    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.