计蒜客 方程的解个数(dfs搜索)

计蒜客 方程的解个数

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值