CF1647D Madoka and the Best School in Russia
Luogu 链接
CodeForces 链接
Virtual Judge 链接
题意
题目描述
定义好数为 \(d\) 的倍数,不能分解为两好数之积的为美丽数。
现在给定两个正整数 \(x\) 和 \(d\),试问 \(x\) 是否有至少两种方式写为至少一个美丽数之积。
输入格式
多测,第一行一个正整数 \(T\)(\(1\le T\le 100\)),表示有 \(T\) 组数据。
每组数据第一行两个正整数 \(x\) 和 \(d\)(\(2\le x,d\le 10^9\)),含义见题目描述。
输出格式
对于每组数据,若 \(x\) 有至少两种方式写为至少一个美丽数之积,则输出 \(\texttt{YES}\);否则输出 \(\texttt{NO}\)(忽略大小写)。
思路
令 \(\displaystyle x=d^k\times y\),满足 \(d^{k+1}\nmid x\),则显然 \(k-1\) 个 \(d\) 和 一个 \(d\times y\) 是满足条件的一个解。接下来讨论是否有其他解。
- 当 \(k=1\) 时(第一类):显然无第二种解。
- 当 \(k>1\) 时:
- 若 \(y\) 为合数(第二类):令 \(y=a\times b\),则 \(k-2\) 个 \(d\)、一个 \(d\times a\) 和一个 \(d\times b\) 为满足条件的一个解。
- 若 \(y\) 为质数或 \(1\):考虑分解 \(d\)。
- \(d\) 为质数(第三类):显然无第二种解。
- \(d\) 为合数:令 \(d=y^q\times z\),满足 \(y^{q+1}\nmid d\)。
- 若 \(z\neq 1\)(第四类):\(k-3\) 个 \(d\)、一个 \(d\times y\)、一个 \(d\times z\) 和一个 \(d\times y^q\) 为满足条件的一个解(须满足 \(k>2\))。
- 若 \(z=1\):
- 若 \(q>2\)(第五类):\(k-3\) 个 \(d\)、一个 \(d\times y^2\)、一个 \(d\times y^{q-1}\) 为满足条件的一个解(须满足 \(k>2\))。
- 若 \(q=2\)(第六类):\(k-4\) 个 \(d\) 和三个 \(d\times y\) 为满足条件的一个解(须满足 \(k>3\))。
注意到后三类可以整合为对 \(y^2\) 是否等于 \(d\) 的判断。
程序
#include<cstdlib>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdio>
#include<iostream>
#include<vector>
#include<map>
#include<cmath>
#include<iomanip>
#include<string>
#include<stack>
#include<random>
#include<utility>
#define re register
#define ll long long
#define ull unsigned long long
#define vl __int128
#define ld long double
#define LL 2e18
#define INT 1e9
#define INF 0x3f3f3f3f
#define MP make_pair
#define pb push_back
#define lb(x) (x&(-x))
#ifdef __linux__
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc _getchar_nolock
#define pc _putchar_nolock
#endif
int T=1;
using namespace std;
pair<int*,int*> pair_int;pair<ll*,ll*> pair_ll;pair<ull*,ull*> pair_ull;pair<double*,double*> pair_double;pair<ld*,ld*> pair_ld;pair<char*,char*> pair_char;pair<bool*,bool*> pair_bool;
inline bool blank(const char x){return !(x^32)||!(x^10)||!(x^13)||!(x^9);}
template<typename Tp>inline void read(Tp &x){x=0;bool z=true;char a=gc();for(;!isdigit(a);a=gc())if(a=='-')z=false;for(;isdigit(a);a=gc())x=(x<<1)+(x<<3)+(a^48);x=(z?x:~x+1);}
inline void read(double &x){x=0.0;bool z=true;double y=0.1;char a=gc();for(;!isdigit(a);a=gc())if(a=='-')z=false;for(;isdigit(a);a=gc())x=x*10+(a^48);if(a!='.')return x=z?x:-x,void();for(a=gc();isdigit(a);a=gc(),y/=10)x+=y*(a^48);x=(z?x:-x);}
inline void read(ld &x){x=0.0;bool z=true;ld y=0.1;char a=gc();for(;!isdigit(a);a=gc())if(a=='-')z=false;for(;isdigit(a);a=gc())x=x*10+(a^48);if(a!='.')return x=z?x:-x,void();for(a=gc();isdigit(a);a=gc(),y/=10)x+=y*(a^48);x=(z?x:-x);}
inline void read(char &x){for(x=gc();blank(x)&&(x^-1);x=gc());}
inline void read(char *x){char a=gc();for(;blank(a)&&(a^-1);a=gc());for(;!blank(a)&&(a^-1);a=gc())*x++=a;*x=0;}
inline void read(string &x){x="";char a=gc();for(;blank(a)&&(a^-1);a=gc());for(;!blank(a)&&(a^-1);a=gc())x+=a;}
template<typename T>inline void read(pair<T*,T*> p){T *begin,*end,*i;begin=p.first,end=p.second;if(begin<end)for(i=begin;i<end;++i)read(*i);else for(i=begin-1;i>=end;--i)read(*i);}
template<typename T,typename ...Tp>inline void read(T &x,Tp &...y){read(x),read(y...);}
template<typename Tp>inline void write(Tp x){if(!x)return pc(48),void();if(x<0)pc('-'),x=~x+1;int len=0;char tmp[64];for(;x;x/=10)tmp[++len]=x%10+48;while(len)pc(tmp[len--]);}
inline void write(const double x){int a=6;double b=x,c=b;if(b<0)pc('-'),b=-b,c=-c;double y=5*powl(10,-a-1);b+=y,c+=y;int len=0;char tmp[64];if(b<1)pc(48);else for(;b>=1;b/=10)tmp[++len]=floor(b)-floor(b/10)*10+48;while(len)pc(tmp[len--]);pc('.');for(c*=10;a;a--,c*=10)pc(floor(c)-floor(c/10)*10+48);}
inline void write(const ld x){int a=6;ld b=x,c=b;if(b<0)pc('-'),b=-b,c=-c;ld y=5*powl(10,-a-1);b+=y,c+=y;int len=0;char tmp[64];if(b<1)pc(48);else for(;b>=1;b/=10)tmp[++len]=floor(b)-floor(b/10)*10+48;while(len)pc(tmp[len--]);pc('.');for(c*=10;a;a--,c*=10)pc(floor(c)-floor(c/10)*10+48);}
inline void write(const pair<int,double>x){int a=x.first;if(a<7){double b=x.second,c=b;if(b<0)pc('-'),b=-b,c=-c;double y=5*powl(10,-a-1);b+=y,c+=y;int len=0;char tmp[64];if(b<1)pc(48);else for(;b>=1;b/=10)tmp[++len]=floor(b)-floor(b/10)*10+48;while(len)pc(tmp[len--]);a&&(pc('.'));for(c*=10;a;a--,c*=10)pc(floor(c)-floor(c/10)*10+48);}else printf("%.*lf",a,x.second);}
inline void write(const pair<int,ld>x){int a=x.first;if(a<7){ld b=x.second,c=b;if(b<0)pc('-'),b=-b,c=-c;ld y=5*powl(10,-a-1);b+=y,c+=y;int len=0;char tmp[64];if(b<1)pc(48);else for(;b>=1;b/=10)tmp[++len]=floor(b)-floor(b/10)*10+48;while(len)pc(tmp[len--]);a&&(pc('.'));for(c*=10;a;a--,c*=10)pc(floor(c)-floor(c/10)*10+48);}else printf("%.*Lf",a,x.second);}
inline void write(const char x){pc(x);}
inline void write(const bool x){pc(x?49:48);}
inline void write(char *x){fputs(x,stdout);}
inline void write(const char *x){fputs(x,stdout);}
inline void write(const string &x){fputs(x.c_str(),stdout);}
template<typename T>inline void write(pair<T*,T*> p){T *begin,*end,*i;begin=p.first,end=p.second;for(i=begin;i<end;++i)write(*i),write(' ');}
template<typename T>inline void write(pair<pair<T*,T*>,char> p){T *begin,*end,*i;begin=p.first.first,end=p.first.second;char c=p.second;for(i=begin;i<end;++i)write(*i),write(c);}
template<typename T,typename ...Tp> inline void write(T x,Tp ...y){write(x),write(y...);}
template<typename T>inline void init(T *begin,T *end,const T& val=T()){T* i;for(i=begin;i<end;++i)*i=val;}
template<typename T>inline T max(T *begin,T *end){T *ans,*i;for(i=begin;i<end;++i)if(i==begin||*ans<*i)ans=i;return *ans;}
template<typename T>inline T min(T *begin,T *end){T *ans,*i;for(i=begin;i<end;++i)if(i==begin||*i<*ans)ans=i;return *ans;}
template<typename T>inline T calc_sum(T *begin,T *end,const T& val=T()){T ans=val,*i;for(i=begin;i<end;++i)ans+=*i;return ans;}
template<typename T>inline bool is_equal(T *begin,T *end,const T& val=T()){T *i;for(i=begin;i<end;++i)if(*i!=val)return false;return true;}
template<typename T>inline T qp(T x,T y){T ans=1;while(y){if(y&1)ans=ans*x;x=x*x;y>>=1;}return ans;}
template<typename T>inline T qp(T x,T y,T z){T ans=1;while(y){if(y&1)ans=ans*x%z;x=x*x%z;y>>=1;}return ans;}
template<typename T>inline T abs(T x){return x>=0?x:-x;}
ll mod=0;
const int MAXN=1e5;
const int N=MAXN+10;
//#define DEBUG
#define more_text
ll n,d;
ll k;
bool is_prime(ll x){
if(x<4)return true;
if(x%2==0||x%3==0)return false;
for(int i=5;i*i<=x;i+=6)if(x%i==0||x%(i+2)==0)return false;
return true;
}
void SOLVE(int _){
read(n,d);k=0;
while(n%d==0)++k,n/=d;
if(k<2)write("NO\n");
else if(!is_prime(n))write("YES\n");
else if(is_prime(d))write("NO\n");
else write(k>(d==n*n)+2?"YES\n":"NO\n");
}
/*
Input:
Output:
Outline:
*/
int main(){
#ifdef DEBUG
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
#endif
#ifdef more_text
read(T);
#endif
for(int i=1;i<=T;++i)SOLVE(i);
#ifdef DEBUG
fclose(stdin);fclose(stdout);
#endif
return 0;
}

浙公网安备 33010602011771号