《ybtoj高效进阶》第五部分第四章例题5 山谷数

题目大意

当一个数字,从高位到低位上的数字没有出现先递增接着递减的现象,就被称作山谷数。求 [ 1 , n ] [1,n] [1,n]的山谷数有多少。多组数据,结果对 1 e 9 + 7 1e9+7 1e9+7取模。

思路

套路,设 f i , j , k f_{i,j,k} fi,j,k为填i位,第i位为j,目前上升(k=1)和下降(k=2)状态为k(0表示未知)
记得判前导0。
code:

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstring>
using namespace std;
//When I wrote this code,only God and me know unterstood was I doing
//Now, God only knows
unsigned long long t,f[311][300][3],myd=1e9+7;
string n;
unsigned long long dfs(int pos,int ud,bool tjh,int las,bool wj)
{
	if (pos==n.size()) return !wj;
	if (!tjh&&f[pos][las][ud]!=-1) return f[pos][las][ud];
	int mx=(tjh?(n[pos]-'0'):9);
	unsigned long long ans=0;
	for (int i=0;i<=mx;i++)
	{
		if (i<las&&ud==1) continue;
		if (wj) ans+=dfs(pos+1,ud,tjh&&(i==mx),i,(i==0)&&wj),ans%=myd;
		else
		{
			if (i==las) ans+=dfs(pos+1,ud,tjh&&(i==mx),i,0),ans%=myd;
			else ans+=dfs(pos+1,(i<las?2:1),tjh&&(i==mx),i,0),ans%=myd;
		}
	}
	if (!tjh) return f[pos][las][ud]=ans;
	return ans;
}
unsigned long long kyx(string n)
{
	memset(f,-1,sizeof(f));
	return dfs(0,0,1,0,1);
}
int main()
{
	cin>>t;
	while (t--)
	{
		cin>>n;
		cout<<kyx(n)<<endl;
	}
	return 0;
}

《关于<ybtoj高效进阶>例题全部AC这档事》
完结撒花

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值