版权声明:反正也没有人会转,下一个 https://blog.csdn.net/drtlstf/article/details/83474550
Description
fiile name: problem
编程求出所有的 n 位数中,有多少个数中有偶数个数字 3。
Input
一行一个正整数 n,0
Output
一行一个正整数,表示 n 位数中有多少个数有偶数个 3,(由于结果可能很大,你只需要输出这个答案mod 12345的值。)。
Sample Input
2
Sample Output
73
同样DFS记忆化
#include <iostream>
#include <cstdio>
#include <cstring>
#define SIZE 1010
using namespace std;
int dp[SIZE][2], n;
int dfs(int pos, bool flag) // pos当前的位置, flag是否有偶数个3
{
int i, res = 0;
if (pos == n) // 到达边界
{
return flag;
}
if ((~dp[pos][flag]) && (pos)) // 如果以前找过就返回以前的答案
{
return dp[pos][flag];
}
for (i = (pos) ? 0 : 1; i < 10; ++i)
{
if (i == 3)
{
res += dfs(pos + 1, !flag);
}
else
{
res += dfs(pos + 1, flag);
}
res %= 12345;
}
return dp[pos][flag] = res; // 记录结果
}
int main(void)
{
scanf("%d", &n);
memset(dp, -1, sizeof (dp));
printf("%d", dfs(0, true));
return 0;
}