QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#497164#9139. Prefix Divisible by Suffixhos_lyricAC ✓5389ms3952kbC++144.2kb2024-07-28 20:28:202024-07-28 20:28:21

Judging History

你现在查看的是最新测评结果

  • [2024-07-28 20:28:21]
  • 评测
  • 测评结果:AC
  • 用时:5389ms
  • 内存:3952kb
  • [2024-07-28 20:28:20]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


// a x + b y = (+/-) gcd(a, b)
//   (a, 0): g = a, x = 1, y = 0
//   (0, b), (b, b), (-b, b) (b != 0): g = b, x = 0, y = 1
//   otherwise: 2 |x| <= |b|, 2 |y| <= |a|
// S: signed integer
template <class S> S gojo(S a, S b, S &x, S &y) {
  if (b != 0) {
    const S g = gojo(b, a % b, y, x);
    y -= (a / b) * x;
    return g;
  } else {
    x = 1;
    y = 0;
    return a;
  }
}

// x == bs[i]  (mod ms[i])
// S: signed integer
template <class S>
// pair<S, S> modSystem(const vector<S> &ms, const vector<S> &bs) {
pair<S, S> modSystem(S m0, S b0, S m1, S b1) {
  // const int len = ms.size();
  // assert(static_cast<size_t>(len) == bs.size());
  // S m0 = 1, b0 = 0;
  // for (int i = 0; i < len; ++i) {
    // assert(ms[i] >= 1);
    // S m1 = ms[i], b1 = bs[i];
    if ((b1 %= m1) < 0) b1 += m1;
    if (m0 < m1) {
      swap(m0, m1);
      swap(b0, b1);
    }
    // to avoid overflow
    if (m0 % m1 == 0) {
      if (b0 % m1 != b1) return make_pair(0, 0);
      return make_pair(m0, b0);// continue;
    }
    S z0, z1;
    const S g = gojo(m0, m1, z0, z1);
    b1 -= b0;
    if (b1 % g != 0) return make_pair(0, 0);
    (b1 /= g) %= m1;
    m1 /= g;
    b0 += m0 * ((z0 * b1) % m1);
    m0 *= m1;
    if (b0 < 0) b0 += m0;
  // }
  return make_pair(m0, b0);
}


Int TEN[20];
Int N, C;

Int P;
int len;
Int M[20], B[20];

// 1 <= p <= P
// p == b  (mod m)
Int dfs(int i, Int m, Int b) {
  chmin(m, P + 1);
  Int ret = 0;
  if (b <= P) {
    ret += (P - b) / m + 1;
    if (b == 0) --ret;
    for (int j = i + 1; j < len; ++j) {
      // const auto res = modSystem<Int>({m, M[j]}, {b, B[j]});
      const auto res = modSystem<Int>(m, b, M[j], B[j]);
      if (res.first) {
        ret -= dfs(j, res.first, res.second);
      }
    }
  }
  return ret;
}

