3.14总结

· · 个人记录

月色的GCD与LCM

错误原因

lcm计算错误

正确思路

  lcm用a*b/__gcd(a,b)来求

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int t,x,y;
int lcm(int x,int y){
    return x*y/__gcd(x,y);//求lcm
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>t;
    while(t--){
        cin>>x>>y;
        cout<<__gcd(x,y)<<" "<<lcm(x,y)<<"\n";
    }
    return 0;
}

月色的数列寻找

错误原因

  分解质因数的思路错误

正确思路

  用分解质因数的方法得到所有的质因数,再输出

正确代码

#include <bits/stdc++.h>
using namespace std;
int n,k;
int main(){
    cin>>n>>k;
    int m=n,cnt=0;
    if(n<k){cout<<-1;return 0;}
    vector<int> res;
    for(int i=2;i<=sqrt(m);i++){
        while(m%i==0 && cnt<k-1){
            res.push_back(i);
            m/=i;
            cnt++;
        }
    }
    if(cnt==k-1 && m>1){
        res.push_back(m);
        for(int x:res)cout<<x<<" ";
    }else{
        cout<<-1;
    }
    return 0;
}

月色的幸运数

错误原因

没有解题思路

正确思路

利用唯一分解定理解题

正确代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int n,k;
    cin>>n>>k;
    while(n--){
        map<int,int>mp;
        int x;
        cin>>x;
        int t=x;
        for(int i=2;i<=t/i;i++){//用埃氏筛
            while(x%i==0){
                mp[i]++;
                x/=i;
            }
        }
        if(x>1) mp[x]++;//剩余的要加上
        int cnt=mp.size();
        cout<<(cnt==k ? "Yes":"No")<<"\n";
    }
    return 0;
}

月色的幸运数字个数

错误原因

没有解题思路

正确思路

用埃氏筛才能正确的解题

正确代码

#include<bits/stdc++.h>
using namespace std;
const int MAX=1e7+10;
int cnt[MAX]; 
void isp(){
    for(int i=2;i<MAX;i++){
        if(cnt[i]==0){
            for(int j=i;j<MAX;j+=i){
                cnt[j]++;
            }
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    isp();
    int t;
    cin>>t;
    while(t--){
        int r,k,ans=0;
        cin>>r>>k;
        if(k<0||k>10)ans=0;
        else{
            r=min(r,MAX-1);
            for(int i=1;i<=r;i++){
                if(cnt[i]==k)ans++;
            }
        }
        cout<<ans<<"\n";
    }
    return 0;
}

月色的神奇排列

错误原因

没有解题思路

正确思路

偶数的情况,相邻两项反着放,奇数的情况,第一个放1,其余的相邻两个反着放

正确代码

#include <bits/stdc++.h>
using namespace std;
int t,n;
int main(){
    cin>>t;
    while(t--){
        cin>>n;
        if(n%2==0){//偶数
            for(int i=1;i<=n;i+=2){
                cout<<i+1<<" "<<i<<" ";
            }
            cout<<"\n";
        }else{//奇数
            cout<<1<<" ";//第一个放1
            for(int i=2;i<=n;i+=2){
                cout<<i+1<<" "<<i<<" ";
            }
            cout<<"\n";
        }
    }
    return 0;
}

月色的神奇数对

错误原因

没有找到正确的规律

正确思路

利用公式n+n/2+n/2+n/3+n/3来计算

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int t,n; 
signed main(){
    cin>>t;
    while(t--){
        cin>>n;
        cout<<n+n/2+n/2+n/3+n/3<<"\n";//利用公式
    }
    return 0;
}