QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#507975 | #8337. Counter Reset Problem | ucup-team2307 | TL | 14ms | 17332kb | C++17 | 9.0kb | 2024-08-07 01:30:06 | 2024-08-07 01:30:07 |
Judging History
answer
#pragma GCC optimize("trapv")
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define all(x) begin(x), end(x)
using uint = uint32_t;
const int mod = 1000000009;
namespace math {
const int L = 1<<20, A = 1<<20, B = 32, G = 3;
#define INLINE inline __attribute__ (( always_inline ))
struct mint {
uint32_t v;
template<class T = int>
mint(T x = 0) {
x %= mod;
if(x < 0) x += mod;
v = x;
}
mint operator-() const {
return mint(v ? mod-v : 0);
}
mint &operator*=(const mint &r) {
v = v*1ll*r.v%mod;
return *this;
}
mint &operator+=(const mint &r) {
v = v+r.v>=mod ? (v+r.v-mod) : (v+r.v);
return *this;
}
mint &operator-=(const mint &r) {
return *this += -r;
}
mint &operator/=(const mint &r) {
return *this *= r.inv();
}
friend mint operator+(mint a, const mint &b) {
return a += b;
}
friend mint operator-(mint a, const mint &b) {
return a -= b;
}
friend mint operator*(mint a, const mint &b) {
return a *= b;
}
friend mint operator/(mint a, const mint &b) {
return a /= b;
}
template<class T = int>
mint pow(T p) const {
mint res = 1, cur = *this;
while(p) {
if(p&1) res = res*cur;
cur = cur*cur, p>>=1;
}
return res;
}
mint inv() const {
return mint(*this).pow(mod-2);
}
friend bool operator==(const mint &a, const mint &b) {
return a.v == b.v;
}
friend bool operator!=(const mint &a, const mint &b) {
return !(a == b);
}
friend istream& operator>>(istream &is, mint &m) {
ll v;
is >> v;
m = mint(v);
return is;
}
friend ostream& operator<<(ostream &os, const mint &m) {
os << m.v;
return os;
}
};
mint fact[A], inum[A], ifact[A];
void calc_inum() {
inum[1] = 1;
for(int i = 2; i < A; i++) inum[i] = -inum[mod%i]*(mod/i);
}
void calc_combi() {
if(0 == inum[1]) calc_inum();
fact[0] = ifact[0] = 1;
for(int i = 1; i < A; i++) fact[i] = fact[i-1]*i;
for(int i = 1; i < A; i++) ifact[i] = ifact[i-1]*inum[i];
}
mint nck(int n, int k) {
if(0 == fact[0]) calc_combi();
if(k > n || k < 0) return 0;
return fact[n]*ifact[k]*ifact[n-k];
}
uint W[L], WI[L];
INLINE void calc_roots() {
W[0] = 1;
int g = mint(G).pow((mod-1)/L).v;
int i;
for(i = 1; i < L/2; i++) W[i] = W[i-1]*1ull*g%mod;
for(int x = 2; x < L; x*=2) {
for(int y = 0; y < L/2; y+=x)
W[i++] = W[y];
}
for(i = 0; i < L; i++) {
WI[i] = (uint64_t(W[i])<<B)/mod;
}
}
INLINE void _ntt(int N, vector<mint> &a) {//normal poly -> bit-rev dft | internally [0; 2*mod)
int pos = 0;
for(int l = L/2; l != N/2; l/=2)
pos += l;
for(int l = N/2; l; l/=2) {
for(int x = 0; x < N; x+=2*l) {
for(int j = pos, i = 0; i < l; i++, j++) {
uint u = a[x+i].v+a[x+l+i].v;
if(u >= 2*mod) u -= 2*mod;
uint v = a[x+i].v-a[x+l+i].v+2*mod;
uint Q = (v*1ull*WI[j])>>B;
v = v*W[j] - Q*mod;
a[x+i].v = u, a[x+l+i].v = v;
}
}
pos += l;
}
for(int i = 0; i < N; i++) if(a[i].v >= mod) a[i].v -= mod;
}
INLINE void _ntt_inv(int N, vector<mint> &a) {//bit-rev dft -> normal poly | internally [0; 4*mod)
int pos = L-2;
for(int l = 1; l < N; l*=2) {
for(int x = 0; x < N; x += 2*l) {
for(int j = pos, i = 0; i < l; i++, j++) {
uint u = a[x+i].v, v = a[x+l+i].v;
uint Q = (WI[j]*1ull*v)>>B;
if(u >= 2*mod) u -= 2*mod;
v = v*W[j] - Q*mod;
a[x+i].v = u+v, a[x+l+i].v = u-v+2*mod;
}
}
pos -= 2*l;
}
reverse(1 + all(a));
mint x = mint((mod+1)/2).pow(__lg(N));
for(auto &i : a) i *= x;//takes care of 2*mod
}
template<bool inv>
INLINE void ntt(int n, vector<mint> &a) {//don't forget about reverse order
if(W[0] == 0) calc_roots();
if(!inv) _ntt(n, a);
else _ntt_inv(n, a);
}
struct poly : vector<mint> {
template<class... Args>
explicit poly(Args... args) : vector<mint>(args...) {}
poly(initializer_list<mint> il) : vector<mint>(il.begin(), il.end()) {}
poly &trim(int k) {// mod x^k
if(size() > k) {
erase(begin()+k, end());
shrink_to_fit();
}
return *this;
}
poly &trim() {//remove heading zeroes
int k = 0;
while(k < size() && (*this)[size()-k-1] == 0) k++;
trim(size()-k);
return *this;
}
poly low(int k) const {//get first k coefficients
k = min(k, (int)size());
poly res;
for(int i = 0; i < k; i++) res.push_back((*this)[i]);
return res;
}
friend poly operator*(poly a, const poly &b) {
poly res = b;
int n = a.size()+b.size()-1;
while(n&(n-1)) n += n&-n;
a.resize(n);
res.resize(n);
ntt<0>(n, a);
ntt<0>(n, res);
for(int i = 0; i < n; i++) res[i] *= a[i];
ntt<1>(n, res);
res.trim();//remove ?
return res;
}
poly &operator*=(const poly &b) {
return (*this) = (*this)*b;
}
template<class T>
poly &operator*=(const T &x) {
for(auto &i : *this) i *= x;
return *this;
}
poly &operator+=(const poly &x) {
if(size() < x.size()) resize(x.size());
for(int i = 0; i < min(size(), x.size()); i++) (*this)[i] += x[i];
return *this;
}
poly &operator-=(const poly &x) {
if(size() < x.size()) resize(x.size());
for(int i = 0; i < min(size(), x.size()); i++) (*this)[i] -= x[i];
return *this;
}
template<class T> friend poly operator*(poly p, const T &x) { return p *= x; }
template<class T> friend poly operator*(const T &x, poly p) { return p *= x; }
friend poly operator+(poly p, const poly &x) { return p += x; }
friend poly operator-(poly p, const poly &x) { return p -= x; }
poly inv(int N) {//first n coefficients of P^-1
assert((*this)[0].v);
poly R {(*this)[0].inv()};
R.reserve(2*N);
for(int len = 2; len/2 < N; len*=2) {//R' = R(2 - RT)
poly T = low(len);
T.resize(2*len);ntt<0>(2*len, T);
R.resize(2*len);ntt<0>(2*len, R);
for(int i = 0; i < 2*len; i++) R[i] = R[i]*(2 - R[i]*T[i]);
ntt<1>(2*len, R);
R.trim(len);
}
return R.trim(N);
}
poly &derive() {
for(int i = 0; i+1 < size(); i++) {
(*this)[i] = (*this)[i+1]*(i+1);
}
pop_back();
return *this;
}
poly derivative() { return poly(*this).derive(); }
poly &integrate() {
if(0 == inum[1]) calc_inum();
push_back(0);
for(int i = size(); i-- > 1;) {
(*this)[i] = (*this)[i-1] * inum[i];
}
(*this)[0] = 0;
return *this;
}
poly integral() { return poly(*this).integrate(); }
poly ln(int N) {//first n coefficients of ln(P) = P'/P
return (low(N+1).derivative() * inv(N)).trim(N-1).integrate().trim(N);
}
poly exp(int N) {//first n coefficients of exp(P), quite slow
poly R{1};
for(int len = 2; len/2 < N; len*=2) {
R = (R*(poly{1}+low(len)-R.ln(len))).trim(len);
}
return R.trim(N);
}
};
}
using namespace math;
const int D = 9;
int getbit(int msk, int b) { return (msk >> b) & 1; }
int update_lds(int msk, int d) {
// d = D - 1 - d;
int x = d;
while(x < 10 && !getbit(msk, x)) x++;
if(x < 10) msk -= 1<<x;
return msk + (1<<d);
}
mint count(string N, bool include) {
reverse(all(N));
vector dp(1<<D, vector<mint>(2, 0));
dp[0][include] = 1;
for(int dig = 0; dig < N.size(); dig++) {
vector ndp(1<<D, vector<mint>(2, 0));
for(int msk = 0; msk < 1<<D; msk++) {
for(int d = 0; d < 10; d++) {
int nmsk = d ? update_lds(msk, d - 1) : msk;
for(int flag = 0; flag < 2; flag++) {
int N_dig = N[dig] - '0';
int nflag = d == N_dig ? flag : d < N_dig;
ndp[nmsk][nflag] += dp[msk][flag];
}
}
}
dp = ndp;
// for(int msk = 0; msk < 1<<D; msk++) {
// if(dp[msk][1].v) cout << msk << " " << dp[msk][1] << endl;
// }
// cout << "---" << endl;
}
mint ans = 0;
for(int msk = 1; msk < 1<<D; msk++) {
ans +=(__builtin_popcount(msk) - 1) * dp[msk][1];
// if(dp[msk][1].v) cout << msk << " " << dp[msk][1] << endl;
}
return ans;
}
int n;
string s;
int easy_score()
{
mint ans = 0;
for(auto i : s) ans = (10*ans + i - '0'); ans += 1;
ans *= 10 - (s[0] - '0');
for(int d = 0; d < s[0] - '0'; d++) {
ans += (d+1) * (mint(10).pow(s.size() - 1));
}
return ans.v;
}
int hard_score()
{
return count(s, 1).v;
}
int score()
{
int A = easy_score();
int B = hard_score();
// cout<<s<<" "<<A<<" "<<B<<"\n";
return (A+B*10ll)%mod;
}
signed main()
{
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
// cout << count("040139021316", 1) << endl;
// cout << count("10", 1) << endl;
// return 0;
cin>>n;
// n = 5000;
string l, r;
cin>>l>>r;
// l = r= string(5000, '0');
// r[0] = '1';
s = r;
int A = score();
int B = 0;
if (l != string(n, '0'))
{
int pos = n-1;
while (true)
{
if (l[pos] == '0')
l[pos--] = '9';
else
{
l[pos] = l[pos]-1;
break;
}
}
}
s = l;
B = score();
cout<<mint(A-B)<<"\n";
}
详细
Test #1:
score: 100
Accepted
time: 14ms
memory: 16444kb
input:
2 19 23
output:
51
result:
ok 1 number(s): "51"
Test #2:
score: 0
Accepted
time: 10ms
memory: 16564kb
input:
6 100084 518118
output:
9159739
result:
ok 1 number(s): "9159739"
Test #3:
score: 0
Accepted
time: 12ms
memory: 17276kb
input:
12 040139021316 234700825190
output:
771011551
result:
ok 1 number(s): "771011551"
Test #4:
score: 0
Accepted
time: 13ms
memory: 17332kb
input:
1 5 6
output:
9
result:
ok 1 number(s): "9"
Test #5:
score: 0
Accepted
time: 13ms
memory: 16240kb
input:
2 06 72
output:
609
result:
ok 1 number(s): "609"
Test #6:
score: 0
Accepted
time: 14ms
memory: 16932kb
input:
3 418 639
output:
2912
result:
ok 1 number(s): "2912"
Test #7:
score: -100
Time Limit Exceeded
input:
5000 0517031462295902016787205636287842713710486158285091634061538907131690102542613263904109051429895599547551249682345434244517372300211330243052548402051817254239088411128320032011447373157210750522722463984933692575118884942425236057310901139962840332684448050855646476051878413350560455871387882...
output:
107583434