题目描述
The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the time bomb. The number sequence of the time bomb counts from 1 to N. If the current number sequence includes the sub-sequence "49", the power of the blast would add one point.
Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them?
输入
The first line of input consists of an integer T (1 <= T <= 10000), indicating the number of test cases. For each test case, there will be an integer N (1 <= N <= 2^63-1) as the description.
The input terminates by end of file marker.
输出
For each test case, output an integer indicating the final points of the power.
样例输入 Copy
3
1
50
500
样例输出 Copy
0
1
15
提示
From 1 to 500, the numbers that include the sub-sequence "49" are "49","149","249","349","449","490","491","492","493","494","495","496","497","498","499",
so the answer is 15.
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef vector<string> VS;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
inline int log_2(int x) {return 31-__builtin_clz(x);}
inline int popcount(int x) {return __builtin_popcount(x);}
inline int lowbit(int x) {return x&-x;}
ll f[20][2],b[20];
ll dfs(int pos,bool is_4,bool is_max)
{
if(!pos) return 1;
if(!is_max&&(~f[pos][is_4])) return f[pos][is_4];
ll ans = 0;
int m = is_max?b[pos]:9;
for(int i=0;i<=m;++i)
{
if(is_4&&i==9) continue;
ans += dfs(pos-1,i==4,is_max&&i==m);
}
if(!is_max) f[pos][is_4] = ans;
return ans;
}
//dig_dp搜索的是从0到x的合法解
ll dig_dp(ll x)
{
int len = 0;
//注意此处的b数组写成while
while(x) b[++len] = x%10,x/=10;
return dfs(len,0,1);
}
void solve()
{
ll n;
cin>>n;
cout<<n+1-dig_dp(n)<<'\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
memset(f,-1,sizeof f);
int T;
cin>>T;
while(T--)
{
solve();
}
}
标签:typedef,return,sequence,int,ll,pos,Bomb,DP,数位
From: https://www.cnblogs.com/ruoye123456/p/18377225