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

时间: 2024-11-02 23:39:15

HOJ 1096 Divided Product (DFS)的相关文章

[hihoCoder] #1096 : Divided Product

时间限制: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

POJ 3087 Shuffle&#39;m Up (DFS)

题目链接:Shuffle'm Up 题意:有a和b两个长度为n的字符序列,现定义操作: 将a.b的字符交叉合并到一个序列c,再将c最上面的n个归为a,最下面n个归为b 给出a,b和目标序列c,问最少多少次操作a.b转化为c 解析:将a.b放入哈希表,然后模拟操作过程直接dfs即可. AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <map> using names

LeetCode Subsets (DFS)

题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 return a

poj1753 Flip Game(枚举Enum+dfs)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1753 Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the o

POJ 3411 Paid Roads(dfs)

*注:这一题很重要的是对与数据的处理和细节的把握把! http://poj.org/problem?id=3411 题目大意: 有n个城市,m条路,(0<=n,m<=10).从a到b,如果之前已经经过c点,那么付费p,否者付费r.求最小的费用,从1-->n! 注意: There may be more than one road connecting one city with another. so:你不能用map[a][b]存a->b的距离.只能有road [ i ]了. 还有

POJ 1699 Best Sequence(DFS)

題目鏈接 題意 : 將幾個片段如圖所示方法縮成一個序列,求出最短這個序列. 思路 : 其實我也不知道怎麼做.....看網上都用了DP.....但是我不會.....這個DP不錯,還有用KMP+状压DP做的 1 //1699 2 #include <iostream> 3 #include <stdio.h> 4 #include <string.h> 5 #include <string> 6 7 using namespace std; 8 9 string

LeetCode Subsets II (DFS)

题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 ans.push_back(vector

poj A Knight&#39;s Journey(DFS)

题目链接:http://acm.hrbust.edu.cn/vj/index.php?c=problem-problem&id=190592 题意:给出p*q的棋盘,从(A,1)开始,走“日”字,问能否走完棋盘上所有的点,如果能,按字典序输出路径: 思路:DFS,并保存路径即可,注意处理走的方向顺序int dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}}; #include <stdio.h> #in

POJ - 1780 Code (欧拉回路+手写DFS)

Description KEY Inc., the leading company in security hardware, has developed a new kind of safe. To unlock it, you don't need a key but you are required to enter the correct n-digit code on a keypad (as if this were something new!). There are severa