QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#311151#1175. Bags of CandiesbaecoAC ✓614ms108600kbC++141.9kb2024-01-21 23:22:162024-01-21 23:22:16

Judging History

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

  • [2024-01-21 23:22:16]
  • 评测
  • 测评结果:AC
  • 用时:614ms
  • 内存:108600kb
  • [2024-01-21 23:22:16]
  • 提交

answer

#include<bits/stdc++.h>
#define MAX 8000001
#define MAX2 1000001
using namespace std;
typedef long long ll;

vector<int> sieve(MAX);
bool sieveRaw[MAX];
ll A[MAX], B[MAX];
ll N, C, y;
int num[MAX], pri[MAX];

void update(int idx, int value){
	while(idx <= y){
		sieve[idx] = sieve[idx] + value;
		idx = idx + (idx & -idx);
	}
}

int sum(int idx){
	int ret = 0;
	while(idx > 0){
		ret = ret + sieve[idx];
		idx = idx - (idx & -idx);
	}
	return ret;
}

void make_fenwick_tree(){
	for(int i = 2; i <= y; i++){
        sieve[i] += 1;
        if(i + (i & -i) <= y) sieve[i + (i & -i)] += sieve[i];
    }
}

ll s0(ll v){
	if(v <= y) return sum(v);
	return B[N / v]; 
}

void calc_primes(){
	ll i, j;
	for(ll p = 2;p <= C;p++){
		if(sieveRaw[p]) continue;
		ll sp = sum(p - 1);
		ll lim = min(N / y, N / (p * p));
		B[1] -= s0(N / p) - sp;
		for(i = p;i <= lim;i++){
			if(sieveRaw[i]) continue;
			B[i] -= s0(N / (i * p)) - sp;
		}
		j = p * p;
		while(j <= y){
			if(sieveRaw[j]){
				j += p;
				continue;
			}
			sieveRaw[j] = true;
			update(j, -1);
			j += p;
		}
	}
}

void init(){
	ll i;
	for(i = 0;i < MAX;i++) sieve[i] = 0;
	make_fenwick_tree();
	sieveRaw[1] = true;
	for(i = 2;i <= y;i++) sieveRaw[i] = false;
	for(i = 1;i <= C;i++){
		A[i] = i - 1;
		B[i] = N / i - 1;
	}
}

void inn(){
	ll i, j;
	num[1] = 1;
	for(i = 2;i * i < MAX;i++){
		if(num[i]) continue;
		for(j = i * i;j < MAX;j+= i){
			num[j] = 1;
		}
	}
	for(i = 1;i < MAX;i++) pri[i] = pri[i - 1] + !num[i];
}

ll pi(ll n){
	N = n;
	if(N <= 1e6) return pri[N];
	C = sqrt(N);
	y = round(0.7*pow(N, 2.0/3.0) / pow(log(N), 2.0/3.0));
	y = min(y, (ll)4e9);
	y = max(C+1, y);
	init();
	calc_primes();
	return B[1];
}

int main(){
	int T;
	ll n;
	inn();
	scanf("%d", &T);
	while(T--){
		scanf("%lld", &n);
		ll ans = pi(n) - pi(n / 2);
		printf("%lld\n", ans + 1 + (n - ans) / 2);
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 35ms
memory: 98628kb

input:

2
4
9

output:

3
6

result:

ok 2 number(s): "3 6"

Test #2:

score: 0
Accepted
time: 43ms
memory: 96920kb

input:

5
2
3
4
5
6

output:

2
3
3
4
4

result:

ok 5 number(s): "2 3 3 4 4"

Test #3:

score: 0
Accepted
time: 55ms
memory: 96888kb

input:

5
1111
2018
3333
4006
5555

output:

599
1078
1772
2128
2942

result:

ok 5 number(s): "599 1078 1772 2128 2942"

Test #4:

score: 0
Accepted
time: 62ms
memory: 101764kb

input:

5
26666666
10000000
23456789
27777777
24444442

output:

13730373
5158034
12080298
14301448
12588059

result:

ok 5 number(s): "13730373 5158034 12080298 14301448 12588059"

Test #5:

score: 0
Accepted
time: 614ms
memory: 108600kb

input:

5
47890123456
12345678901
96666666669
85555555558
100000000000

output:

24438086351
6307451722
49300536501
43638011231
50999200118

result:

ok 5 number(s): "24438086351 6307451722 49300536501 43638011231 50999200118"