A.求到 1 的最小步数,显然看出三步花费每次减少一次减少。模拟即可
//A
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int q;
int ans,flag;
ll tp;
cin>>q;
while(q--){
cin>>tp;
ans = 0; flag = 0;
while(tp!=1){
if(tp%2!=0&&tp%3!=0&&tp%5!=0){
cout<<-1<<endl; flag = 1;
break;
}
while(tp%2==0){
tp/=2;
ans++;
}
if(tp%3==0){
tp=tp/3*2;
ans++;
}
if(tp%5==0){
tp=tp/5*4;
ans++;
}
}
if(!flag)cout<<ans<<endl;
}
return 0;
}
B.简单模拟
//B
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n,t,ans,m;
int a[101];
int b[101];
int main()
{
cin>>t;
while(t--){
cin>>n;
ans = 0; b[1]=0; b[2]=0;
int j=0;
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]%3!=0){
if(a[i]%3==1)b[1]++;
else b[2]++;
}else ans++;
}
m = min(b[1],b[2]);
if(b[1]>m)ans+=(b[1]-m)/3;
if(b[2]>m)ans+=(b[2]-m)/3;
cout<<ans+m<<endl;
}
return 0;
}
C. 求最少剔除多少个数只有顺序序列 4,8,15,16,23,42
//C
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int a[500005];
int mp[101];
int cnt[7];
int n,m;
int main()
{
cin>>n;
mp[4] = 1; mp[8] = 2; mp[15] = 3;
mp[16] = 4; mp[23] = 5; mp[42] = 6;
cnt[0] = 500005;
int tp;
for(int i=0;i<n;i++)
{
cin>>tp;
tp = mp[tp];
if(cnt[tp-1]){
cnt[tp]++; cnt[tp-1]--;
}
}
cout<<n-cnt[6]*6<<endl;
return 0;
}
/*
100
4 42 23 23 8 42 16 23 42 16 42 8 4 23 4 4 23 42 16 42 23 23 23 42 4 42 8 8 16 23 15 23 16 4 42 15 15 23 16 15 16 4 4 15 23 42 42 15 8 23 8 23 4 15 16 15 42 8 23 16 15 42 23 8 4 16 15 16 23 16 16 4 23 16 8 23 16 15 23 4 4 8 15 4 4 15 8 23 23 4 4 8 8 4 42 15 4 4 42 16
*/
D.
题意:如果ai是素数,那么b中附加的就是第ai个素数,否则就是ai的最大公因数在bi中,
从大到小,会有一 一对应的关系,low[i*prime[j] ] = prime[j]是线性筛的性质保证每个合数只会被它的最小质因数筛去
//D
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 2750131+10;
int n;
int prime[maxn+1],is_prime[maxn+1];
int low[maxn+1];
int mp[maxn+1];
multiset<int> q;
vector<int> ans;
void phi_prime()
{
memset(is_prime,0,sizeof(is_prime));
for(int i=2;i<=2750131;i++){
if(!is_prime[i])prime[++prime[0]]=i;
for(int j=1;j<=prime[0]&&i*prime[j]<=2750131;j++){
is_prime[i*prime[j]] = 1; low[i*prime[j]] = prime[j];
if(i%prime[j]==0)break;
}
}
for(int i=1;i<=prime[0];i++){
mp[prime[i]] = i;
}
}
int main()
{
int n,tp;
phi_prime();
cin>>n;
q.clear();
for(int i=0;i<2*n;i++){
cin>>tp;
q.insert(tp);
}
while(q.size()){
int x = *q.rbegin();
q.erase(--q.end());
if(is_prime[x]){
multiset<int>::iterator it = q.find(x/low[x]);
ans.push_back(x); q.erase(it);
}else{
multiset<int>::iterator it = q.find(mp[x]);
ans.push_back(mp[x]); q.erase(it);
}
}
for(int i=0;i<ans.size();i++){
cout<<ans[i]<<" \n"[i==ans.size()-1];
}
return 0;
}
E.
黑白染色问题,这个图联通,则一定存在答案。
//E
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 200005;
vector<int> ans,G[maxn];
int vis[maxn];
int n,m;
void dfs(int u,int fg){
vis[u] = 1;
if(fg) ans.push_back(u);
for(int i=0;i<G[u].size();i++){
int v = G[u][i];
if(vis[v])continue;
dfs(v,fg^1);
}
}
int main()
{
cin.tie(0);
int t,u,v;
cin>>t;
while(t--){
cin>>n>>m;
for(int i=1;i<=n;i++)G[i].clear(),vis[i]=0;
ans.clear();
for(int i=0;i<m;i++){
cin>>u>>v;
G[u].push_back(v); G[v].push_back(u);
}
dfs(1,0);
if(ans.size()<=n/2){
cout<<ans.size()<<endl;
for(int i=0;i<ans.size();i++){
cout<<ans[i]<<" \n"[i==ans.size()-1];
}
}else {
ans.clear();
for(int i=1;i<=n;i++)
vis[i] = 0;
dfs(1,1);
cout<<ans.size()<<endl;
for(int i=0;i<ans.size();i++){
cout<<ans[i]<<" \n"[i==ans.size()-1];
}
}
}
return 0;
}