Skip to content

Commit 6ef0019

Browse files
committed
.
1 parent 5ae5384 commit 6ef0019

9 files changed

+293
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"C. Classy Numbers","group":"Codeforces - Educational Codeforces Round 50 (Rated for Div. 2)","url":"https://codeforces.com/problemset/problem/1036/C","interactive":false,"memoryLimit":256,"timeLimit":3000,"tests":[{"id":1617679023134,"input":"4\n1 1000\n1024 1024\n65536 65536\n999999 1000001","output":"1000\n1\n0\n2"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"CClassyNumbers"}},"batch":{"id":"6e23cc66-5cc6-428c-a392-e5df2f16eb3f","size":1},"srcPath":"c:\\Users\\Dhruv\\myGithub\\Competitive-Programming\\Practice\\Digit DP\\C_Classy_Numbers.cpp"}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"name":"Chef and special numbers","group":"CodeChef - Practice(Medium)","url":"https://www.codechef.com/problems/WORKCHEF","interactive":false,"memoryLimit":256,"timeLimit":5000,"tests":[{"input":"3\n48 48 1\n48 48 2\n5 15 1\n","output":"1\n1\n11\n","id":1615483676253}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"ChefAndSpecialNumbers"}},"srcPath":"c:\\Users\\Dhruv\\myGithub\\Competitive-Programming\\Practice\\Digit DP\\Chef_and_special_numbers.cpp"}
1+
{"name":"Chef and special numbers","group":"CodeChef - Practice(Medium)","url":"https://www.codechef.com/problems/WORKCHEF","interactive":false,"memoryLimit":256,"timeLimit":5000,"tests":[{"id":1615483676253,"input":"3\n48 48 1\n48 48 2\n5 15 1","output":"1\n1\n11"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"ChefAndSpecialNumbers"}},"srcPath":"c:\\Users\\Dhruv\\myGithub\\Competitive-Programming\\Practice\\Digit DP\\Chef_and_special_numbers.cpp"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"D. Magic Numbers","group":"Codeforces - Educational Codeforces Round 8","url":"https://codeforces.com/problemset/problem/628/D","interactive":false,"memoryLimit":256,"timeLimit":2000,"tests":[{"input":"2 6\n10\n99\n","output":"8\n","id":1617681416443},{"input":"2 0\n1\n9\n","output":"4\n","id":1617681416383},{"input":"19 7\n1000\n9999\n","output":"6\n","id":1617681416428}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"DMagicNumbers"}},"batch":{"id":"60e03a55-17ca-4a90-be80-67514fca8e62","size":1},"srcPath":"c:\\Users\\Dhruv\\myGithub\\Competitive-Programming\\Practice\\Digit DP\\D_Magic_Numbers.cpp"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"Local: Maximum Product","url":"c:\\Users\\Dhruv\\myGithub\\Competitive-Programming\\Practice\\Digit DP\\Maximum Product.cpp","tests":[{"id":1617679766823,"input":"1 10\n","output":"9\n"},{"id":1617679777541,"input":"51 62\n","output":"59"}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\Users\\Dhruv\\myGithub\\Competitive-Programming\\Practice\\Digit DP\\Maximum Product.cpp","group":"local","local":true}
89.4 KB
Binary file not shown.
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
2+
//------------------------------------------------------------------------------
3+
// #include <iostream>
4+
// #include <vector>
5+
#include <bits/stdc++.h>
6+
// #include <cmath>
7+
// #include <algorithm>
8+
// #include <unordered_map>
9+
// #include <map>
10+
// #include <set>
11+
// #include <unordered_set>
12+
//------------------------------------------------------------------------------
13+
using namespace std;
14+
//------------------------------------------------------------------------------
15+
#define FastIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
16+
#define v(Type) vector<Type>
17+
#define w(T) \
18+
int T; \
19+
cin >> T; \
20+
while (T--)
21+
#define int long long int
22+
#define mod 1000000007ll
23+
#define endl "\n"
24+
//------------------------------------------------------------------------------
25+
// Any fucntion can be called using Math.function_name();
26+
//------------------------------------------------------------------------------
27+
class Math
28+
{
29+
public:
30+
//Returns gcd of two numbers
31+
int gcd(int a, int b)
32+
{
33+
return (a % b == 0) ? b : gcd(b, a % b);
34+
}
35+
36+
//Returns lcm of two numbers
37+
int lcm(int a, int b)
38+
{
39+
return a * (b / gcd(a, b));
40+
}
41+
42+
// Returns flag array isPrime
43+
// isPrime[i] = true (if i is Prime)
44+
// isPrime[i] = false (if i is not Prime)
45+
vector<bool> *seiveOfEratosthenes(const int N)
46+
{
47+
vector<bool> *isPrime = new vector<bool>(N + 1, true);
48+
(*isPrime)[0] = (*isPrime)[1] = false;
49+
for (int i = 2; i * i <= N; ++i)
50+
if ((*isPrime)[i])
51+
for (int j = i * i; j <= N; j += i)
52+
(*isPrime)[j] = false;
53+
54+
return isPrime;
55+
}
56+
57+
//Returns (x ^ n)
58+
int pow(const int &x, int n)
59+
{
60+
if (n == 0)
61+
return 1;
62+
int h = pow(x, n / 2);
63+
return (n & 1) ? h * h * x : h * h;
64+
}
65+
66+
//Returns (x ^ n) % M
67+
int pow(const int &x, int n, const int &M)
68+
{
69+
if (n == 0)
70+
return 1;
71+
int h = pow(x, n / 2) % M;
72+
return (n & 1) ? (h * h * x) % M : (h * h) % M;
73+
}
74+
75+
//Returns all Primes <= N
76+
vector<int> *primesUptoN(const int N)
77+
{
78+
vector<bool> *isPrime = seiveOfEratosthenes(N);
79+
vector<int> *Primes = new vector<int>;
80+
if (2 <= N)
81+
(*Primes).push_back(2);
82+
for (int i = 3; i <= N; i += 2)
83+
if ((*isPrime)[i])
84+
(*Primes).push_back(i);
85+
return Primes;
86+
}
87+
88+
} Math;
89+
//------------------------------------------------------------------------------
90+
int dp[19][4][2];
91+
int solve(string &s, int pos, int cnt, bool t)
92+
{
93+
94+
if (pos == s.length())
95+
return 1;
96+
if (dp[pos][cnt][t] != -1)
97+
return dp[pos][cnt][t];
98+
int ub = (t) ? s[pos] - '0' : 9;
99+
int ans = 0;
100+
for (int i = 0; i <= ub; i++)
101+
{
102+
int cntupd = cnt + (i > 0);
103+
if (cntupd <= 3)
104+
ans += solve(s, pos + 1, cntupd, t & (i == ub));
105+
}
106+
return dp[pos][cnt][t] = ans;
107+
}
108+
109+
void solve()
110+
{
111+
int l, r;
112+
cin >> l >> r;
113+
114+
string L = to_string(l - 1);
115+
string R = to_string(r);
116+
memset(dp, -1, sizeof(dp));
117+
int ansR = solve(R, 0, 0, 1);
118+
memset(dp, -1, sizeof(dp));
119+
int ansL = solve(L, 0, 0, 1);
120+
121+
cout << ansR - ansL << endl;
122+
}
123+
//------------------------------------------------------------------------------
124+
int32_t main()
125+
{
126+
FastIO;
127+
128+
w(T)
129+
solve();
130+
131+
return 0;
132+
}
133+
//------------------------------------------------------------------------------

Practice/Digit DP/D_Magic_Numbers.cpp

Whitespace-only changes.
67.1 KB
Binary file not shown.
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
2+
//------------------------------------------------------------------------------
3+
// #include <iostream>
4+
// #include <vector>
5+
#include <bits/stdc++.h>
6+
// #include <cmath>
7+
// #include <algorithm>
8+
// #include <unordered_map>
9+
// #include <map>
10+
// #include <set>
11+
// #include <unordered_set>
12+
//------------------------------------------------------------------------------
13+
using namespace std;
14+
//------------------------------------------------------------------------------
15+
#define FastIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
16+
#define v(Type) vector<Type>
17+
#define w(T) \
18+
int T; \
19+
cin >> T; \
20+
while (T--)
21+
#define int long long int
22+
#define mod 1000000007ll
23+
#define endl "\n"
24+
//------------------------------------------------------------------------------
25+
// Any fucntion can be called using Math.function_name();
26+
//------------------------------------------------------------------------------
27+
class Math
28+
{
29+
public:
30+
//Returns gcd of two numbers
31+
int gcd(int a, int b)
32+
{
33+
return (a % b == 0) ? b : gcd(b, a % b);
34+
}
35+
36+
//Returns lcm of two numbers
37+
int lcm(int a, int b)
38+
{
39+
return a * (b / gcd(a, b));
40+
}
41+
42+
// Returns flag array isPrime
43+
// isPrime[i] = true (if i is Prime)
44+
// isPrime[i] = false (if i is not Prime)
45+
vector<bool> *seiveOfEratosthenes(const int N)
46+
{
47+
vector<bool> *isPrime = new vector<bool>(N + 1, true);
48+
(*isPrime)[0] = (*isPrime)[1] = false;
49+
for (int i = 2; i * i <= N; ++i)
50+
if ((*isPrime)[i])
51+
for (int j = i * i; j <= N; j += i)
52+
(*isPrime)[j] = false;
53+
54+
return isPrime;
55+
}
56+
57+
//Returns (x ^ n)
58+
int pow(const int &x, int n)
59+
{
60+
if (n == 0)
61+
return 1;
62+
int h = pow(x, n / 2);
63+
return (n & 1) ? h * h * x : h * h;
64+
}
65+
66+
//Returns (x ^ n) % M
67+
int pow(const int &x, int n, const int &M)
68+
{
69+
if (n == 0)
70+
return 1;
71+
int h = pow(x, n / 2) % M;
72+
return (n & 1) ? (h * h * x) % M : (h * h) % M;
73+
}
74+
75+
//Returns all Primes <= N
76+
vector<int> *primesUptoN(const int N)
77+
{
78+
vector<bool> *isPrime = seiveOfEratosthenes(N);
79+
vector<int> *Primes = new vector<int>;
80+
if (2 <= N)
81+
(*Primes).push_back(2);
82+
for (int i = 3; i <= N; i += 2)
83+
if ((*isPrime)[i])
84+
(*Primes).push_back(i);
85+
return Primes;
86+
}
87+
88+
} Math;
89+
//------------------------------------------------------------------------------
90+
pair<int, string> dp[19][2][2][2];
91+
pair<int, string> solve(string &l, string &r, int pos, bool tl, bool tr, bool start)
92+
{
93+
94+
if (pos == l.length())
95+
return {1, ""};
96+
97+
if (dp[pos][tl][tr][start].first != -1)
98+
return dp[pos][tl][tr][start];
99+
int lb = (tl) ? l[pos] - '0' : 0;
100+
int ub = (tr) ? r[pos] - '0' : 9;
101+
102+
pair<int, string> ans = {0, ""};
103+
104+
for (int i = lb; i <= ub; i++)
105+
{
106+
int val = 1;
107+
if (start == false and i == 0)
108+
val = 1;
109+
else
110+
val = i;
111+
112+
auto x = solve(l, r, pos + 1, (tl) & (i == lb), (tr) & (i == ub), start | (i > 0));
113+
if (x.first * val >= ans.first)
114+
{
115+
reverse(x.second.begin(), x.second.end());
116+
char ch = i + '0';
117+
x.second.push_back(ch);
118+
reverse(x.second.begin(), x.second.end());
119+
x.first *= val;
120+
ans = x;
121+
}
122+
}
123+
return dp[pos][tl][tr][start] = ans;
124+
}
125+
void solve()
126+
{
127+
string l, r;
128+
cin >> l >> r;
129+
130+
reverse(l.begin(), l.end());
131+
while (l.size() < r.size())
132+
l.push_back('0');
133+
134+
reverse(l.begin(), l.end());
135+
136+
for (int i = 0; i < 19; i++)
137+
for (int j = 0; j < 2; j++)
138+
for (int k = 0; k < 2; k++)
139+
for (int p = 0; p < 2; p++)
140+
dp[i][j][k][p].first = -1;
141+
142+
auto ans = solve(l, r, 0, 1, 1, 0);
143+
144+
cout << stoll(ans.second) << endl;
145+
}
146+
//------------------------------------------------------------------------------
147+
int32_t main()
148+
{
149+
FastIO;
150+
151+
// w(T)
152+
solve();
153+
154+
return 0;
155+
}
156+
//------------------------------------------------------------------------------

0 commit comments

Comments
 (0)