int main() {
  TEN[0] = 1;
  for (int e = 1; e <= 14; ++e) TEN[e] = TEN[e - 1] * 10;
  
  for (; ~scanf("%lld%lld", &N, &C); ) {
    Int ans = 0;
    for (Int k = 1, ten = 10; k <= 7; ++k, ten *= 10) {
      const Int q = N / ten;
      const Int r = N % ten;
      if (q >= 1) {
        for (Int s = (k == 1) ? 0 : TEN[k - 1]; s < TEN[k]; ++s) {
          P = (r >= s) ? q : (q - 1);
          len = 1;
          M[0] = s + C;
          B[0] = 0;
          for (Int j = k - 1; j >= 1; --j) {
            // a p + b == 0  (mod m)
            Int a = TEN[k - j];
            Int b = s / TEN[j];
            Int m = s % TEN[j] + C;
            Int x, y;
            const Int g = gojo(a, m, x, y);
            if (b % g == 0) {
              M[len] = m/g;
              B[len] = -((b/g) * x % (m/g));
              ++len;
            }
          }
          const Int res = dfs(0, M[0], B[0]);
/*
if(N<1000){
 cerr<<"k = "<<k<<", s = "<<s<<", res = "<<res<<endl;
 cerr<<"  M = ";pv(M,M+len);
 cerr<<"  B = ";pv(B,B+len);
}
*/
          ans += res;
        }
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3888kb

input:

20 1

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3884kb

input:

111 4

output:

9

result:

ok 1 number(s): "9"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3876kb

input:

1111 10

output:

75

result:

ok 1 number(s): "75"

Test #4:

score: 0
Accepted
time: 176ms
memory: 3792kb

input:

1000000 7

output:

111529

result:

ok 1 number(s): "111529"

Test #5:

score: 0
Accepted
time: 0ms
memory: 3884kb

input:

1 1

output:

0

result:

ok 1 number(s): "0"

Test #6:

score: 0
Accepted
time: 1ms
memory: 3948kb

input:

99999 10000

output:

0

result:

ok 1 number(s): "0"

Test #7:

score: 0
Accepted
time: 0ms
memory: 3792kb

input:

1000 1

output:

287

result:

ok 1 number(s): "287"

Test #8:

score: 0
Accepted
time: 2381ms
memory: 3948kb

input:

10000000 1

output:

3102010

result:

ok 1 number(s): "3102010"

Test #9:

score: 0
Accepted
time: 2851ms
memory: 3952kb

input:

100000000 1

output:

31035571

result:

ok 1 number(s): "31035571"

Test #10:

score: 0
Accepted
time: 3139ms
memory: 3808kb

input:

1000000000 1

output:

310375697

result:

ok 1 number(s): "310375697"

Test #11:

score: 0
Accepted
time: 3465ms
memory: 3872kb

input:

10000000000 1

output:

3103907933

result:

ok 1 number(s): "3103907933"

Test #12:

score: 0
Accepted
time: 3729ms
memory: 3884kb

input:

100000000000 1

output:

31039265058

result:

ok 1 number(s): "31039265058"

Test #13:

score: 0
Accepted
time: 3931ms
memory: 3864kb

input:

1000000000000 1

output:

310394177863

result:

ok 1 number(s): "310394177863"

Test #14:

score: 0
Accepted
time: 3998ms
memory: 3756kb

input:

10000000000000 1

output:

3103943538348

result:

ok 1 number(s): "3103943538348"

Test #15:

score: 0
Accepted
time: 4177ms
memory: 3792kb

input:

100000000000000 1

output:

31039449626535

result:

ok 1 number(s): "31039449626535"

Test #16:

score: 0
Accepted
time: 4364ms
memory: 3752kb

input:

100000000000000 10

output:

9041696367054

result:

ok 1 number(s): "9041696367054"

Test #17:

score: 0
Accepted
time: 4573ms
memory: 3812kb

input:

100000000000000 100

output:

1744469671929

result:

ok 1 number(s): "1744469671929"

Test #18:

score: 0
Accepted
time: 4846ms
memory: 3872kb

input:

100000000000000 1000

output:

263959224215

result:

ok 1 number(s): "263959224215"

Test #19:

score: 0
Accepted
time: 5234ms
memory: 3952kb

input:

100000000000000 10000

output:

35400402243

result:

ok 1 number(s): "35400402243"

Test #20:

score: 0
Accepted
time: 4708ms
memory: 3884kb

input:

100000000000000 239

output:

870346971377

result:

ok 1 number(s): "870346971377"

Test #21:

score: 0
Accepted
time: 4574ms
memory: 3872kb

input:

99999987654321 111

output:

1606282527743

result:

ok 1 number(s): "1606282527743"

Test #22:

score: 0
Accepted
time: 5305ms
memory: 3948kb

input:

96709210826727 9568

output:

35605797003

result:

ok 1 number(s): "35605797003"

Test #23:

score: 0
Accepted
time: 5204ms
memory: 3876kb

input:

22952388910151 8412

output:

9470863043

result:

ok 1 number(s): "9470863043"

Test #24:

score: 0
Accepted
time: 5121ms
memory: 3884kb

input:

49191272026279 3065

output:

49381052989

result:

ok 1 number(s): "49381052989"

Test #25:

score: 0
Accepted
time: 5389ms
memory: 3760kb

input:

75434450109703 9205

output:

28741533023

result:

ok 1 number(s): "28741533023"

Test #26:

score: 0
Accepted
time: 4697ms
memory: 3944kb

input:

1677628193127 753

output:

5631822336

result:

ok 1 number(s): "5631822336"

Test #27:

score: 0
Accepted
time: 3608ms
memory: 3864kb

input:

1000010000 10000

output:

328824

result:

ok 1 number(s): "328824"

Extra Test:

score: 0
Extra Test Passed