E. Living Sequence

题目:
有一个巧妙的解法:
考虑这个问题, 从一个没有限制的从1开始的递增序列找出第k个数, 显然就是十进制的k。而这里则可以定义新的进制为 "012356789" 9进制, 那么k对应的就是这个特殊的九进制数, 我们只需要把它转换为十进制就行。
代码:

#define _CRT_SECURE_NO_WARNINGS 
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
typedef long long ll;
using namespace std;

stack<ll>st;
int main()
{
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	ll t; cin >> t;
	while (t--)
	{
		ll n; cin >> n;
		while (n > 0)
		{
			ll x = n % 9; if (x >= 4)x++;
			st.push(x); n /= 9;
		}
		while (!st.empty()) { cout << st.top(); st.pop(); }
		cout << '\n';

	}
	return 0;
}

或者数位dp:定义dp[i][j]为长度为i, 且最高位为j的含4的数的数量。然后相减与条件符合的就是答案。

posted @ 2024-05-31 16:15  WHUStar  阅读(22)  评论(0)    收藏  举报