Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
A number becomes bad exactly when its prime factorization contains a one-digit prime. There are only four one-digit primes, so don't even think about factoring numbers up to .
The one-digit primes are . So a number is good iff it is not divisible by any of .
Now the problem is just: count integers in that avoid divisibility by four fixed primes. Compute a prefix function = good numbers from to , then answer .
Use inclusion-exclusion over the set . Add for products of an even number of chosen primes, subtract it for products of an odd number.
There are only subsets, so each test case is basically free. Counting as good inside is fine because , so it cancels correctly when using prefix differences.
The definition looks like it wants prime factorization, but that is bait. For , factoring every number in a range would be a disaster, and also completely unnecessary.
A number is bad if some prime in its factorization has one digit.
The one-digit primes are only:
So a number is good iff it is not divisible by any of those four primes. That's the entire trick.
For example:
So we need to count numbers in not divisible by , , , or .
Prefix counting
Let be the number of integers from to that are good. Then the answer for a test case is:
Now we only need a fast way to compute .
Inclusion-exclusion
Start with all numbers from to .
Remove numbers divisible by , , , or .
But if we subtract numbers divisible by and numbers divisible by , then numbers divisible by got subtracted twice, so we add them back. Same old inclusion-exclusion stuff.
For every subset of primes :
The empty subset has product and even size, so it contributes .
So:
There are only subsets, so this is tiny. Like, embarrassingly tiny. Codeforces gave us and we bonk it with sixteen divisions.
What about ?
The input always has . Our prefix formula counts as good, because it is not divisible by . That is fine. When answering , this behaves correctly, and for ranges starting at , it subtracts away the .
Complexity
For each test case, we check subsets.
Time complexity: , basically .
Memory complexity: .
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
ll countGoodUpTo(ll n) {
vector<ll> primes = {2, 3, 5, 7};
ll ans = 0;
for (int mask = 0; mask < (1 << 4); mask++) {
ll product = 1;
int bits = 0;
for (int i = 0; i < 4; i++) {
if (mask & (1 << i)) {
product *= primes[i];
bits++;
}
}
if (bits % 2 == 0) ans += n / product;
else ans -= n / product;
}
return ans;
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
ll l, r;
cin >> l >> r;
cout << countGoodUpTo(r) - countGoodUpTo(l - 1) << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
ll countGoodUpTo(ll n) {
vector<ll> primes = {2, 3, 5, 7};
ll ans = 0;
for (int mask = 0; mask < (1 << 4); mask++) {
ll product = 1;
int bits = 0;
for (int i = 0; i < 4; i++) {
if (mask & (1 << i)) {
product *= primes[i];
bits++;
}
}
if (bits % 2 == 0) ans += n / product;
else ans -= n / product;
}
return ans;
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
ll l, r;
cin >> l >> r;
cout << countGoodUpTo(r) - countGoodUpTo(l - 1) << '\n';
}
}