Game of Dice Gym

来源:互联网 发布:淘宝达人是淘客吗 编辑:程序博客网 时间:2024/05/06 13:48
#include<stdio.h>#include<iostream>#include<algorithm>#include<string>#include<string.h>#include<map>#define LL long longusing namespace std;const int MOD=1e9+7;int diec[20][10];map<LL,int> M;long long ans=0;int x;long long pow(long long x,int n){    long long y=1;    long long u=x%MOD;    while(n)    {        if(n&1) y=y*u%MOD;        n>>=1;        u=u*u%MOD;    }    return y;}void dfs(int s,int e,int flag,long long res){    if(e==s)    {        if(flag)        {            ++M[res];        }        else        {            ans+=M[x*pow(res,MOD-2)%MOD];        }    }    else    {        for(int i=0;i<6;i++)        {            dfs(s+1,e,flag,res*diec[s][i]%MOD);        }    }}int main(){    int T;    scanf("%d",&T);    while(T--)    {        ans=0;        M.clear();        int n;        scanf("%d%d",&n,&x);        for(int i=0;i<n;i++)        {            for(int j=0;j<6;j++)            {                scanf("%d",&diec[i][j]);            }        }        dfs(0,n/2,1,1);        dfs(n/2,n,0,1);        cout<<ans<<endl;    }}

It is always fun to play with dice, here is one interesting game:

You are given n fair dice with 6 faces, the faces does not necessarily have values from 1 to 6 written on them. instead, they can have any values from 1 to 100 written on them.

Suppose you threw the n dice one after another, let us define the result of the throws as the multiplication of the numbers you get from each dice mod109 + 7.

You task is to find how many different ways you can get the result of the dice throws equal tox.


Input

The first line contains an integer T, whereT is the number of test cases.

The first line of each test case contains two integers n and x (1 ≤ n ≤ 14) (0 ≤ x < 109 + 7), wheren is the number of the dice and x is the required result from the throws.

Then n lines follow, the ith line contains 6 integersfi1, fi2, ..., fi6(1 ≤ fij ≤ 100), giving the values ofith dice's faces.

Output

For each test case, print a single line containing how many different ways you can get the result of the dice throws equal tox

Example
Input
33 91 2 3 4 5 61 2 3 4 5 61 2 3 4 5 65 61 2 9 9 9 91 2 9 9 9 91 3 9 9 9 91 3 9 9 9 91 6 9 9 9 95 999999937100 1 1 1 1 1100 1 1 1 1 1100 1 1 1 1 1100 1 1 1 1 1100 1 1 1 1 1
Output
351

题意: 你有n个骰子,每个骰子的六个面已经给出,问你有多少种可能 使得这些骰子投出的数的乘积对1e9+7取模后=x、


一个裸的折半搜索题。

一开始想直接暴搜,然后发现复杂度高达6^14 肯定过不了。

然后发现这是个配对问题,所以可以用折半搜索。

对前半部分用map存一下搜索后的结果。

然后后半部分搜索时配对一下就行。

由于涉及到除法取模,快速幂求一下逆元就好。

复杂度应该是6^(n/2)再乘个log。


原创粉丝点击