P1025 数的划分
题解
时隔许久打个dfs
为了防止重复,我们划分数字的时候按照升序划分
代码
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<string> #include<cstring> #include<queue> #include<cmath> using namespace std; typedef long long ll; inline int read() { int ans=0; char last=' ',ch=getchar(); while(ch<'0'||ch>'9') last=ch,ch=getchar(); while(ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar(); if(last=='-') ans=-ans; return ans; } int n,m,ans=0; int dfs(int k,int pre,int res) { int ret=0; if(k==m) return res==0; if(res<=0) return 0; for(int i=pre;(i+m-k-1)<=res;i++){ ret=ret+dfs(k+1,i,res-i); } return ret; } int main() { n=read();m=read(); ans=dfs(0,1,n); printf("%d ",ans); return 0; }