【暴力搜索】分数分解
直接搜索从0开始一直到1,然后搜索过程中只能有一个分母大于m所以,如果当前是sum,已经枚举了k个那么
#include <iostream>
#include <cmath>
#include <cstdio>
#define MAXN 10
using namespace std;
int N,cnt,M;
int sa,sb,sc;
int zz(int a,int b){
sa = a,sb = b;
while(sb!=0){
sc = sb;
sb = sa%sb;
sa = sc;
}
return sa;
}
bool GetNum(int up,int xm,int xs){
if(xm%xs == 0&&xm/xs>=up){
//Test(xm/G);
return true;
}
return false;
}
void dfs(int i,int up,int Fm,int Fs){
if(Fm<=Fs) return ;
if(i == N){
if(GetNum(up,Fm,Fm-Fs))
cnt++;
return ;
}
if(Fs>1000000){
int G = zz(Fm,Fs);
Fm /= G;
Fs /= G;
}
if(Fs*M + (N-i)*Fm > Fm*M)
return ;
if(Fs*up + (N-i+1)*Fm <up*Fm)
return ;
for(int j=up;j<=M;j++)
dfs(i+1,j,Fm*j,Fs*j+Fm);
}
int main()
{
cin>>N>>M;
if(N == 1){
cout<<1;
return 0;
}
dfs(1,2,1,0);
cout<<cnt;
return 0;
}