栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 3964 Factorial Simplifica...

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 3964 Factorial Simplifica...

#include<cstring>#include<cstdio>#include<algorithm>using namespace std;const int maxn =10010;int prime[maxn+10],len;int mk[maxn+10];void getprime(){ len =0; memset(mk,0,sizeof(mk)); for(int i=2;i<=maxn;i++){ int sign =0; for(int j=1;j<=len&′[j]*prime[j]<=i;j++)if(i%prime[j]==0){ sign =1; break; } if(sign==0){ mk[i]=1; prime[++len]=i; } }}int calcpow(int x,int y){ if(x==0) return 0; else return calcpow(x/y,y)+x/y;}int pow[maxn];const int inf = 0x3fffffff;int calc(int be,int ×){ int low = 1,high=be; while(low<high){ int mid=(low+high+1)/2; int mk=0,nowtimes=inf; for(int i=1;i<=len&′[i]<=mid;i++){ int tmp = pow[i]/calcpow(mid,prime[i]); if(tmp ==0){ mk=1; break; }else nowtimes = min(tmp,nowtimes); } if(mk==1) high=mid-1; else{ low = mid; times = nowtimes; } } for(int i=1;i<len;i++) pow[i]-=calcpow(low,prime[i])*times; return low;}int n,m;int main(){ getprime(); scanf("%d %d",&n,&m); for(int i=1;i<=n;i++){ int x; scanf("%d",&x); for(int j=1;j<=len&′[j]<=x;j++){ pow[j]+=calcpow(x,prime[j]); } } for(int i=1;i<=m;i++){ int x; scanf("%d",&x); for(int j=1;j<=len&′[j]<=x;j++){ pow[j]-=calcpow(x,prime[j]); } } int ok=1; for(int i=0;i<=len&&oki++) if(pow[i]<0){ ok=0;} if(!ok) puts("-1"); else { int be =10006; int fine =0; int times =0; int cnt=0; int ans[maxn],t[maxn]; int tmp; while((tmp=calc(be,times))!=1){ fine =1; ans[++cnt]=tmp; t[cnt]=times; be = tmp-1; } printf("%dn",cnt); for(int i=1;i<=cnt;i++) printf("%d %dn",ans[i],t[i]); } return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/366118.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号