7-1
#include <bits/stdc++.h>
using namespace std;
bool check(string s)
{
int x = stoi(s);
if(x == 0 || x == 1) return true;
for(int i=2;i*i<=x;i++)
{
if(x % i == 0)
return true;
}
return false;
}
int main(){
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
bool flag = true;
string s; cin >> s;
while(s.size())
{
if(!check(s))
{
cout << s << " Yes" << endl;
}
else
{
cout << s << " No" << endl;
flag = false;
}
s = s.substr(1);
}
if(flag) puts("All Prime!");
return 0;
}
7-2
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10,M = 1e5 + 10;
vector<int> v;
bool s[M];
int n,m;
int g[11][N];
bool st[11];
bool ex[M];
int main(){
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
int a,b;
cin >> a >> b;
v.push_back(a);
v.push_back(b);
ex[a] = true;
ex[b] = true;
s[abs(b-a)] = true;
cin >> n >> m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin >> g[i][j];
for(int j=1;j<=m;j++)
for(int i=1;i<=n;i++)
{
if(st[i]) continue;
int x = g[i][j];
if(!s[x] || ex[x])
{
st[i] = true;
printf("Round #%d: %d is out.\n",j,i);
}
else
{
ex[x] = true;
for(auto y : v)
s[abs(x-y)] = true;
v.push_back(x);
}
}
vector<int> res;
for(int i=1;i<=n;i++)
if(!st[i])
res.push_back(i);
if(res.size() == 0) puts("No winner.");
else
{
printf("Winner(s):");
for(auto x : res)
cout << " " << x;
cout << endl;
}
return 0;
}
7-3
#include <bits/stdc++.h>
using namespace std;
const int N = 510;
int n,m,k;
bool st[N][N];
void solve()
{
vector<int> v(n);
set<int> s;
for(auto &x : v)
{
cin >> x;
s.insert(x);
}
if(s.size() > k)
{
puts("Error: Too many species.");
return ;
}
if(s.size() < k)
{
puts("Error: Too few species.");
return ;
}
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
{
if(v[i] == v[j] && st[i + 1][j + 1])
{
puts("No");
return ;
}
}
puts("Yes");
}
int main(){
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
cin >> n >> m >> k;
while(m -- )
{
int a,b;
cin >> a >> b;
st[a][b] = st[b][a] = true;
}
int T;
cin >> T;
while(T--)
{
solve();
}
return 0;
}
7-4
#include <bits/stdc++.h>
#define pii pair<int,int>
using namespace std;
const int N = 1e5 + 10;
vector<int> v[N];
int n,m;
int a[N];
int main(){
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
cin >> n >> m;
for(int i=0;i<n;i++)
cin >> a[i];
priority_queue<pii,vector<pii>,greater<pii>> heap;
for(int i=0;i<m;i++)
heap.push({0,a[i]});
for(int i=m;i<n;i++)
{
int x = a[i];
int cur;
if(x >= heap.top().second)
cur = heap.top().first;
else
cur = heap.top().first + 1;
v[heap.top().first].push_back(heap.top().second);
heap.pop();
heap.push({cur,x});
}
while(heap.size())
{
auto t = heap.top();
v[t.first].push_back(t.second);
heap.pop();
}
int cur = 0;
while(v[cur].size())
{
for(int i=0;i<v[cur].size();i++)
{
if(!i) cout << v[cur][i];
else cout << " " << v[cur][i];
}
cout << endl;
cur ++ ;
}
return 0;
}