计蒜客 方程的解个数
#include <bits/stdc++.h>
using namespace std;
const int max_n = 10;
int coeffs[max_n];
int exps[max_n];
int n, m;
int memory[160][10];
int ans = 0;
//the key important ,prepare process the data, or will TLE
void preProc() {
for(int i = 1; i <= m; ++i) {//x取值范围为[1 m]
for(int j = 1; j <= n; ++j) {
if( j == 1) {
memory[i][j] = i;
continue;
}
memory[i][j] = memory[i][j-1] * i;
}
}
}
void dfs(int idx, int total) {//idx:算到了第idx个,total算出来的总值
if(idx == n + 1) {
if(total == 0) {//找到1个解
ans++;
return;
}
return;//没有解也要返回
}
for(int val = 1; val <= m; ++val) {//尝试x取val
int coeff = coeffs[idx];
int exp = exps[idx];
int temp = memory[val][exp] * coeff;
total += temp;//使用val
dfs(idx+1, total);
total -= temp;//不使用val
}
}
int main() {
//freopen("data.in", "r", stdin);
memset(coeffs, -1, sizeof(coeffs));
memset(exps, -1, sizeof(exps));
memset(memory, -1, sizeof(memory));
scanf("%d", &n);
scanf("%d", &m);
int k, p;
for(int i = 1; i <= n; ++i) {
scanf("%d %d", &coeffs[i], &exps[i]);
}
preProc();
dfs(1, 0);
printf("%d\n", ans);
return 0;
}