洛谷P1069 细胞分裂 数学
洛谷P1069 细胞分裂
数学
质因数分解
题意 求一个最小的 自然数 x 使 s[ i ]^x 任意一个s[ i ] 能够整除以 m1^m2 只要能够整除以就行
题解 这题就是分解质因数 但是 分解s[ i ] 就太大了,我们只要分解 m1 就行了 ,因为m1比较小,分解完之后指数乘 m2就行
然后看s[ i ] 是否含有 m1的质因子 ,有就分解,如果没有这个质因子,说明一定不行,直接退出,有的话就是
m1 中含有的这个因子的个数 除以 s[ i ] 中含有的这个因子的个数,然后向上取整
PS m1==1 时特判一下, 0 步到位
还有分解质因数如果质数就会剩下一个数 ,这个数的个数也要乘以 m2
1 #include <cstdio> 2 #include <cmath> 3 #include <cstdlib> 4 #include <cstring> 5 #include <string> 6 #include <algorithm> 7 #include <iostream> 8 #include <iomanip> 9 using namespace std ; 10 11 struct node{ 12 int val,num,oth ; 13 }; 14 const int inf = 2e9 ; 15 int m1,m2,n,tot,x,mi,mx,k,kk ; 16 node s[1001] ; 17 18 inline int read() 19 { 20 char ch = getchar() ; 21 int x = 0,f = 1 ; 22 while(ch<'0'||ch>'9') { if(ch=='-') f = -1 ; ch = getchar() ; } 23 while(ch>='0'&&ch<='9' ) { x = x*10 + ch - 48 ; ch = getchar() ; } 24 return x*f ; 25 } 26 27 int main() 28 { 29 n = read() ; 30 m1 = read() ; m2 = read() ; 31 tot = 0 ; 32 if (m1==1) {cout<<0<<endl;return 0;} /// 33 for(int i=2,zz = int(sqrt(m1)) ;i<=zz;i++ ) 34 { 35 if(m1%i==0) 36 { 37 s[++tot].val = i ; 38 while(m1%i==0) s[tot].num++,m1/=i ; 39 s[tot].num*=m2 ; 40 } 41 } 42 if(m1!=1) s[++tot].val = m1,s[tot].num = 1*m2 ; 43 44 45 mi = inf ; 46 for(int i=1;i<=n;i++) 47 { 48 x = read() ; 49 mx = 0 ; 50 for(int j=1;j<=tot;j++ ) 51 { 52 s[ j ].oth = 0 ; 53 while(x%s[ j ].val==0) s[ j ].oth++,x/=s[ j ].val ; 54 if(s[ j ].oth==0) { 55 mx = inf ; 56 break ; 57 } 58 mx = max(mx,( s[ j ].num-1 ) /s[ j ].oth +1 ) ; 59 } 60 if(mx < mi ) 61 { 62 mi = mx ; 63 k = i ; kk = x; 64 } 65 } 66 //printf("%d %d\n",k,kk) ; 67 if(mi != inf ) 68 printf("%d\n",mi) ; 69 else 70 printf("-1\n") ; 71 return 0 ; 72 }