uva11916 Emoogle Grid

137 篇文章 0 订阅

You have to color an M N (1 M;N 10 8 ) two dimensional grid.
You will be provided K (2 K 10 8 ) different colors to do so. You
will also be provided a list of B (0 B 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. Now the great problem setter smiled with emotion and thought that he would ask the contestants to
nd 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 nd 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 redis- covered 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 nd the input generator and his codes, but
luckily he has the input le and the correct answer le. So, he asks
your help to regenerate the data. Yes, you are given the input le
which contains all the information except the
number of rows’ and the
answer le; you have to nd the number of rows he might have used for
this problem. Input Input starts with an integer T ( T 150),
denoting the number of test cases. Each test case starts with a line
containing four integers N , K , B and R (0 R< 100000007) which
denotes the result for this case. Each of the next B lines will
contains two integers x and y (1 x M , 1 y 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.

先确定至少出现的列数【最大的不涂色格子】,计算方案数【上面没有涂色格有k种涂法,否则有k-1种涂法】验证,然后加上一行计算【方法同上】验证,再接下来每加上一行的情况就相同了,也就是解方程now*((k-1)^n)^m=r(now是当前答案,m是要求的解),用BSGS求离散对数。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define LL long long
#define ans ban
const LL mod=100000007;
struct ban
{
    LL x,y;
    bool operator < (const ban &bb) const
    {
        return x<bb.x||(x==bb.x&&y<bb.y);
    }
}a[510];
ans f[50010];
LL n,k,r;
int b;
void init()
{
    int i;
    scanf("%lld%lld%d%lld",&n,&k,&b,&r);
    a[1].x=a[1].y=0;
    for (i=1;i<=b;i++)
      scanf("%lld%lld",&a[i].x,&a[i].y);
    sort(a+1,a+b+1);
}
bool find(ban bb)
{
    int l=1,r=b,mid;
    while (l<r)
    {
        mid=(l+r)/2;
        if (a[mid]<bb) l=mid+1;
        else r=mid;
    }
    return (!(bb<a[l]))&&(!(a[l]<bb));
}
LL pow(LL base,LL p)
{
    LL ret=1;
    while (p)
    {
        if (p&1) ret=ret*base%mod;
        base=base*base%mod;
        p>>=1;
    }
    return ret;
}
LL solve()
{
    int i,j,num,ll,rr,mid;
    LL tot,cnt=n,now,x,m,inv,u,edge;
    edge=b?a[b].x:1;
    tot=n*edge;
    for (i=1;i<=b;i++)
    {
        if (a[i].x<edge&&!find((ban){a[i].x+1,a[i].y}))
          cnt++;
        if (a[i].x==1) cnt--;
    }
    now=pow(k,cnt)*pow(k-1,tot-cnt-b)%mod;
    if (now==r) return edge;
    cnt=n;
    for (i=1;i<=b;i++)
      if (a[i].x==edge) cnt--;
    now=now*pow(k-1,cnt)%mod*pow(k,n-cnt)%mod;
    if (now==r) return edge+1;
    /*now*((k-1)^n)^m=r*/
    r=r*pow(now,mod-2)%mod;
    x=pow(k-1,n);
    m=sqrt(mod);
    u=1;
    f[1]=(ans){1,0};
    for (i=1;i<m;i++)
    {
        u=u*x%mod;
        f[i+1]=(ans){u,i};
    }
    inv=pow(u*x%mod,mod-2);
    sort(f+1,f+m+1);
    for (i=0;;i++)
    {
        ll=1;
        rr=m;
        while (ll<rr)
        {
            mid=(ll+rr)/2;
            if (f[mid].x>=r) rr=mid;
            else ll=mid+1;
        }
        if (f[ll].x==r) return f[ll].y+i*m+edge+1;
        r=r*inv%mod;
    }
}
int main()
{
    int T,K=0;
    scanf("%d",&T);
    while (T--)
    {
        init();
        printf("Case %d: %lld\n",++K,solve());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值