分析:怎么看都是超时,但是可以先筛一遍1e6以内的每个数的最小素数

算出每个数由多少个素数组成,然后应用,c[1e6][20]

就是题解的那一套,参照题解,比赛的时候没有想到好的办法筛一个数的因子,醉了

然后赛后发现,预处理因子肯定超时,虽然是O(nlogn)的,但是n是1e6啊,常数太大

而且单组操作只有5e4,所以暴力sqrt(x)即可

  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <cstdio>
  5. #include <cmath>
  6. #include <cstring>
  7. #include <algorithm>
  8. using namespace std;
  9. const int N = 1e6+;
  10. int c[N][],tot,prime[],mn[N],cnt[N],q,kase;
  11. bool check[N];
  12. char op[];
  13. void getprime()
  14. {
  15. for(int i=; i<=N-; ++i)
  16. {
  17. if(!check[i])mn[i]=prime[++tot]=i;
  18. for(int j=; j<=tot; ++j)
  19. {
  20. if(i*prime[j]>N-)break;
  21. check[i*prime[j]]=true;
  22. mn[i*prime[j]]=prime[j];
  23. if(i%prime[j]==)break;
  24. }
  25. }
  26. }
  27. void getcnt()
  28. {
  29. for(int i=; i<=N-; ++i)
  30. {
  31. int tmp=i;
  32. while(tmp!=)++cnt[i],tmp/=mn[tmp];
  33. }
  34. }
  35. int main()
  36. {
  37. getprime();
  38. getcnt();
  39. memset(mn,,sizeof(mn));
  40. while(~scanf("%d",&q),q)
  41. {
  42. printf("Case #%d:\n",++kase);
  43. memset(c,,sizeof(c));
  44. int ttt=;
  45. for(int i=; i<q; ++i)
  46. {
  47. int x;
  48. scanf("%s%d",op,&x);
  49. if(op[]=='I')
  50. {
  51. if(mn[x]==kase)continue;
  52. mn[x]=kase;++ttt;
  53. for(int j=; j*j<=x; ++j)
  54. {
  55. if(x%j)continue;
  56. ++c[j][cnt[x/j]];
  57. if(x/j!=j)++c[x/j][cnt[j]];
  58. }
  59. }
  60. else if(op[]=='D')
  61. {
  62. if(mn[x]!=kase)continue;
  63. mn[x]=;--ttt;
  64. for(int j=; j*j<=x; ++j)
  65. {
  66. if(x%j)continue;
  67. --c[j][cnt[x/j]];
  68. if(x/j!=j)--c[x/j][cnt[j]];
  69. }
  70. }
  71. else
  72. {
  73. if(ttt==){printf("-1\n");continue;}
  74. int ans=;
  75. for(int j=; j*j<=x; ++j)
  76. {
  77. if(x%j)continue;
  78. for(int k=; k<=; ++k)
  79. {
  80. if(c[j][k])
  81. {
  82. ans=min(ans,k+cnt[x/j]);
  83. break;
  84. }
  85. }
  86. if(x/j!=j)
  87. {
  88. for(int k=; k<=; ++k)
  89. {
  90. if(c[x/j][k])
  91. {
  92. ans=min(ans,k+cnt[j]);
  93. break;
  94. }
  95. }
  96. }
  97. }
  98. if(ans==)ans=-;
  99. printf("%d\n",ans);
  100. }
  101. }
  102. }
  103. return ;
  104. }

HDU5812 Distance 构造,预处理的更多相关文章

  1. HDU5812 Distance(枚举 + 分解因子)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5812 Description In number theory, a prime is a ...

  2. LeetCode---String

    Count and Say 思路:递归求出n - 1时的字符串,然后双指针算出每个字符的次数,拼接在结果后面 public String countAndSay(int n) { if(n == 1) ...

  3. java,大数据批量插入、更新

    public void exec(Connection conn){ try { conn.setAutoCommit(false); Long beginTime = System.currentT ...

  4. CodeForces 631C Print Check

    排序+构造+预处理 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm ...

  5. 用Java向数据库中插入大量数据时的优化

    使用jdbc向数据库插入100000条记录,分别使用statement,PreparedStatement,及PreparedStatement+批处理3种方式进行测试: public void ex ...

  6. 【霍夫曼树】poj 1339 poker card game (数组排序+辅助队列的方法,预处理O(nlogn),构造霍夫曼树O(n))

    poj.org/problem?id=1339 #include<iostream> #include<cstdio> #include<string> #incl ...

  7. Distance Dependent Infinite Latent Feature Model 阅读笔记1

    阅读文献:Distance Dependent Infinite Latent Feature Model 作者:Samuel J.Gershman ,Peter I.Frazier ,and Dav ...

  8. Codeforces CF#628 Education 8 C. Bear and String Distance

    C. Bear and String Distance time limit per test 1 second memory limit per test 256 megabytes input s ...

  9. CF #296 (Div. 1) B. Clique Problem 贪心(构造)

    B. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

随机推荐

  1. linux驱动模型<输入子系统>

    在linux中提供一种输入子系统的驱动模型,其主要是实现在input.c中. 在输入子系统这套模型中,他把驱动分层分类.首先分为上下两层,上层为input.c .下层为驱动的实现,下层分为两部分,一部 ...

  2. linux read和write函数

    原文出处:http://blog.chinaunix.net/space.php?uid=20558494&do=blog&id=2803003read函数是Linux下不带缓存的文件 ...

  3. 【USACO】【section1.1】Your Ride Is Here

    以前的账号忘记了,只能从头了. 入门题不解释,就是sumg和sumc初始值置1不能置0.开始享用一个循环计算出sumg和sumc,其实两个数组最大程度为6,节省不了什么时间. /*ID:Moment1 ...

  4. Linux内核同步机制之(三):memory barrier【转】

    转自:http://www.wowotech.net/kernel_synchronization/memory-barrier.html 一.前言 我记得以前上学的时候大家经常说的一个词汇叫做所见即 ...

  5. CMD下查看某个端口被谁占用了

    cmd运行 netstat -aon|findstr "8080" 运行结果如下 TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 6416 TCP [:: ...

  6. 路由器扫描的Java源码

    这个源码不是本人写的,是我原来的领导写的,我们都叫他东哥,这个是东留给我的一个小资源,好佩服他哦,这个东西可以用来扫描全世界的路由器,破解路由器账户和密码 当然是简单的了.我能力不够没有更完善的补充下 ...

  7. 2.cadence制板流程[原创]

    1.元器件库(原理图库) 2.原理图 3.DRC检查 4.输出网表 5.PCB封装 6.板子边框 7.导入网表 8.设置约束规则 9.布局,布线,铺铜 10.DRC检查,出丝印,钻孔,出广汇

  8. ZOJ Problem Set - 3861 Valid Pattern Lock(dfs)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3861 这道题当时没做出来,后来经过队友提醒才做出来. 3*3的九宫格,给你 ...

  9. Vector示例一,二

    #include <iostream> int main(void) { double a[] = {1, 2, 3, 4, 5}; std::cout<<mean(a, 5) ...

  10. laravel named route

    laravel中一般对于路由的使用方法是在routes.php中定义一个路由,在view中如果要引用一个url则直接通过<a href="url/">来使用. 但是随着 ...