Acwing第 59 场周赛(AK)

本文介绍了两个算法问题的解决方案:一是计算整数n的质因子减少过程中的减法次数,二是利用并查集解决环形连通分量的问题。对于质因子问题,通过寻找最小质因子并不断减去它来计算次数;在连通分量问题中,利用并查集进行边的连接和组件的查找,最终统计满足特定条件的环形连通分量数量。
摘要由CSDN通过智能技术生成
4492. 减法操作

给定一个整数 n n n,执行如下算法:

  1. 如果 n = 0 n=0 n=0,则结束算法。
  2. 找到 n n n 的最小质因子 d d d
  3. n n n 减去 d d d 并跳转步骤 1 1 1

请你计算,在算法执行的过程中,一共进行了多少次减法操作。

输入格式

一个整数 n n n

输出格式

一个整数,表示减法操作的次数。

数据范围
所有测试点满足 2 ≤ n ≤ 1 0 10 2≤n≤10^{10} 2n1010
/*
A: 10min
B: 20min
C: 30min
D: 40min
*/ 
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <assert.h>
#include <sstream>
#define pb push_back 
#define all(x) (x).begin(),(x).end()
#define mem(f, x) memset(f,x,sizeof(f)) 
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo_(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

template<typename T>
ostream& operator<<(ostream& os,const vector<T>&v){for(int i=0,j=0;i<v.size();i++,j++)if(j>=5){j=0;puts("");}else os<<v[i]<<" ";return os;}
template<typename T>
ostream& operator<<(ostream& os,const set<T>&v){for(auto c:v)os<<c<<" ";return os;}
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const map<T1,T2>&v){for(auto c:v)os<<c.first<<" "<<c.second<<endl;return os;}
template<typename T>inline void rd(T &a) {
    char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
    while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}

typedef pair<long long ,long long >PII;
typedef pair<long,long>PLL;

typedef long long ll;
typedef unsigned long long ull; 
const int N=2e5+10,M=1e9+7;

ll n,cnt;
// 判断n是不是质数
bool is_prime(ll n) {
    if (n == 1) return false;
    for (ll i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}
void solve(){
    cin>>n;
    if(!is_prime(n)){
        if(n&1){
            // 求n的最小质因子
            ll x=n;
            ll i;
            for( i=2;i*i<=x;i++){
                if(x%i==0){
                    break;
                }
            }
            cout<<1+(x-i)/2<<endl;
        }
        else
            cout<<n/2;
    }
    else{
        cout<<1;
    }
}

int main(){
    solve();
	return 0;
}


4493. 环形连通分量

并查集水题,题意模拟就行

/*
A: 10min
B: 20min
C: 30min
D: 40min
*/ 
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <assert.h>
#include <sstream>
#define pb push_back 
#define all(x) (x).begin(),(x).end()
#define mem(f, x) memset(f,x,sizeof(f)) 
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo_(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

template<typename T>
ostream& operator<<(ostream& os,const vector<T>&v){for(int i=0,j=0;i<v.size();i++,j++)if(j>=5){j=0;puts("");}else os<<v[i]<<" ";return os;}
template<typename T>
ostream& operator<<(ostream& os,const set<T>&v){for(auto c:v)os<<c<<" ";return os;}
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const map<T1,T2>&v){for(auto c:v)os<<c.first<<" "<<c.second<<endl;return os;}
template<typename T>inline void rd(T &a) {
    char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
    while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}

typedef pair<long long ,long long >PII;
typedef pair<long,long>PLL;

typedef long long ll;
typedef unsigned long long ull; 
const int N=4e5+10,M=1e9+7;

int n,m;
int h[N],e[N],ne[N],idx;
int cnt;
vector<int>g[N];
int fa[N];
void add(int a,int b){
    e[idx]=b;
    ne[idx]=h[a];
    h[a]=idx++;
}

int find(int u){
    return fa[u]==u?u:fa[u]=find(fa[u]);
}

void merge(int u,int v){
    int fu=find(u),fv=find(v);
    if(fu==fv)return;
    fa[fu]=fv;
}
int d[N];
void solve(){   
    cin>>n>>m;
    memset(h,-1,sizeof(h));
    fo(i,1,n)fa[i]=i;
    fo(i,1,m){
        int u,v;cin>>u>>v;
        add(u,v);add(v,u);
        d[u]++;
        d[v]++;
        merge(u,v);
    }
    fo(i,1,n){
        g[find(i)].pb(i);
    }
    // fo(i,1,n){
    //     cout<<i<<" "<<g[i].size()<<endl;
    // }
    for(int i=1;i<=n;i++){
        if(g[i].size()>=3){
            bool flag=1;
            for(auto c:g[i]){
                if(d[c]!=2){
                    flag=0;
                    break;
                }
            }
            if(flag){
                cnt++;
            }
        }
    }
    cout<<cnt<<endl;
}

int main(){
    solve();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值