紫书 习题 10-10 UVa 1645(递推)

除了根节点以外,有n-1个节点,然后就看n-1的因数有那些,所有因数加起来(递推)就好了。

#include<cstdio>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;

const int MOD = 1e9 + 7;
const int MAXN = 1123;
int ans[MAXN];

void init()
{
	ans[1] = 1;
	REP(i, 2, MAXN)
		REP(j, 1, i)
			if((i - 1) % j == 0)
				ans[i] = (ans[i] + ans[j]) % MOD;
}

int main()
{
	init();
	int a, kase = 0;
	while(~scanf("%d", &a))
		printf("Case %d: %d\n", ++kase, ans[a]);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_34416123/article/details/81167106