题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3067

题意:用K种颜色给一个N*M的格子涂色。其中有B个格子是不能涂色的。涂色时满足同一列上下紧邻的两个格子的颜色不同。所有的涂色方案模100000007后为R。现在给出M、K、B、R,求一个最小的N,满足题意。

思路:设给出的B个不能涂的格子的最大行坐标为maxX。首先,我们能计算出前maxX行的方案数ans,若ans=R则maxX就是答案。接着,我们能计算出前maxX+1行的方案数ans1,若ans1=R则答案为 maxX+1。否则,设下面还需要t行,那么有ans1*((K-1)^M)^t%100000007=R,将ans1的逆元乘到右侧得到新的R'=R*reverse(ans1),令p=(K-1)^M。那么就成了求最小的t满足p^t%100000007=R'。

int Pow(int x,int y)
{
    int ans=1;
    while(y)
    {
        if(y&1) ans=(i64)ans*x%mod;
        x=(i64)x*x%mod;
        y>>=1;
    }
    return ans;
}

int Pow(int x,int y,int mod)
{
    int ans=1;
    while(y)
    {
        if(y&1) ans=(i64)ans*x%mod;
        x=(i64)x*x%mod;
        y>>=1;
    }
    return ans;
}

int exGcd(int a,int b,int &x,int &y)
{
    if(!b) 
    {
        x=1; y=0;
        return a;
    }
    int temp=exGcd(b,a%b,x,y);
    int t=x;
    x=y;
    y=t-a/b*y;
    return temp;
}

int reverse(int a,int b)
{
    int x,y;
    exGcd(a,b,x,y);
    x=x%b;
    if(x<0) x+=b;
    return x;
}

int logMod(int a,int b,int n)
{
    int m=sqrt(n+0.5);
    int v=reverse(Pow(a,m,n),n);
    map<int,int> mp;
    int x=1,i;
    mp[1]=0;
    for(i=1;i<m;i++)
    {
        x=(i64)x*a%n;
        if(!mp.count(x)) mp[x]=i;
    }
    FOR0(i,m) 
    {
        if(mp.count(b)) return i*m+mp[b];
        b=(i64)b*v%n;
    }
    return -1;
}

int m,R,K,B,p;

int cal(int x)
{
    if(x<=0) return 1;
    if(x==1) return K;
    int ans=(i64)K*Pow(K-1,x-1,mod)%mod;
    return ans;
}

int cal(vector<int> V,int maxX)
{
    int pre=0,ans=1,i;
    FOR0(i,SZ(V))
    {
        ans=(i64)ans*cal(V[i]-pre-1)%mod;
        pre=V[i];
    }
    ans=(i64)ans*cal(maxX-pre)%mod;
    return ans;
}

void cal(vector<int> V[N],int n,int maxX)
{
    int temp,i,x=m-n;
    int ans=Pow(cal(maxX),x);
    for(i=1;i<=n;i++)
    {
        temp=cal(V[i],maxX);
        ans=(i64)ans*temp%mod;
        temp=SZ(V[i])-1;
        if(V[i][temp]<maxX) x++;
    }
    if(ans==R) PR(maxX);
    else 
    {
        ans=(i64)ans*Pow(K-1,x)%mod*Pow(K,m-x)%mod;
        if(ans==R) PR(maxX+1);
        else
        {
            temp=(i64)reverse(ans,mod)*R%mod;
            PR(maxX+1+logMod(p,temp,mod));
        }
    }
}

int main()
{
    int num=0;
    rush()
    {
        RD(m,K); RD(B,R);
        vector<int> V[N];
        map<int,int> mp;
        int i,x,y,t=0,maxX=0;
        FOR1(i,B)
        {
            RD(x,y);
            if(!mp.count(y)) mp[y]=++t;
            V[mp[y]].pb(x);
            maxX=max(maxX,x);
        }
        FOR1(i,t) sort(V[i].begin(),V[i].end());
        p=Pow(K-1,m,mod);
        printf("Case %d: ",++num);
        int ans;
        if(maxX==0)
        {
            ans=Pow(K,m);
            if(ans==R) puts("1");
            else
            {
                ans=(i64)reverse(ans,mod)*R%mod;
                PR(1+logMod(p,ans,mod)); 
            }
        }
        else cal(V,t,maxX);
    }
}

