forked from sunildinday/my-algo-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigit-dp2.cpp
58 lines (53 loc) · 873 Bytes
/
digit-dp2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Problem Link:http://www.spoj.com/problems/NY10E/
*/
#include<bits/stdc++.h>
using namespace std;
long long int dp[65][10];
std::vector<int> digit;
long long int digitdp(int idx,int tight,int lastdigit)
{
if(idx==-1)
{
return 1;
}
if(!tight&&dp[idx][lastdigit]!=-1)
return dp[idx][lastdigit];
int newdigit;
if(tight)
newdigit=digit[idx];
else
newdigit=9;
long long int ans=0;
for(int d=0;d<=newdigit;d++)
{
if(d>=lastdigit)
ans+=digitdp(idx-1,tight&&(d==newdigit),d);
}
if(!tight)
dp[idx][lastdigit]=ans;
return ans;
}
long long int solve(long long int num)
{
digit.clear();
while(num)
{
digit.push_back(9);
num--;
}
return digitdp(digit.size()-1,1,0);
}
int main()
{
long long int a,b;
int p;
cin>>p;
while(p--)
{
cin>>b>>a;
memset(dp,-1,sizeof(dp));
cout<<b<<" "<<solve(a)<<endl;
}
return 0;
}