QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#65727 | #1175. Bags of Candies | eyiigjkn | AC ✓ | 969ms | 16156kb | C++14 | 1.0kb | 2022-12-03 13:08:53 | 2022-12-03 13:08:55 |
Judging History
answer
# include <bits/stdc++.h>
using namespace std;
using ll=long long;
constexpr double eps=1e-8;
ll solve(ll n)
{
int sqn=sqrtl(n),m=0,tot=0;
static int prime[400010];
static ll blo[700010],f[700010];
static bool npri[400010];
npri[1]=1;
for(int i=2;i<=sqn;i++)
{
if(!npri[i]) prime[++m]=i;
for(int j=1;j<=m && i*prime[j]<=sqn;j++)
{
npri[i*prime[j]]=1;
if(i%prime[j]==0) break;
}
}
for(ll i=1,j;i<=n;i=j+1) j=n/(n/i),blo[++tot]=n/i;
auto getid=[&](ll x)->int{return x<=sqn?tot-x+1:n/x;};
for(int i=1;i<=tot;i++) f[i]=blo[i]-1;
for(int i=1;i<=m;i++)
{
double inv=1./prime[i];
for(int j=1;j<=tot && blo[j]>=(ll)prime[i]*prime[i];j++)
f[j]-=f[getid(blo[j]*inv+eps)]-i+1;
}
ll ans=n-(n-1-(f[getid(n)]-f[getid(n/2)]))/2;
memset(prime+1,0,sizeof(int)*m);
memset(blo+1,0,sizeof(ll)*tot);
memset(f+1,0,sizeof(ll)*tot);
memset(npri+1,0,sizeof(bool)*sqn);
return ans;
}
int main()
{
int T;
ll n;
cin>>T;
while(T--) scanf("%lld",&n),printf("%lld\n",solve(n));
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 4ms
memory: 5668kb
input:
2 4 9
output:
3 6
result:
ok 2 number(s): "3 6"
Test #2:
score: 0
Accepted
time: 5ms
memory: 7712kb
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: 1ms
memory: 7712kb
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: 4ms
memory: 7832kb
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: 969ms
memory: 16156kb
input:
5 47890123456 12345678901 96666666669 85555555558 100000000000
output:
24438086351 6307451722 49300536501 43638011231 50999200118
result:
ok 5 number(s): "24438086351 6307451722 49300536501 43638011231 50999200118"