HOJ 1096 Divided Product (DFS)

时间限制: 10000ms
单点时限: 1000ms
内存限制: 256MB

描述

Given two positive integers N and M, please divide N into several integers A1, A2, ..., Ak (k >= 1), so that:

1. 0 < A1 < A2 < ... < Ak;

2. A1 + A2 + ... + Ak = N;

3. A1, A2, ..., Ak are different with each other;

4. The product of them P = A1 * A2 * ... * Ak is a multiple of M;

How many different ways can you achieve this goal?

输入

Two integers N and M. 1 <= N <= 100, 1 <= M <= 50.

输出

Output one integer -- the number of different ways to achieve this goal, module 1,000,000,007.

样例提示

There are 4 different ways to achieve this goal for the sample:

A1=1, A2=2, A3=4;

A1=1, A2=6;

A1=2, A2=5;

A1=3, A2=4.

样例输入
7 2
样例输出
4

题意:给一个数N,能分解成多少个数,这些数满足题目所说的4个条件。问有多少种情况?
思路:看到N最大只有100,1+2+3...+14>100最多14层递归。用DFS找出所有情况。
感想:1.其中有个乘法要注意下数据范围,第一用了int,WA了,后来改用long long。
2.题目数据貌似水了,这题要求GCD,目前还没看懂,先贴个水过的代码,以后再补上。

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std;

const int INF=0x3f3f3f3f;
const double eps=1e-10;
const double PI=acos(-1.0);
#define maxn 500
int vis[maxn],a[maxn];
int n, m, cnt;
void dfs(int sum, long long mul, int pos, int num)
{
    if(sum == n && mul%m ==0)
    {
//        for(int i = 0; i < num; i++)
//            printf("%d ", a[i]);
//        printf("\n");
        cnt++;
        return;
    }

    for(int i = pos; i <= n; i++)
    {
        if(sum + i > n)
            break;
        a[num] = i;
        dfs(sum+i, mul*i, i+1, num+1);
    }
}
int main()
{
    scanf("%d%d", &n, &m);
    cnt = 0;
    dfs(0,1,1,0);
    printf("%d\n", cnt%1000000007);
    return 0;
}

 

转载于:https://www.cnblogs.com/ZP-Better/p/4937554.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值