QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#311150 | #1175. Bags of Candies | baeco | WA | 94ms | 99032kb | C++14 | 1.9kb | 2024-01-21 23:21:38 | 2024-01-21 23:21:39 |
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){
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) + 1;
printf("%lld\n", ans + (n - ans) / 2);
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 94ms
memory: 97868kb
input:
2 4 9
output:
3 6
result:
ok 2 number(s): "3 6"
Test #2:
score: 0
Accepted
time: 94ms
memory: 99032kb
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: -100
Wrong Answer
time: 86ms
memory: 97224kb
input:
5 1111 2018 3333 4006 5555
output:
598 1078 1771 2127 2942
result:
wrong answer 1st numbers differ - expected: '599', found: '598'