2 条题解

  • 0
    @ 2026-5-21 10:44:26
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    int pp[7000010],pr[1000010],pi,mu[7000010];
    void ols(int n){
    	for(int i=2;i<=n;i++){
    		if(!pp[i]){
    			pr[++pi]=i;
    		}
    		for(int j=1;j<=pi&&i*pr[j]<=n;j++){
    			mu[i*pr[j]]=pr[j];
    			pp[i*pr[j]]=1;
    			if(i%pr[j]==0)break;
    		}
    	}
    }
    int main(){
    	ios::sync_with_stdio(0);
    	cin.tie(0);
    	ols(5e6);
    	int n,p;
    	cin>>n>>p;
    	if(p==2){
    		if(2*n>1e9){
    			cout<<0;
    			return 0;
    		}
    		cout<<2*n;
    		return 0;
    	}
    	else if(p==3){
    		if(3ll*(n*2-1)>1e9){
    			cout<<0;
    			return 0;
    		}
    		cout<<3*(n*2-1);
    		return 0;
    	}
    	else if(p<1000){
    		int cnt=0;
    		for(int i=1;p*i<=1e9;i++){
    			int fl=1;
    			for(int j=1;pr[j]<p;j++){
    				if(p*i%pr[j]==0){
    					fl=0;
    					break;
    				}
    			}
    			cnt+=fl;
    			if(cnt==n){
    				cout<<p*i;
    				return 0;
    			}
    		}
    		cout<<0;
    		return 0;
    	}
    	if((ll)n*p>1e9){
    		cout<<0;
    		return 0;
    	}
    	int i=0,cnt=0;
    	while(cnt<n){
    		i++;
    		while(((mu[i]&&mu[i]<p)||(!mu[i]&&i<p&&i!=1))&&i*p<=1e9){
    			i++;
    			if(i>5e6){
    				cout<<0;
    				return 0;
    			}
    		}
    		if(i*p>1e9){
    			cout<<0;
    			return 0;
    		}
    		cnt++;
    	}
    	cout<<i*p;
    	return 0;
    }
    
    • 0
      @ 2026-4-28 17:45:35

      传送门

      本题思路:

      这道题我们可以试一下暴力,一个一个去枚举计算 PP 的倍数,中途再判断一下这个数是否最小的公倍数是 PP,过程中进行判定,如果超了就直接输出 1-1,这样暴力就可以输出大部分答案。这样写必然是会超 11 个样例的时间,但是可以开 O2 过。

      本题代码:

      #include<bits/stdc++.h>
      using namespace std;
      int n,p,num;//n,p见题目中,num为答案 
      int main()
      {
      	cin>>n>>p;
      	if(p*n>1000000000)//如果p的n倍已经超过了边界,直接输出0,否则浪费时间
      	{
      		cout<<0;
      		return 0;
      	}
      	for(int i=1;i<=n;i++)//枚举答案 
      	{
      		num+=p;//每次加上一个p,向前推进 
      		if(num>1000000000)//如果超过了,直接输出0 
      		{
      			cout<<0;
      			return 0;
      		}
      		for(int j=2;j<p;j++)//判断是否是最小因数 
      		{
      			if(num%j==0)
      			{
      			   i--;//如果是就不算这个答案 
      			   break;
      			}
      		}
      	}
      	cout<<num;//输出答案	
      	return 0;//完结撒花!!! 
      }
      
      

      请勿抄袭,用此方法请记得开洛谷的 O2 优化。

      • 1

      信息

      ID
      4846
      时间
      1000ms
      内存
      64MiB
      难度
      8
      标签
      递交数
      26
      已通过
      5
      上传者