跳到正文

个人记录

总结

xuyulan约 8 分钟
原文
文章信息与版本

文章信息

收录
2026年1月5日
更新
2026年1月6日
篇幅
约 8 分钟

版本

  1. v1当前

T717048 数学专题2-1

100分

C++
#include<bits/stdc++.h>
using namespace std;
const int N=5e3+50;
int n,m,v[N];
char s=',';
void ch(){
	v[1]=1;
	for(int i=2;i<=n;i++){
		if(!v[i]){
			for(int j=i*2;j<=n;j+=i){
				v[j]=1;
			}
		}
	}
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>m>>n;
	ch();
	for(int i=m;i<=n;i++){
		int ans=i;
		for(int j=2;j<i;j++){
			if(i%j==0&&!v[j]){
				ans=j;
			}
		}
		if(i!=m){
			cout<<s;
		}
		cout<<ans;
	}
	return 0;
}

T717049 数学专题2-2

70分(超时)

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+50;
long long n,ans,a[N],t,c;
int ch(int x){
	if(x==1)return 0;
	for(int i=2;i<x;i++){
		if(x%i==0)return 0;
	}
	return 1;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>t;
	while(t--){
		cin>>n;
		c=0;
		for(int i=2;i*i<=n;i++){
			if(n%i==0&&ch(i)){
				while(n%i==0){
					cout<<i<<" ";
					n/=i;
				}
			}
		}
		if(ch(n)){
			cout<<n;
		}
		cout<<"\n";
	}
	return 0;
}

100分

  1. 可以把判断质数删除,应为只要n%i==0,i便保证为质数
  2. 最后一个判断该为n>1;
C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+50;
long long n,ans,a[N],t,c;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>t;
	while(t--){
		cin>>n;
		c=0;
		for(long long i=2;i*i<=n;i++){
			if(n%i==0){
				while(n%i==0){
					cout<<i<<" ";
					n/=i;
				}
			}
		}
		if(n>1){
			cout<<n;
		}
		cout<<"\n";
	}
	return 0;
}

T717047 数学专题2-3

95分(超时)

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+50;
int n,ans=1e9,a[N];
int ch(int x){
	if(x==1)return 0;
	for(int i=2;i<x;i++){
		if(x%i==0)return 0;
	}
	return 1;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n;
	ans=n;
	while(1){
		if(ch(ans)){
			cout<<ans;
			return 0;
		}
		int cnt=ans;
		ans=0;
		for(int i=2;i*i<=cnt;i++){
			if(cnt%i==0&&ch(i)){
				while(cnt%i==0){
					ans+=i;
					cnt/=i;
				}
			}
		}
		if(ch(cnt)){
			ans+=cnt;
		}
	}
	return 0;
}

100分

  1. 可以把判断质数删除,应为只要n%i==0,i便保证为质数(同样适用)
  2. 最后一个判断该为n>1;
  3. 删除第一个判断并在后面加一个判断(如果ans没变,输出ans)
C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+50;
int n,ans=1e9,a[N];
int ch(int x){
	if(x==1)return 0;
	for(int i=2;i<x;i++){
		if(x%i==0)return 0;
	}
	return 1;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n;
	ans=n;
	while(1){
		int cnt=ans,k=ans;
		ans=0;
		for(long long i=2;i*i<=cnt;i++){
			if(cnt%i==0){
				while(cnt%i==0){
					ans+=i;
					cnt/=i;
				}
			}
		}
		if(cnt>1){
			ans+=cnt;
		}
		if(k==ans){
			cout<<ans;
			return 0;
		}
	}
	return 0;
}

T717052 数学专题2-4

0分

100分

k 个数的公约数含义就是这k个数均含有某个因数,那我们把所有数的因数全部求出来,就会发 现有k个数均含有某个因数,那么这个数必然是这 k 个数的公约数。其中找出最大的就是它们的最大公约 数。

按因子从大到小枚举,如果这个因子出现的次数大于等于 k, 就说明这个因子就是选 k 个 数的最大公约数

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+50;
long long n,ans,a[N],t,c;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>t;
	while(t--){
		cin>>n;
		c=0;
		for(long long i=2;i*i<=n;i++){
			if(n%i==0){
				while(n%i==0){
					cout<<i<<" ";
					n/=i;
				}
			}
		}
		if(n>1){
			cout<<n;
		}
		cout<<"\n";
	}
	return 0;
}

T716631 数学专题2-5

0分

100分

将A和B中相同的数字配对移除

  • 如果剩余数字数量不等于1,则A/B不可能是质数(除非剩余为1,但1不是质数)
  • 如果剩余数字数量为1,检查该数字是否为质数
C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+50;
int n,t,a[N],m,b[N],c[N];
bool ch(int x){
	if(x<=1){
		return 0;
	}
	for(int i=2;i<=x/i;i++){
		if(x%i==0)return 0;
	}
	return 1;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>t;
	while(t--){
		cin>>n>>m;
		for(int i=1;i<=n;i++){
			cin>>a[i];
		}
		for(int i=1;i<=m;i++){
			cin>>b[i];
		}
		sort(a+1,a+1+n);
		sort(b+1,b+1+m);
		int x=0,y=1;
		for(int i=1;i<=n;i++){
			if(b[y]!=a[i]){
				c[++x]=a[i];
			}else{
				y++;
				if(y>m){
					for(int j=i+1;j<=n;j++)c[++x]=a[j];
					break;
				}
			}
		}
		if(x!=1){
			cout<<"NO\n";
		}else{
			if(ch(c[1])==0)cout<<"NO\n";
			else cout<<"YES\n";
		}
	}
	return 0;
}
原文