QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#311147 | #1175. Bags of Candies | baeco | WA | 150ms | 102272kb | C++14 | 1.9kb | 2024-01-21 23:14:26 | 2024-01-21 23:14:26 |
Judging History
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){
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;
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: 133ms
memory: 97248kb
input:
2 4 9
output:
3 6
result:
ok 2 number(s): "3 6"
Test #2:
score: 0
Accepted
time: 130ms
memory: 98676kb
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: 122ms
memory: 97636kb
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: -100
Wrong Answer
time: 150ms
memory: 102272kb
input:
5 26666666 10000000 23456789 27777777 24444442
output:
13315663 4992125 11712158 13870561 12205234
result:
wrong answer 1st numbers differ - expected: '13730373', found: '13315663'