QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#133084#5587. Distinct Parity ExcessjeznguyenAC ✓593ms193776kbC++203.4kb2023-08-01 14:54:382023-08-01 14:54:39

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-01 14:54:39]
  • 评测
  • 测评结果:AC
  • 用时:593ms
  • 内存:193776kb
  • [2023-08-01 14:54:38]
  • 提交

answer

#include <assert.h>
#include <limits.h>
#include <math.h>

#include <algorithm>
#include <bit>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define ll long long
#define ld long double
#define vll vector<ll>
#define vvll vector<vll>
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvi>
#define vs vector<string>
#define pi pair<int, int>
#define vpi vector<pi>
#define vvpi vector<vpi>
#define pll pair<ll, ll>
#define ppll pair<pll, pll>
#define vpll vector<pll>
#define vvpll vector<vpll>
#define PB push_back
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
double EPS = 1e-18;
// const vpi MOVES_ADJACENT{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
// const vpi MOVES_ALL{{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0,
// -1}, {1, -1}};

template <typename T>
void db(T const& coll) {
    typename T::const_iterator pos;
    typename T::const_iterator end(coll.end());

    for (pos = coll.begin(); pos != end; ++pos) {
        cout << *pos << ' ';
    }
    cout << "\n";
}

void ans(ll x) { cout << (x == 0 ? "YES" : "NO") << "\n"; }

ll MAXN = 1e7;
// ll MAXN = 1e5;
// ll MAXN = 50;
vector<bool> is_prime(MAXN + 5, true);
vll primes;
vll dcount(MAXN + 5, 0);
void init() {
    is_prime[0] = is_prime[1] = false;
    for (ll i = 2; i * i <= MAXN; i++) {
        if (is_prime[i]) {
            for (ll j = i * i; j <= MAXN; j += i) is_prime[j] = false;
        }
    }
    dcount[1] = 1;
    deque<ll> q;
    for (ll i = 1; i <= MAXN; ++i) {
        if (is_prime[i]) primes.PB(i);
    }
    vll index(MAXN + 5, -1);
    for (ll i = 0; i < primes.size(); ++i) {
        ll p = primes[i];
        dcount[p] = 1;
        index[p] = i; // index of the prime, to reference next
        q.PB(p);
    }
    // db(q);
    ll count = q.size();
    while (!q.empty()) {
        ll x = q.front(); q.pop_front();
        // cout << "AT " << x << "\n";
        for (ll next_index = index[x];
             next_index < primes.size() && x * primes[next_index] <= MAXN;
             ++next_index) {
            // cout << " MUL WITH " << primes[next_index] << "\n";
            ll v = x * primes[next_index];
            if (index[x] == next_index) {
                dcount[v] = dcount[x];
            } else {
                dcount[v] = dcount[x] + 1;
            }
            index[v] = next_index;
            q.PB(v);
        }
    }
    // cout << count << "\n";
    // for (ll i = 0; i <= MAXN; ++i) cout << i << " ";
    // cout << "\n";
    // db(dcount);
    // db(primes);
    // db(dcount);
    // db(index);
}
void solve() {
    vll pref(MAXN+5, 0);
    for (ll i = 1; i <= MAXN; ++i) {
        pref[i] += pref[i-1] + (dcount[i]%2==0);
    }
    ll n;
    cin >> n;
    ll l, r;
    for (ll i = 0; i < n; ++i) {
        cin >> l >> r;
        ll total = r - l + 1;
        ll evens = pref[r] - pref[l-1];
        cout << evens - (total-evens) << "\n";
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    init();
    // ll t;
    // cin >> t;
    // while (t--) {
    //     solve();
    // }

    solve();
}

详细

Test #1:

score: 100
Accepted
time: 562ms
memory: 192856kb

input:

3
2 2
2 5
2 10

output:

-1
-4
-5

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 573ms
memory: 193064kb

input:

8
2 100
2 50
50 100
2 1000
100 143
2 1000000
80000 90000
1000000 1000000

output:

13
-1
15
63
0
-1909
-31
1

result:

ok 8 lines

Test #3:

score: 0
Accepted
time: 566ms
memory: 192652kb

input:

10
382245 819896
541000 940657
146832 465833
603509 801534
298623 379575
375605 402241
131337 581395
92899 271294
718706 825535
714017 861484

output:

-784
-608
-518
-88
-253
-41
-1067
-646
-386
-44

result:

ok 10 lines

Test #4:

score: 0
Accepted
time: 574ms
memory: 192840kb

input:

10
5029986 7710504
2780724 8715606
1124900 1304897
1391064 2877235
6811377 8645635
3609999 9999334
3611675 6616197
3599993 6888463
4602820 8926934
948628 1015372

output:

741
-383
-186
-416
-603
894
277
-153
-305
321

result:

ok 10 lines

Test #5:

score: 0
Accepted
time: 560ms
memory: 192184kb

input:

10
2038711 3100771
5100982 8330782
4186549 6595205
2745178 8582462
2990225 5249134
714917 4907291
2265159 8632136
1653054 5378313
3011697 7955156
7552410 9776486

output:

381
963
487
203
-382
-1069
-12
-836
-162
619

result:

ok 10 lines

Test #6:

score: 0
Accepted
time: 578ms
memory: 192284kb

input:

10
2825912 6331426
3371283 8994476
1852689 9530995
503251 2875846
1484762 4452738
2718118 7925568
8246970 9515925
551401 9338634
1219402 1788177
6627126 6835439

output:

181
382
-127
-1282
-147
307
-14
-1250
388
56

result:

ok 10 lines

Test #7:

score: 0
Accepted
time: 593ms
memory: 192080kb

input:

10
4511877 8376994
232714 4628307
5313897 6635021
1350661 6178784
3662577 7241422
1825460 9682385
1108136 5021151
728661 5585243
2193613 4398189
3150983 4600167

output:

312
-1500
1017
-274
-680
-254
-1332
-863
199
-149

result:

ok 10 lines

Test #8:

score: 0
Accepted
time: 555ms
memory: 193776kb

input:

3
2 10000000
2 1000000
2 100000

output:

-1651
-1909
-721

result:

ok 3 lines