B. Different Divisors(思维/打表)

https://codeforces.com/contest/1474/problem/B

题意:

We gave you an integer d and asked you to find the smallest positive integer a, such that

  • aa has at least 4 divisors;
  • difference between any two divisors of a is at least d.

思路:

首先至少4个因子。假设刚好取4个,那么一个1,一个本身,剩下2个。设答案为x。x可以质因数分解为p1^k1*p2^k2*p3*k3*p4^k4.....pn^kn

那么只要kn不为0,pn就是x的因子。如果p1(x的因子)和p2(x的因子)不满足>=d,那么该x不符合条件。如果已经满足>=d,那么x要最小取其次幂为1.于是就x变成了两个质数相乘。


如果一时间没有思路,为什么不打表看看呢?(打表程序)

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5;
typedef long long LL;
inline LL read()
{
	LL x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
LL a[10000];
bool check(LL x,LL d){
    LL sum=0;
    memset(a,0,sizeof(a));
    for(LL i=1;i<=sqrt(x);i++){
        if(x%i==0&&i*i!=x){
            a[++sum]=i;a[++sum]=x/i;
        }
        if(i*i==x){
            a[++sum]=i;
        }
    }
    sort(a+1,a+1+sum);
    for(LL i=1;i<=sum;i++){
        cout<<a[i]<<" ";
    }
    cout<<endl;
    if(sum<4) return false;

    for(LL i=1;i<=sum-1;i++){
        if(abs(a[i]-a[i+1])<d){
            return false;
        }
    }
    return true;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL t;cin>>t;
  while(t--){
    LL d;cin>>d;

    for(LL i=6;i<=1000;i++){
        if( check(i,d)==true ){
            cout<<i<<endl;
            break;
        }
    }
  }
return 0;
}

可以观察出每次中间两个数是质数。于是多组用筛处理质数,然后质数存下来是个单调序列,二分去找即可。

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5;
typedef long long LL;
LL v[100010],primes[100100];///v[]存的是最小质因子,primes[]存的是质数
LL m=0;///质数数量
void getprimes(LL x){
    for(LL i=2;i<=x;i++){
        if(v[i]==0) {v[i]=i;primes[++m]=i;}
        for(LL j=1;j<=m;j++){
            if(primes[j]>v[i]||primes[j]*i>x) break;
            v[i*primes[j]]=primes[j];
        }
    }
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  getprimes(100000);
  LL t;cin>>t;
  while(t--){
    LL d;cin>>d;
    LL pos1=lower_bound(primes+1,primes+m+1,1+d)-(primes);
    LL pos2=lower_bound(primes+1,primes+m+1,primes[pos1]+d)-(primes);
    cout<<primes[pos1]*primes[pos2]<<endl;
  }
return 0;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/112859341
今日推荐