求助大佬为何最后一点TLE了,求助大佬
查看原帖
求助大佬为何最后一点TLE了,求助大佬
853953
ljx_gkx楼主2023/3/27 19:13
#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

const int N = 20;
int path[N];	//记录方案!
int n, ans;		//火柴数,方案数! 
int d[N] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};

void dfs(int u, int cost)
{
	if (cost > n) return ;
	if (cost == n && u == 4)
	{
		if (path[1] + path[2] == path[3])
			ans ++;
		return ;
	}
	
	for (int i=0; i <= 1000; i ++)
	{
		path[u] = i;
		int sum=0, t = i;
		if (!t) sum += d[t];
		while (t)
		{
			sum += d[t%10];
			t /= 10;
		}
		dfs(u+1, cost + sum);
		path[u] = 0;
	}
}

int main()
{
	cin >> n;
	n -= 4;
	dfs(1, 0);
	cout << ans << endl;
	return 0;
}
2023/3/27 19:13
加载中...