B. Different Divisors (thinking/watching)

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

Title:

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.

Ideas:

First at least 4 factors. Assuming that four are taken, then one is 1, one itself, and two are left. Let the answer be x. x can be decomposed by prime factors into p1^k1*p2^k2*p3*k3*p4^k4...pn^kn

So as long as kn is not 0, pn is the factor of x. If p1 (the factor of x) and p2 (the factor of x) do not satisfy >=d, then the x does not meet the condition. If >=d has been satisfied, then x must be taken to the smallest power of 1. So x becomes the multiplication of two prime numbers.


If you don’t have any ideas for a while, why don’t you check the watch? (The watch program)

#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;
}

It can be observed that the two numbers in the middle are prime numbers each time. So multiple groups use sieves to process prime numbers, and then prime numbers are saved as a monotonic sequence, just look for them in two points.

#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;
}

 

Guess you like

Origin blog.csdn.net/zstuyyyyccccbbbb/article/details/112859341