Arcueid likes nya number very much.
A nya number is the number which has exactly X fours and Y sevens(If X=2 and Y=3 , 172441277 and 47770142 are nya numbers.But 14777 is not a nya number ,because it has only 1 four).
Now, Arcueid wants to know the K-th nya number which is greater than P and not greater than Q.
A nya number is the number which has exactly X fours and Y sevens(If X=2 and Y=3 , 172441277 and 47770142 are nya numbers.But 14777 is not a nya number ,because it has only 1 four).
Now, Arcueid wants to know the K-th nya number which is greater than P and not greater than Q.
The second line contains 4 non-negative integers: P,Q,X and Y separated by spaces.
( 0<=X+Y<=20 , 0< P<=Q <2^63)
The third line contains an integer N(1<=N<=100).
Then here comes N queries.
Each of them contains an integer K_i (0<K_i <2^63).
For each query, output a line contains an integer number, representing the K_i-th nya number in (P,Q].
If there is no such number,please output "Nya!"(without the quotes).
1 38 400 1 1 10 1 2 3 4 5 6 7 8 9 10
Case #1: 47 74 147 174 247 274 347 374 Nya! Nya!
给定X和Y的值,一个数字用十进制表示,对于数位4出现恰好X次,7恰好Y次的数字称为nya数。然后给P和Q, 后面是一系列查询, 每个查询K就是在区间(P,Q]上找到第K个nya数,不存在则输出Nya!
思路:
数位dp
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
int t;
ll P,Q,X,Y;
int n;
int bit[20];
ll dp[20][20][20];
ll dfs(int len,int sta1,int st2,bool flag)
{
if(sta1 > X || st2 > Y) return 0;
if(len <= 0) return sta1 == X && st2 == Y;
if(!flag && dp[len][sta1][st2] != -1)
return dp[len][sta1][st2];
int end = flag ? bit[len] : 9;
ll res = 0;
for(int i=0;i<=end;i++)
{
if(i == 4)
res += dfs(len-1,sta1+1,st2,flag && i == end);
else if(i == 7)
res += dfs(len-1,sta1,st2+1,flag && i == end);
else
res += dfs(len-1,sta1,st2,flag && i == end);
}
return flag ? res : dp[len][sta1][st2] = res;
}
ll cal(ll x)
{
int pos= 0;
while(x> 0)
{
bit[++pos] =x% 10;
x/= 10;
}
return dfs(pos,0,0,1);
}
int main()
{
int cas=1;
scanf("%d",&t);
while(t--)
{
printf("Case #%d:\n",cas++);
memset(dp,-1,sizeof(dp));
scanf("%I64d%I64d%I64d%I64d",&P,&Q,&X,&Y);
ll dn=cal(P);
ll up=cal(Q);
scanf("%d",&n);
while(n--)
{
ll m;
scanf("%I64d",&m);
ll l = P + 1;
ll r = Q;
if(m > up -dn)
{
printf("Nya!\n");
continue;
}
while(l < r)
{
ll mid = (l + r) / 2;
ll s=cal(mid);
if(s - dn>= m )
r = mid;
else
l = mid + 1;
}
printf("%I64d\n",l);
}
}
return 0;
}