子2023

【问题描述】
小蓝在黑板上连续写下从 1 到 2023 之间所有的整数,得到了一个数字序列:
S = 12345678910111213 . . . 20222023。
小蓝想知道 S 中有多少种子序列恰好等于 2023?
提示,以下是 3 种满足条件的子序列(用中括号标识出的数字是子序列包含的数字):
1[2]34567891[0]111[2]1[3]14151617181920212223…
1[2]34567891[0]111[2]131415161718192021222[3]…
1[2]34567891[0]111213141516171819[2]021222[3]…
注意以下是不满足条件的子序列,虽然包含了 2、0、2、3 四个数字,但是顺序不对:
1[2]345678910111[2]131415161718192[0]21222[3]…

1.dp写法

#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define endl '\n'
typedef long long LL;
typedef pair<int,int> PII;
string s;
LL dp[5];

int main()
{
 	IOS;
	
	for(int i=1;i <= 2023;i++)	s+=to_string(i);

	for(int i=0;i < s.size();i++)
	{
		if(s[i] == '2')
		{
			dp[1]++;
			dp[3]+=dp[2];
		}
		
		if(s[i] == '0') dp[2]+=dp[1];
		if(s[i] == '3') dp[4]+=dp[3];
	}
		
	cout<<dp[4];
	
 	return 0;
}


2.剪枝后暴力

#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define endl '\n'
typedef long long LL;
typedef pair<int,int> PII;
string s;
LL ans=0;
string op="2023";

void dfs(int u,int cnt)
{
	if(cnt == 4)
	{
		ans++;
		return ;
	}
	if(u >= s.size()) return ;
	
	if(op[cnt] == s[u]) dfs(u+1,cnt+1);
	dfs(u+1,cnt);
}

int main()
{
 	IOS;
	
	for(int i=1;i <= 2023;i++)
	{
		string t=to_string(i);
		for(int j=0;j < t.size();j++)
		{
			if(t[j] == '2' || t[j] == '0' || t[j] == '3')
			s+=t[j];	
		}	
	} 	
	
	dfs(0,0);
	
	cout<<ans<<endl;
	
 	return 0;
}

枚举四个位置,只有数字放到该放的位置,才能填下一个位置,也可以选择不填。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值