zoukankan      html  css  js  c++  java
  • uva 11916 解模方程a^x=b (mod n)

      Emoogle Grid 

    You have to color an M x N ( 1$ le$M, N$ le$108) two dimensional grid. You will be provided K ( 2$ le$K$ le$108) different colors to do so. You will also be provided a list of B( 0$ le$B$ le$500) list of blocked cells of this grid. You cannot color those blocked cells. A cell can be described as (x, y), which points to the y-th cell from the left of the x-th row from the top.

    While coloring the grid, you have to follow these rules -

     

    1. You have to color each cell which is not blocked.
    2. You cannot color a blocked cell.
    3. You can choose exactly one color from K given colors to color a cell.
    4. No two vertically adjacent cells can have the same color, i.e. cell (x, y) and cell (x + 1, y) cannot contain the same color.

     

    epsfbox{p11916.eps}

    Now the great problem setter smiled with emotion and thought that he would ask the contestants to find how many ways the board can be colored. Since the number can be very large and he doesn't want the contestants to be in trouble dealing with big integers; he decided to ask them to find the result modulo 100,000,007. So he prepared the judge data for the problem using a random generator and saved this problem for a future contest as a giveaway (easiest) problem.

    But unfortunately he got married and forgot the problem completely. After some days he rediscovered his problem and became very excited. But after a while, he saw that, in the judge data, he forgot to add the integer which supposed to be the `number of rows'. He didn't find the input generator and his codes, but luckily he has the input file and the correct answer file. So, he asks your help to regenerate the data. Yes, you are given the input file which contains all the information except the `number of rows' and the answer file; you have to find the number of rows he might have used for this problem.

     

    Input 

    Input starts with an integer T (T$ le$150), denoting the number of test cases.

    Each test case starts with a line containing four integers N, K, B and R ( 0$ le$R < 100000007) which denotes the result for this case. Each of the next B lines will contains two integers x and y ( 1$ le$x$ le$M, 1$ le$y$ le$N), denoting the row and column number of a blocked cell. All the cells will be distinct.

     

    Output 

    For each case, print the case number and the minimum possible value of M. You can assume that solution exists for each case.

     

    Sample Input 

     

    4
    3 3 0 1728
    4 4 2 186624
    3 1
    3 3
    2 5 2 20
    1 2
    2 2
    2 3 0 989323
    

     

    Sample Output 

     

    Case 1: 3
    Case 2: 3
    Case 3: 2
    Case 4: 20
    
    题目大意:已知N,K,R和B个格子的位置求最小可能的M。

     

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cmath>
    #include<map>
    #include<set>
    using namespace std;
    
    typedef long long LL;
    const int MOD=100000007;
    const int Max=1000;
    int N,M,B,K,R,x[Max],y[Max];
    set<pair<int,int> > bset;
    
    LL mult_mod(LL a,LL b)
    {
        LL t=0;
        a%=MOD;
        while(b)
        {
            if(b&1) t=(t+a)%MOD;
            b>>=1;
            a=(a<<1)%MOD;
        }
        return t;
    }
    
    LL pow_mod(LL a,LL b)
    {
        LL t=1;
        a%=MOD;
        while(b)
        {
            if(b&1) t=mult_mod(t,a);
            b>>=1;
            a=mult_mod(a,a);
        }
        return t;
    }
    
    int Extended_Euclid(int a,int b,int &x,int &y)
    {
        int d,t;
        if(b==0)
        {
            x=1;y=0;return a;
        }
        d=Extended_Euclid(b,a%b,x,y);
        t=x;
        x=y;
        y=t-a/b*y;
        return d;
    }
    
    int inv(int a)
    {
        int x,y,d;
        d=Extended_Euclid(a,MOD,x,y);
        x=(x%MOD+MOD)%MOD;
        return x;
    }
    
    int log_mod(int a,int b)
    {
        int c,v,e=1,i;
        c=(int)sqrt(MOD+0.5);
        v=inv(pow_mod(a,c));
        map<int,int> xx;
        xx[1]=0;
        for(i=1;i<c;i++)//计算e[i]
        {
            e=mult_mod(e,a);
            if(!xx.count(e)) xx[e]=i;
        }
        for(i=0;i<c;i++)
        {
            if(xx.count(b)) return (i*c+xx[b]);
            b=mult_mod(b,v);
        }
        return -1;
    }
    
    int Count()
    {
        int cnt;//涂色种数
        int c=0;//能涂k种的个数
        int i;
        for(i=0;i<B;i++)
            //上面是不能涂色的下面是能涂色的情况,排除不能涂色的相邻的情况
            if(x[i]!=M && !bset.count(make_pair(x[i]+1,y[i])) )    c++;
        c+=N;//第一行的都能涂k种色
        for(i=0;i<B;i++)//减去第一行不能涂色的
            if(x[i]==1) c--;
        //ans=k^c * (k-1)^(m*n-c-b) mod MOD
        cnt=mult_mod(pow_mod(K,c),pow_mod(K-1,(LL)M*N-B-c));
        return cnt;
    }
    
    int Deal()
    {
        int i,cnt=Count();
           if(cnt==R) return M;
        int c=0,m;
        for(i=0;i<B;i++)//不变部分最后一行的下一行能涂k种的个数
            if(x[i]==M) c++;
        M++;
        cnt=mult_mod(cnt,pow_mod(K,c));
        cnt=mult_mod(cnt,pow_mod(K-1,N-c));
        if(cnt==R) return M;
        //模方程求解 a^x=b (mod n),用log_mod(a,b,n)函数求解
        m=log_mod(pow_mod(K-1,N),mult_mod(R,inv(cnt)))+M;
        return m;
    }
    
    int main()
    {
        int t,i,Case=0;
        scanf("%d",&t);
        while(t--)
        {
                    bset.clear();
            Case++;
            scanf("%d %d %d %d",&N,&K,&B,&R);
            M=1;
            for(i=0;i<B;i++)
            {
                scanf("%d %d",x+i,y+i);
                bset.insert(make_pair(x[i],y[i]));//插入一个座标
                if(M<x[i]) M=x[i];
            }
            printf("Case %d: %d
    ",Case,Deal());
        }
        return 0;
    }
  • 相关阅读:
    IDEA运行测试错误Failed to resolve org.junit.platform:junit-platform-launcher
    mysql索引基本原理
    as3.0声音波形系列03_十组合
    as3.0声音波形系列02_六组合
    as3.0声音波形系列01_八组合
    FiltersEffect(效果)
    AS3 TransitionManager 自带特效类
    as3 判断鼠标移动方向
    AS3代码生成xml方法
    求线段的交点
  • 原文地址:https://www.cnblogs.com/xiong-/p/3242433.html
Copyright © 2011-2022 走看看