题目:
https://www.jisuanke.com/course/2291/182237
思路:
来自:https://blog.csdn.net/qq_29980371/article/details/76599695
dfs(int cnt, int curVal)//cnt是k,p的下标,curVal当前的和值
从1到m遍历进入dfs,然后不停dfs,进行查找,满足条件将结果加一,cnt临界就跳出循环,
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<queue> 6 using namespace std; 7 //计蒜客 方程的解数 8 int n, m, k[5], p[5], sum; 9 int poww[151][5]; 10 void init() 11 { 12 //memset(poww, 1, sizeof(poww)); 13 for (int i = 0; i <= 150; i++) 14 poww[i][0] = 1; 15 for (int i = 1; i <= 150; i++) 16 { 17 for (int j = 1; j <= 4; j++) 18 { 19 poww[i][j] = i*poww[i][j - 1]; 20 } 21 } 22 } 23 24 void dfs(int cnt, int curVal)//cnt是k,p的下标,curVal当前的和值 25 { 26 if (cnt == n && curVal == 0)//满足方程式,sum++ 27 { 28 sum++; 29 return; 30 } 31 if (cnt == n )//跳出循环 32 return; 33 for (int i = 1; i <= m; i++) 34 { 35 dfs(cnt + 1, curVal + k[cnt] * poww[i][p[cnt]]); 36 } 37 } 38 39 int main() 40 { 41 while (scanf("%d%d", &n, &m) == 2) 42 { 43 for (int i = 0; i<n; i++) 44 scanf("%d%d", &k[i], &p[i]); 45 46 sum = 0; 47 init(); 48 dfs(0, 0); 49 printf("%d\n", sum); 50 } 51 return 0; 52 }