Time Limit: 15000MS Memory Limit: 128000K Total Submissions: 7084 Accepted: 2431 Case Time Limit: 5000MS

Description

1 <= n <= 6；1 <= M <= 150。

31。

★本题中，指数Pi(i=1,2,...,n)均为正整数。

Input

Output

Sample Input

3150 1 2 -1 2 1 2

Sample Output

178

``````#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int MAX = 4000000;
struct Hash
{
int val;
int count;
}HashTable[MAX];
int n,m,ans;
int k[6],p[6];
bool used[MAX];

int getpow(int x, int p)
{
int tmp = 1;
while(p)
{
if(p & 1) tmp *= x;
x *= x;
p >>= 1;
}
return tmp;
}

int searchHash(int s)
{
int tmp = s;
tmp = (tmp % MAX + MAX) % MAX;
while(used[tmp] && HashTable[tmp].val != s)
{
tmp++;
tmp = (tmp % MAX + MAX) % MAX;
}
return tmp;
}

void insert(int s)
{
int pos = searchHash(s);
HashTable[pos].val = s;
HashTable[pos].count++;
used[pos] = true;
}

void leftHalf(int d,int s)
{
if(d == n / 2)
{
insert(s);
return;
}
for(int i = 1; i <= m; i++)
leftHalf(d+1,s + k[d] * getpow(i,p[d]));
}

void rightHalf(int d,int s)
{
if(d == n)
{
s = -s;
int pos = searchHash(s);
if(HashTable[pos].val == s)
ans += HashTable[pos].count;
return;
}
for(int i = 1; i <= m; i++)
rightHalf(d+1,s + k[d] * getpow(i,p[d]));
}

int main()
{
scanf("%d%d",&n,&m);
for(int i = 0; i < n; i++)
scanf("%d%d",&k[i],&p[i]);
leftHalf(0,0);
rightHalf(n/2,0);
printf("%d\n",ans);
return 0;
}``````