牛客 40E 珂朵莉的数论题

大意: 给定$x,y$, 求第$x$小的最小素因子为$y$的数, 若答案>1e9输出0.

若$y>=60$, 可以暴力筛出1e9/60以内的答案.

否则容斥+二分算出答案.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head



const int M = 2e7+10, S = 60;
int x, y, cnt, ret;
bool vis[M];
int p[S];

int dfs(int d, int num, int z) {
	return d>cnt?num*z:dfs(d+1,num,z)+dfs(d+1,num/p[d],-z);
}

int main() {
	scanf("%d%d", &x, &y);
	if (x==1) return printf("%d\n", y),0;
	if ((ll)y*y>1e9) return puts("0"),0;
	if (y>=S) {
		int n = 1e9/y, sum = 0;
		REP(i,2,y-1) if (!vis[i]) {
			for (int j=i;j<=n;j+=i) vis[j]=1;
		}
		REP(i,1,n) if (!vis[i]) {
			if (++sum==x) return printf("%d\n",i*y),0;
		}
		return puts("0"),0;
	}
	REP(i,2,y-1) if (!vis[i]) {
		p[++cnt] = i;
		for (int j=2*i; j<y; j+=i) vis[j]=1;
	}
	int l=1, r=1e9/y, ans=0;
	while (l<=r) {
		if (dfs(1,mid,1)>=x) ans=mid,r=mid-1;
		else l=mid+1;
	}
	printf("%d\n", ans*y);
}

猜你喜欢

转载自www.cnblogs.com/uid001/p/10920804.html