UVA 11916 Emoogle Grid(同余模)的更多相关文章

  1. uva 11916 Emoogle Grid (BSGS)

    UVA 11916 BSGS的一道简单题,不过中间卡了一下没有及时取模,其他这里的100000007是素数,所以不用加上拓展就能做了. 代码如下: #include <cstdio> #i ...

  2. uva 11916 Emoogle Grid

    题意:用K种颜色给一个N*M的格子涂色.其中有B个格子是不能涂色的.涂色时满足同一列上下紧邻的两个格子的颜色不同.所有的涂色方案模100000007后为R.现在给出M.K.B.R,求一个最小的N,满足 ...

  3. UVA 11916 Emoogle Grid 离散对数 大步小步算法

    LRJ白书上的题 #include <stdio.h> #include <iostream> #include <vector> #include <mat ...

  4. UVA - 11916 Emoogle Grid (组合计数+离散对数)

    假如有这样一道题目:要给一个M行N列的网格涂上K种颜色,其中有B个格子不用涂色,其他每个格子涂一种颜色,同一列中的上下两个相邻格子不能涂相同颜色.给出M,N,K和B个格子的位置,求出涂色方案总数除以1 ...

  5. [uva11916] Emoogle Grid (离散对数)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud  Emoogle Grid  You have to color an MxN ( ...

  6. uva 11916 解模方程a^x=b (mod n)

      Emoogle Grid  You have to color an M x N ( 1M, N108) two dimensional grid. You will be provided K  ...

  7. UVA11916 Emoogle Grid

    Emoogle Grid You have to color an M × N (1 ≤ M, N ≤ 108 ) two dimensional grid. You will be provided ...

  8. UVa 11916 (离散对数) Emoogle Grid

    因为题目要求同列相邻两格不同色,所以列与列之间不影响,可以逐列染色. 如果一个格子的上面相邻的格子,已经被染色则染这个格子的时候,共有k-1中选择. 反过来,如果一个格子位于第一列,或者上面相邻的格子 ...

  9. uva11916 Emoogle Grid (BSGS)

    https://uva.onlinejudge.org/external/119/p11916.pdf 令m表示不能染色的格子的最大行号 设>m行时可以染k种颜色的格子数有ck个,恰好有m行时可 ...

随机推荐

  1. iOS多线程编程Part 1/3 - NSThread & Run Loop

    前言 多线程的价值无需赘述,对于App性能和用户体验都有着至关重要的意义,在iOS开发中,Apple提供了不同的技术支持多线程编程,除了跨平台的pthread之外,还提供了NSThread.NSOpe ...

  2. 结构体,公用体,枚举类型的sizeof

    1)枚举类enum型空间计算 enum只是定义了一个常量集合,里面没有“元素”,而枚举类型是当做int来存储的,所以枚举类型的sizeof值都为4 enum color(red,pink,white, ...

  3. 包装设计模式的实现以改进BufferedReader中的readLine方法为例

    实现与目标对象相同的接口     BufferedReader 定义一个变量记住目标对象 定义一个构造器接收被增强对象 覆盖需要增强的方法 对于不想增强的方法,直接调用目标对象的方法. package ...

  4. C++中使用心得

    1.struct成员默认访问方式是public,而 class默认访问方式是private! 2.exit函数终止程序执行会调用析构函数 ,abort函数终止程序不会调用析构函数! 3.静态局部变量直 ...

  5. bnuoj 33647 Angry Grammar Nazi(字符串)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=33647 [题意]:字符串匹配,暴力配就行了 [题解]:截出单词,然后进行匹配就行了 [code]: ...

  6. ubuntu下修改ip重启系统ip不变

    今天同学问我ubuntu下ip如何写死,我想起这周在公司我们队长也问过我,我就在这把我实验的方法说一下. 打开终端: sudo vim /etc/network/interfaces 然后按如下修改: ...

  7. 无法将 flash.display::Sprite@156b7b1 转换为 mx.core.IUIComponent

    无法将 flash.display::Sprite@156b7b1 转换为 mx.core.IUIComponent 在Flex Application里,是不能直接用addChild添加Sprite ...

  8. 不同的source control下配置DiffMerge

    TFS: 1. 打开Option -> Source Control -> Visual Studio TFS -> Configure User Tools; 2. 添加 .*, ...

  9. 常用git 命令

    1.取消跟踪某些文件或文件夹: 删除文件: $git rm --cached FILENAME 删除文件夹: $git rm -r --cached Path 2.忽略某些文件或文件夹 $vi .gi ...

  10. BZOJ 2763: [JLOI2011]飞行路线 spfa dp

    题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=2763 题解: d[x][kk]表示从s到x用了kk次免费机会的最少花费. 代码: #in ...