HDU 5812 Distance 暴力+素数打表
2017-07-16 21:21
369 查看
/* I insert x; D delect x; Q query min(d(x,y); d(x,y) =x 乘或除一个素数 变成y的最小次数; d(x,y) =ans[x/gcd(x,y)]+ans[y/gcd(x,y]; ans[i] i的素因子数; 保留之前插入 y时,每一个y因子对应的ans[y/i]值 ->因子,ans值较小, 作为查询表的key 统计满足条件的y个数 ->C[i][ans[y/i] 查询暴力每个因子,如果有gcd时,显然最小。 */ #include<iostream> #include<algorithm> #include<cstdio> #include<cmath> #include<cstring> #include<set> using namespace std; const int Max=1000000+1; int ans[Max]={0}; int C[Max][21]; void init() { for(int i=2;i<=1000000;i++) { if(!ans[i]) { ans[i]=1; for(int j=i+i;j<=1000000;j +=i) ans[j]=ans[j/i]+1; } } } int main() { int n,x,cas=0; init(); char q[2]; set<int> s; while(scanf("%d",&n)&&n) { printf("Case #%d:\n",++cas); s.clear(); memset(C,0,sizeof(C)); for(int i=1;i<=n;i++) { scanf("%s%d",q,&x); if(q[0]=='I') { if(s.count(x)) continue; else { s.insert(x); for(int i=1;i<=sqrt(x)+1;i++) { if(x%i==0) { C[i][ans[x/i]]++; C[x/i][ans[i]]++; } } } } if(q[0]=='D') { if(!s.count(x)) continue; else { s.erase(x); for(int i=1;i<=sqrt(x)+1;i++) { if(x%i==0) { C[i][ans[x/i]]--; C[x/i][ans[i]]--; } } } } if(q[0]=='Q') { int Min=1234; if(s.size()==0) { printf("-1\n"); continue; } for(int i=1;i<=sqrt(x)+1;i++) { if(x%i==0) { int j; for(j=0;j<=20;j++) if(C[i][j]) { Min=min(Min,ans[x/i]+j); break; } for(j=0;j<=20;j++) if(C[x/i][j]) { Min=min(Min,ans[i]+j); break; } } } printf("%d\n",Min); } } } return 0; }
相关文章推荐
- Hdu 5812 Distance(三种操作. 1.插入x 2.删除x 3.在集合中找到一个数y,使得y->x,每次能除以一个素数或者乘上一个素数,问最少的操作次数)
- hdu 1431 素数回文(暴力打表,埃托色尼筛法)
- hdu 5104 筛素数打表+暴力枚举
- HDU 1239 Calling Extraterrestrial Intelligence Again 【打表+暴力搜索】
- http://acm.hdu.edu.cn/showproblem.php?pid=2303 (素数打表 + java大数取模)
- 数论 - 筛法暴力打表 --- hdu : 12876 Quite Good Numbers
- HDU 4548 素数打表 + 线段树
- HDU 1005 Number Sequence【多解,暴力打表,鸽巢原理】
- hdu-2682Tree(prim+素数打表)
- hdu 1286 素数打表
- HDU 5778 abs (素数,暴力)
- HDU-1215-七夕节(Java+素数打表+超时坑人)
- HDU 5407 CRB and Candies (2015多校第10场第一题)素数打表,除法取模(乘法逆元)
- 2016 多校第一场 hdu 5276((线段树+暴力打表)|(RMQ + 二分))
- hdu 5879 -Cure 暴力打表
- HDU 2510 符号三角形 暴力打表
- HDU 4548 美素数 (素数打表+前缀和)
- HDU 5104 Primes Problem(素数打表)
- hdu 2098 素数打表
- hdu 2136 素数打表