QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#494265 | #9139. Prefix Divisible by Suffix | ucup-team159# | TL | 7337ms | 3832kb | C++23 | 6.8kb | 2024-07-27 14:57:28 | 2024-07-27 14:57:29 |
Judging History
answer
#line 1 "J.cpp"
// #pragma GCC target("avx2,avx512f,avx512vl,avx512bw,avx512dq,avx512cd,avx512vbmi,avx512vbmi2,avx512vpopcntdq,avx512bitalg,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("Ofast")
#line 2 "/home/sigma/comp/library/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb push_back
#define eb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> bool chmax(T& x, U y){
if(x<y){ x=y; return true; }
return false;
}
template<class T,class U> bool chmin(T& x, U y){
if(y<x){ x=y; return true; }
return false;
}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class T> int lwb(const V<T>& v, const T& a){return lower_bound(all(v),a) - v.begin();}
template<class T>
V<T> Vec(size_t a) {
return V<T>(a);
}
template<class T, class... Ts>
auto Vec(size_t a, Ts... ts) {
return V<decltype(Vec<T>(ts...))>(a, Vec<T>(ts...));
}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
o<<"{";
for(const T& v:vc) o<<v<<",";
o<<"}";
return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }
#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ~ ";
dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {"; \
for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif
template<class D> D divFloor(D a, D b){
return a / b - (((a ^ b) < 0 && a % b != 0) ? 1 : 0);
}
template<class D> D divCeil(D a, D b) {
return a / b + (((a ^ b) > 0 && a % b != 0) ? 1 : 0);
}
/*
x 0 1 2 3 4 5 6 7 8 9
bsr(x) -1 0 1 1 2 2 2 2 3 3
最上位bit
*/
int bsr(int x){
return x == 0 ? -1 : 31 ^ __builtin_clz(x);
}
int bsr(uint x){
return x == 0 ? -1 : 31 ^ __builtin_clz(x);
}
int bsr(ll x){
return x == 0 ? -1 : 63 ^ __builtin_clzll(x);
}
int bsr(ull x){
return x == 0 ? -1 : 63 ^ __builtin_clzll(x);
}
/*
x 0 1 2 3 4 5 6 7 8 9
bsl(x) -1 0 1 0 2 0 1 0 3 0
最下位bit
*/
int bsl(int x){
if(x==0) return -1;
return __builtin_ctz(x);
}
int bsl(uint x){
if(x==0) return -1;
return __builtin_ctz(x);
}
int bsl(ll x){
if(x==0) return -1;
return __builtin_ctzll(x);
}
int bsl(ull x){
if(x==0) return -1;
return __builtin_ctzll(x);
}
#line 1 "/home/sigma/comp/library/misc/int128.cpp"
using Int = __int128;
istream& operator>>(istream& i, Int& x){
x=0;
string s;
i>>s;
int N=s.size(),it=0;
if(s[0]=='-') it++;
for(;it<N;it++) x=(x*10+s[it]-'0');
if(s[0]=='-') x=-x;
return i;
}
ostream& operator<<(ostream& o, const Int& x){
Int tmp=x;
if(tmp==0) return o<<0;
if(tmp<0) o<<'-',tmp=-tmp;
vector<int> ds;
while(tmp) ds.pb(tmp%10),tmp/=10;
reverse(all(ds));
for(int d:ds) o<<d;
return o;
}
#line 7 "J.cpp"
struct EG { Int g, x, y; };
EG extGcdSub(Int a, Int b) {
if(b == 0){
if (a >= 0) return EG{a, 1, 0};
else return EG{-a, -1, 0};
}else{
auto e = extGcdSub(b, a % b);
return EG{e.g, e.y, e.x - a / b * e.y};
}
}
EG extGcd(Int a,Int b){
auto e = extGcdSub(a,b);
if(e.x < 0){
if(b > 0){
e.x += b/e.g;
e.y -= a/e.g;
}else{
e.x -= b/e.g;
e.y += a/e.g;
}
}
return e;
}
pair<Int,Int> crt(Int r2, Int m2, Int r1, Int m1){
auto e = extGcd(m1,m2);
Int g = e.g;
if((r2-r1)%g) return {0,-1};
Int q = (r2-r1)/g * e.x % (m2/g);
Int r = r1 + m1 * q;
Int m = m1 * (m2/g);
return {(r%m+m)%m,m};
}
pair<Int,Int> get_next(Int r, Int m){
if(r < 0) r = (r%m+m)%m;
r %= m;
// 10p = r mod m => p = ? mod ?
Int a = 10;
if(m%2 == 0){
if(r%2) return {0,-1};
a /= 2, r /= 2, m /= 2;
}
if(m%5 == 0){
if(r%5) return {0,-1};
a /= 5, r /= 5, m /= 5;
}
while(true){
if(r%a == 0) return {r/a,m};
r += m;
}
}
const int H = 6;
Int solve(Int X, Int c){
V<Int> ten(15); rep(i,15) ten[i] = TEN(i);
auto is_good = [&](Int n){
int d = to_string(ll(n)).length();
rep1(low,d-1){
Int p = n / ten[low], s = n % ten[low];
if(p%(s+c) == 0) return true;
}
return false;
};
/*
下 sLen 桁が s, p = r mod m
*/
Int ans = 0;
ll cnt = 0;
auto dfs = [&](auto&& self, Int s, int sLen, Int r, Int m, int sgn) -> void {
cnt++;
if(r * ten[sLen] + s > X){
// too big
return;
}
if(sLen == H){
Int pmax = (X-s)/ten[sLen];
// 0 < r, r+m, .. <= pmax
Int num = (pmax-r)/m + 1; if(r == 0) num--;
if(num){
show("-------------");
show(sLen); show(s); show(r); show(m); show(sgn);
show(num);
}
ans += num * sgn;
return;
}
rep(d,10){
auto [r1, m1] = get_next(r-d, m);
// rep(_,4){
// auto x = get_next(r-d,m);
// r1 = x.fs, m1 = x.sc;
// }
if(m1 == -1) continue;
Int ns = s + ten[sLen] * d;
// don't add condition
self(self,ns,sLen+1,r1,m1,sgn);
Int r2 = 0, m2 = ns + c; // TODO: r2 = 0
auto [rr,mm] = crt(r1,m1,r2,m2);
rep(_,4){
auto x = crt(r1,m1,r2,m2);
rr = x.fs, mm = x.sc;
}
if(mm == -1) continue;
self(self,ns,sLen+1,rr,mm,-sgn);
}
};
dfs(dfs,0,0,0,1,1);
// brute for ans < ten[H]
for(Int n=1;n<ten[H];n++){
if(n > X) break;
if(!is_good(n)) ans++;
}
cerr << "dfs call: " << cnt << endl;
return X - ans;
}
Int brute(Int X, Int c){
V<Int> ten(15); rep(i,15) ten[i] = TEN(i);
auto is_good = [&](Int n){
int d = to_string(ll(n)).length();
rep1(low,d-1){
Int p = n / ten[low], s = n % ten[low];
if(p%(s+c) == 0) return true;
}
return false;
};
Int res = 0;
rep1(n,X) if(is_good(n)) res++;
return res;
}
void TEST(){
Int c; cin >> c;
rep1(X,TEN(H+H)){
show(X);
Int res = solve(X,c);
Int god = brute(X,c);
if(res != god){
cout <<"X,c: " << X << " " << c << endl;
cout << res << endl;
cout << god << endl;
assert(false);
}
}
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false); //DON'T USE scanf/printf/puts !!
cout << fixed << setprecision(20);
ll X,c; cin >> X >> c;
cout << solve(X,c) << endl;
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 3832kb
input:
20 1
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 11ms
memory: 3616kb
input:
111 4
output:
9
result:
ok 1 number(s): "9"
Test #3:
score: 0
Accepted
time: 57ms
memory: 3620kb
input:
1111 10
output:
75
result:
ok 1 number(s): "75"
Test #4:
score: 0
Accepted
time: 1309ms
memory: 3820kb
input:
1000000 7
output:
111529
result:
ok 1 number(s): "111529"
Test #5:
score: 0
Accepted
time: 1ms
memory: 3620kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #6:
score: 0
Accepted
time: 842ms
memory: 3596kb
input:
99999 10000
output:
0
result:
ok 1 number(s): "0"
Test #7:
score: 0
Accepted
time: 67ms
memory: 3828kb
input:
1000 1
output:
287
result:
ok 1 number(s): "287"
Test #8:
score: 0
Accepted
time: 2011ms
memory: 3772kb
input:
10000000 1
output:
3102010
result:
ok 1 number(s): "3102010"
Test #9:
score: 0
Accepted
time: 2704ms
memory: 3828kb
input:
100000000 1
output:
31035571
result:
ok 1 number(s): "31035571"
Test #10:
score: 0
Accepted
time: 3571ms
memory: 3524kb
input:
1000000000 1
output:
310375697
result:
ok 1 number(s): "310375697"
Test #11:
score: 0
Accepted
time: 4563ms
memory: 3636kb
input:
10000000000 1
output:
3103907933
result:
ok 1 number(s): "3103907933"
Test #12:
score: 0
Accepted
time: 5933ms
memory: 3604kb
input:
100000000000 1
output:
31039265058
result:
ok 1 number(s): "31039265058"
Test #13:
score: 0
Accepted
time: 7337ms
memory: 3492kb
input:
1000000000000 1
output:
310394177863
result:
ok 1 number(s): "310394177863"
Test #14:
score: -100
Time Limit Exceeded
input:
10000000000000 1