c++中有一个名字叫做 popen 的函数,其作用为执行命令并以 FILE* 类型存下输出内容。洛谷并没有禁止这一函数,于是试图提交以下代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
stringstream w;
w<<"factor "<<n<<'\n';
FILE *fp=popen(w.str().c_str(),"r");
char p[1024]="";
fgets(p,1024,fp);
int le=strlen(p);
p[le++]=' ';
vector<string>c;
string buc="";
for(int i=0;i<le;i++){
if(p[i]==' '){
c.push_back(buc);
buc="";
}else{
buc+=p[i];
}
}
if(c.size()==2)cout<<"Prime\n";
else{
c.back().pop_back();
cout<<c.back()<<'\n';
}
}
return 0;
}
它 RE 了。 通过下载一个数据并在 luogu IDE 测试可知这个函数至多调用 20 次。之后调用会 RE。 于是离线并将询问拼接:
#include<bits/stdc++.h>
using namespace std;
#define int long long
char p[1024];
signed main(){
int t;
cin>>t;
stringstream w;
for(int i=1;i<=t;i++){
int n;
cin>>n;
w<<"factor "<<n<<" ; ";
}
FILE* fp=popen(w.str().c_str(),"r");
for(int i=1;i<=t;i++){
fgets(p,1024,fp);
int le=strlen(p);
p[le++]=' ';
vector<string>c;
string buc="";
for(int i=0;i<le;i++){
if(p[i]==' '){
c.push_back(buc);
buc="";
}else{
buc+=p[i];
}
}
if(c.size()==2)cout<<"Prime\n";
else{
c.back().pop_back();
cout<<c.back()<<'\n';
}
}
return 0;
}
它过了。