QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#260103 | #6728. To the Park | AH20 | AC ✓ | 53ms | 4836kb | C++14 | 1.9kb | 2023-11-21 20:02:31 | 2023-11-21 20:02:31 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e8+7;
const int maxm=1e5+7;
bitset<100000007>not_prime;
int prime[maxm],vis[maxm];
void check(int n){
int tot=0;
for(int i=2;i<=n;i++){
if(not_prime[i]==0){
prime[++tot]=i;
}
for(int j=1;j<=tot and i*prime[j]<=n;j++){
not_prime[i*prime[j]]=1;
if(i%prime[j]==0) break;
}
}
//cout<<tot<<endl;
}
void solve(){
int n;cin>>n;
if(n<=3){cout<<"0\n";return;}
for(int i=1;i<=n;i++) vis[i] = 0;
for(int i=1;i<=n;i++){
if(i == 1) vis[i] = 1;
if(!not_prime[i] and i*2>n){
vis[i] = 1;
}
}
//排除掉不可能结对的这些数字
vector<array<int,2>>all;
vector<int>left;
for(int i=n;i>=1;i--){
if(not_prime[i] or vis[i]) continue;
//如果已经被占用了就直接跳过
vector<int>tmp;
for(int j=i;j<=n;j+=i){
if(vis[j]) continue;
vis[j] = 1;
tmp.push_back(j);
}
int sz = tmp.size();
if(sz%2 == 0){
for(int i=0;i<sz;i+=2){
all.push_back({tmp[i],tmp[i+1]});
}
}
else{
swap(tmp[1],tmp[0]);
for(int i=1;i<sz;i+=2){
all.push_back({tmp[i],tmp[i+1]});
}
left.push_back(tmp[0]);
}
}
int sz = left.size();
if(sz&1){
left.pop_back();
--sz;
}
for(int i=0;i<sz;i+=2){
all.push_back({left[i],left[i+1]});
}
cout<<all.size()<<" ";
for(int i=0;i<all.size();i++){
auto u = all[i];
cout<<u[0]<<" "<<u[1];
if(i+1 < all.size()) cout<<" ";
}
cout<<"\n";
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
check(maxm);
int T;cin>>T;
while(T--) solve();
return 0;
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3676kb
input:
3 1 4 6
output:
0 1 2 4 2 3 6 2 4
result:
ok 4 cases
Test #2:
score: 0
Accepted
time: 53ms
memory: 4836kb
input:
1007 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101...
output:
0 0 0 1 2 4 1 2 4 2 3 6 2 4 2 3 6 2 4 2 3 6 2 8 3 3 9 2 8 6 4 4 5 10 3 9 2 8 6 4 4 5 10 3 9 2 8 6 4 4 5 10 3 6 9 12 2 8 4 5 10 3 6 9 12 2 8 5 7 14 5 10 3 6 9 12 2 8 6 7 14 5 15 3 6 9 12 2 8 10 4 6 7 14 5 15 3 6 9 12 2 4 8 16 6 7 14 5 15 3 6 9 12 2 4 8 16 7 7 14 5 15 3 9 12 18 2 4 8 16 10 6 7 7 14 5 ...
result:
ok 1008 cases