Integer partitioning Problem--dfs

Source: Internet
Author: User

Single point time limit: 1000ms
Memory Limit: 256MB

Describe
Given positive integers n and M, please divide N into several integers A1, A2, ..., Ak (k >= 1), so:
1.0 < A1 < A2 < ... < Ak;
2. A1 + A2 + ... + Ak = N;
3. A1, A2, ..., Ak is different with each other;
4. The product of them P = A1 * A2 * ... * Ak is a multiple of M;
How many different ways can I achieve this goal?
Input
integers n and M. 1 <= n <=, 1 <= M <= 50.
Output
Output one integer–the number of different ways to achieve this goal, module 1,000,000,007.
Sample input
7 2
Sample output
4
Sample Hints
There is 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.

The idea is violence, try all the things.

#include <iostream>using namespace STD;typedef unsigned Long LongUll;ull N, M;ull r =0;Constull MOD =1000000007;voidDFS (ull i, ull s, ull p) {if(s = = N) {if(p% M = =0) r++;return; } for(Ull j = i +1; J < N-S +1;    J + +) {Dfs (J, S + J, p * j); }}intMain () {Cin>> N >> M; DFS (0,0,1);cout<< r% MOD << Endl;return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Integer partitioning Problem--dfs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.