记录编号 48525 评测结果 AAAAAAAAAA
题目名称 整数合并 最终得分 100
用户昵称 GravatarTruth.Cirno 是否通过 通过
代码语言 C++ 运行时间 0.051 s
提交时间 2012-11-05 21:22:43 内存使用 4.13 MiB
显示代码纯文本
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <memory.h>
  5. #include <stack>
  6. using namespace std;
  7.  
  8. int dad[100010],siz[100010];
  9. bool nosu[100010];
  10. stack<int> sta;
  11.  
  12. int findroot(int ys)
  13. {
  14. while (dad[ys])
  15. {
  16. sta.push(ys);
  17. ys=dad[ys];
  18. }
  19. while (!sta.empty())
  20. {
  21. dad[sta.top()]=ys;
  22. sta.pop();
  23. }
  24. return(ys);
  25. }
  26.  
  27. void combine(int a,int b)
  28. {
  29. a=findroot(a);
  30. b=findroot(b);
  31. if (a==b)
  32. return;
  33. if (siz[a]>siz[b])
  34. {
  35. siz[a]+=siz[b];
  36. dad[b]=a;
  37. }
  38. else
  39. {
  40. siz[b]+=siz[a];
  41. dad[a]=b;
  42. }
  43. }
  44.  
  45. int main(void)
  46. {
  47. freopen("setb.in","r",stdin);
  48. freopen("setb.out","w",stdout);
  49. int i,j,a,b,p,total,temp;
  50. cin>>a>>b>>p;
  51. for (i=1;i<=b;i++)
  52. siz[i]=1;
  53. nosu[1]=true;
  54. for (i=2;i<=b;i++)
  55. {
  56. if (!nosu[i])
  57. {
  58. for (j=i<<1;j<=b;j+=i)
  59. {
  60. nosu[j]=true;
  61. if (i>=p)
  62. if (j>=a)
  63. combine(i,j);
  64. }
  65. }
  66. }
  67. total=0;
  68. memset(nosu,0,sizeof(nosu));//we can call "nosu" as "answer" now
  69. for (i=a;i<=b;i++)
  70. {
  71. temp=findroot(i);
  72. if (!nosu[temp])
  73. {
  74. total++;
  75. nosu[temp]=true;
  76. }
  77. }
  78. cout<<total<<endl;
  79. return(0);
  80. }