Emoogle Grid (方案数,逆元求解,对数求解)

来源:互联网 发布:虚拟软件是什么 编辑:程序博客网 时间:2024/06/05 13:41
You have to color an M × N (1 ≤ M,N ≤ 108) two dimensional grid. You will be provided K (2 ≤ K ≤ 108) 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 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 ≤ 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.
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,b,r。即现在有m*n的矩阵,然后要用k种颜色涂满矩阵,但是矩阵中存在b个障碍,即这b个障碍是不能涂色的,而且涂色有规定相邻颜色不能相同。现在已知矩阵的n,以及总方案数mod(1e8+7)等于r。要求最小的可能的m。

题解:先将划出一个刚好容纳下b个障碍的矩阵1,然后求出该矩阵1的方案数,如果mod(1e8+7)等于r,则答案等于矩阵1的m。然后再加上一行,继续求方案数,得到ans,如果它mod(1e8+7)等于r,则答案等于矩阵1的m+1。如果上面两个都不成立,那么可以考虑有以下等式

ans(上面求得的ans)*pow(pow(k-1,x),n) ==  r(mod(1e8+7))//这里的x为我们要求的量。

然后我们只要求出ans的逆元,即可变为下式:

pow(t,x)==r*inv(ans)(%mod)//t=pow(k-1,n)。

然后这条式子即是求p^x==q(%mod)

我们将式子拆解得p^i*p^m==q(%mod)   //m==sqrt(mod),然后我们只需要枚举i,将答案存下,然后再枚举q乘上(p^m)的逆元,如果在原答案中找到相等的,即为最终答案。(这一步算是折半枚举吧)

代码:

#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <map>#include <set>#include <vector>#include <map>#include <bitset>#include <algorithm>#include <queue>#include <stack>#include <cmath>using namespace std;typedef long long ll;const int maxn = 1e6+7;const int INF = 1e9;const ll LINF = 1e18;const int MINF = -1e9;double EPS = 1e-7;const int mod = 100000007;ll n,k,r,b,lm;struct P{    ll x,y;} s[505];ll mod_pow(ll a,ll b){    ll ans = 1;    while(b){        if(b&1){            ans*=a;            ans%=mod;        }        a*=a;        a%=mod;        b>>=1;    }    return ans%mod;}bool cmp(P a,P b){    if(a.y==b.y) return a.x<b.x;    else return a.y<b.y;}void exgcd(ll a,ll b,ll &x,ll &y){    if(b!=0){        exgcd(b,a%b,y,x);        y-=(a/b)*x;    }    else {        x = 1,y = 0;    }}ll inv(ll a){    ll x,y;    exgcd(a,mod,x,y);    return (x%mod+mod)%mod;}ll log_mod(ll a,ll b){    ll m = (ll)sqrt(mod+0.5);    map<ll,ll> x;    x[1] = 0;    ll ans = 1;    for(int i = 1;i <= m;i++){        ans = ans*a%mod;        if(!x.count(ans)) x[ans] = i;    }    ll pm = inv(mod_pow(a,m));    for(int i = 0;i < m;i++){        if(x.count(b)) return i*m+x[b];        b = b*pm%mod;    }}void solve(){    lm = 1;    for(int i = 0;i < b;i++)    {        scanf("%d %d",&s[i].x,&s[i].y);        lm = max(lm,s[i].x);    }    sort(s,s+b,cmp);    ll ans = 1;    ll k1 = n;    for(int i = 0;i < b;i++){        if(s[i].x==1) k1--;        if(s[i].x!=lm){            if(i+1<b&&s[i+1].y==s[i].y){                if(s[i].x+1!=s[i+1].x){                    k1++;                }            }            else k1++;        }    }    ans = ans*mod_pow(k,k1)%mod;    ans = ans*mod_pow(k-1,n*lm-k1-b)%mod;    if(ans==r){        printf("%lld\n",lm);        return;    }    k1 = 0;    for(int i = 0;i < b;i++){        if(s[i].x == lm) k1++;    }    ans = ans*mod_pow(k,k1)%mod;    ans = ans*mod_pow(k-1,n-k1)%mod;    if(ans==r){        printf("%lld\n",lm+1);        return;    }    ll x = inv(ans);    r*=inv(ans);    r%=mod;    k=mod_pow(k-1,n);    printf("%lld\n",log_mod(k,r)+lm+1);    return;}int main(){    int t;    scanf("%d",&t);    int f = 1;    while(t--){        scanf("%lld %lld %lld %lld",&n,&k,&b,&r);        printf("Case %d: ",f++);        solve();    }    return 0;}


原创粉丝点击