uva11916 Emoogle Grid (BSGS)

https://uva.onlinejudge.org/external/119/p11916.pdf

令m表示不能染色的格子的最大行号

设>m行时可以染k种颜色的格子数有ck个,恰好有m行时可以染k种颜色的格子数有ckm个

分m行、m+1行、>m+1行讨论

如果是m行:k^ckm * (k-1)^(n*m-b-ckm) = r

如果是m+1行, k^ckm * (k-1)^(n*m-b-ckm) * k^(ck-ckm) * (k-1)^(n-(ck-ckm)) = r

如果>m行,k^ckm * (k-1)^(n*m-b-ckm) * k^(ck-ckm) * (k-1)^(n-(ck-ckm))  * k^n^mm = r ,ans=m+1+mm

求mm用bsgs

注意:

1、后面注意b=0的情况,当b=0时,n*m-b-ckm 是负数,快速幂死循环,注意特判

2、用bsgs时,A^B≡C mod P,其中A=k^n,不能令A=k,求出的B再除以n

#include<map>
#include<cmath>
#include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;

const int mod=1e8+7;

#define N 501

struct node
{
    int x,y;
}e[N+1];

map<int,int>mp;

void read(int &x)
{
    x=0; char c=getchar();
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) { x=x*10+c-‘0‘; c=getchar(); }
}

int Pow(int a,long long b)
{
    int res=1;
    for(;b;b>>=1,a=1LL*a*a%mod)
        if(b&1) res=1LL*res*a%mod;
    return res;
}

int get_inv(int a)
{
    return Pow(a,mod-2);
}

int BSGS(int a,int b,int p)
{
    mp.clear();
    int m=sqrt(p);
    mp[b]=0;
    for(int i=1;i<=m;++i)
    {
        b=1LL*b*a%mod;
        mp[b]=i;
    }
    int am=Pow(a,m);
    int mul=1;
    for(int i=1;i<=m;++i)
    {
        mul=1LL*mul*am%mod;
        if(mp.find(mul)!=mp.end()) return i*m-mp[mul];
    }
    return -1;
}

bool cmp(node p,node q)
{
    if(p.y!=q.y) return p.y<q.y;
    return p.x<q.x;
}

int main()
{
    int T;
    int n,k,b,r;
    int m,x,y;
    int ck,ckm;
    int a,rr;
    int ans;
    read(T);
    for(int t=1;t<=T;++t)
    {
        read(n); read(k); read(b); read(r);
        ck=ckm=n; m=0;
        for(int i=1;i<=b;++i)
        {
            read(e[i].x); read(e[i].y);
            if(e[i].x==1) ck--,ckm--;
            m=max(m,e[i].x);
        }
        sort(e+1,e+b+1,cmp);
        e[b+1].x=e[b+1].y=-1;
        for(int i=1;i<=b;++i)
            if(!(e[i].y==e[i+1].y && e[i].x==e[i+1].x-1))
            {
                ck++;
                if(e[i].x!=m) ckm++;
            }
        if(m)
        {
            rr=1LL*Pow(k,ckm)*Pow(k-1,1LL*n*m-b-ckm)%mod;
            if(rr==r)
            {
                printf("Case %d: %d\n",t,m);
                continue;
            }
            rr=1LL*rr*Pow(k,ck-ckm)%mod;
            rr=1LL*rr*Pow(k-1,n-(ck-ckm))%mod;
        }
        else rr=Pow(k,n);
        if(rr==r)
        {
            printf("Case %d: %d\n",t,m+1);
            continue;
        }
        r=1LL*r*get_inv(rr)%mod;
        a=BSGS(Pow(k-1,n),r,mod);
        printf("Case %d: %d\n",t,a+m+1);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/8966689.html

时间: 2024-08-26 10:44:33

uva11916 Emoogle Grid (BSGS)的相关文章

中文版kendoUI API — Grid(一)

1.altRowTemplate 类型:Function | String 说明:提供表格行的交替模板,默认grid表格为每一个数据元素提供一个tr 注意:模板中最外层的html元素必须是<tr>,这个<tr>必须有一个uid属性,并设置为#= uid #,grid使用uid属性判定绑定行的元素. Example: 通过Function方式提供模板 1 <div id="grid"></div> 2 <script id="

selenium Grid(一)

selenium grid Quick Start selenium-grid是用于设计帮助我们进行分布式测试的工具,其整个结构是由一个hub节点和若干个代理节点组成.hub用来管理各个代理节点的注册和状态信息,并且接受远程客户端代码的请求调用,然后把请求的命令再转发给代理节点来执行. 1.启动Selenium-Server 1)下载 selenium-server-standalone-*.jar,地址:http://selenium-release.storage.googleapis.co

UVA - 11916 Emoogle Grid (离散对数取模)

You have to color an M x N (1M, N108) two dimensional grid. You will be provided K (2K108) different colors to do so. You will also be provided a list of B (0B500)list of blocked cells of this grid. You cannot color those blocked cells. A cell can be

[uva11916] Emoogle Grid (离散对数)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud  Emoogle Grid  You have to color an MxN ( 1M, N108) two dimensional grid. You will be provided K ( 2K108) different colors to do so. You will also be provided a list of B ( 0B500) list of blo

ALV GRID(2) 详解3 颜色

ALV GRID颜色设置方式分为三种,行.列.单元格 零.颜色 颜色字段为char4类型 一.行 1.首先在显示的内表中定义颜色字段,类型为c(4),如 color(4) type c 2.ALV布局种指定颜色字段 data: ls_lay type slis_layout_alv. "alv布局 ls_lay-info_fieldname = 'COLOR'. 二.列 设置FIELDCAT就可以了,例如 DATA:lth_cat type slis_fieldcat_alv, ltd_cat

Paths on a Grid(规律)

Paths on a Grid Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 23270   Accepted: 5735 Description Imagine you are attending your math lesson at school. Once again, you are bored because your teacher tells things that you already mastere

Discrete Logging POJ - 2417(BSGS)

Discrete Logging POJ - 2417 题意:给P,B,N,求最小的L使得 BL≡N (mod P) Baby Step Giant Step 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <cmath> 5 #define ll long long 6 using namespace std; 7 const int maxn=76543;

[Sdoi2013]随机数生成器(BSGS)

#include<map> #include<cmath> #include<cstdio> #include<iostream> #define ll long long using namespace std; inline int read(){ int x=0;char ch=getchar(); while(ch<'0'||ch>'9')ch=getchar(); while(ch>='0'&&ch<='9'

ALV GRID(1) 例子

ALV GRID 应该是最常用的显示方式了,下面我举一个例子:查询物料描述及物料组 1 REPORT ZTEST01. 2 3 tables:mara,makt. 4 5 type-pools:slis. 6 7 types:begin of typ_mara, 8 matnr type mara-matnr, 9 maktx type makt-maktx, 10 matkl type mara-matkl, 11 end of typ_mara. 12 data:gt_mara type s