比赛 NOIP模拟赛by mzx Day2 评测结果 AWAWAWTTEE
题目名称 学姐的巧克力盒 最终得分 30
用户昵称 Mealy 运行时间 8.612 s
代码语言 C++ 内存使用 68.99 MiB
提交时间 2016-10-20 22:00:05
显示代码纯文本
  1. #include <iostream>
  2. #include <cstdio>
  3. #define u ree[tmpx]
  4. #define lc ree[tmpx<<1]
  5. #define rc ree[tmpx<<1^1]
  6. using namespace std;
  7. const int nmax=1000086;
  8. typedef long long ll;
  9. int n,m,k,FJ1,FJ2;
  10. int type,tmpl,tmpr;
  11. ll val[nmax];
  12. class SegmentTree
  13. {
  14. public:
  15. int l,r;
  16. ll pro;
  17. }ree[nmax*4];
  18.  
  19. ll QuickPow(ll base,ll exp)
  20. {
  21. ll ans=1;
  22. exp%=(FJ2-1);
  23. while(exp)
  24. {
  25. if(exp&1)
  26. {
  27. ans=(ans*base)%FJ2;
  28. }
  29. base=(base*base)%FJ2;
  30. exp>>=1;
  31. }
  32. return ans%FJ2;
  33. }
  34.  
  35.  
  36. void SetTree(int tmpx,int tmpl,int tmpr)
  37. {
  38. u.l=tmpl;
  39. u.r=tmpr;
  40. if(tmpl==tmpr)
  41. {
  42. u.pro=val[tmpl];
  43. }
  44. else
  45. {
  46. SetTree(tmpx<<1,tmpl,(tmpl+tmpr)>>1);
  47. SetTree(tmpx<<1^1,((tmpl+tmpr)>>1)+1,tmpr);
  48. u.pro=lc.pro*rc.pro%FJ1;
  49. }
  50. }
  51. ll QueryPro(int tmpx,int tmpl,int tmpr)
  52. {
  53. if(tmpl<=u.l&&tmpr>=u.r)
  54. {
  55. return u.pro%FJ1;
  56. }
  57. else
  58. {
  59. if(u.l!=u.r)
  60. {
  61. ll tmpans=1;
  62. if(tmpl<=lc.r)
  63. {
  64. tmpans=tmpans*QueryPro(tmpx<<1,tmpl,tmpr)%FJ1;
  65. }
  66. if(tmpr>=rc.l)
  67. {
  68. tmpans=tmpans*QueryPro(tmpx<<1^1,tmpl,tmpr)%FJ1;
  69. }
  70. return tmpans;
  71. }
  72. }
  73. }
  74. void PreDo()
  75. {
  76. scanf("%d%d%d%d%d",&n,&m,&k,&FJ1,&FJ2);
  77. for(int i=1;i<=n;i++)
  78. {
  79. scanf("%lld",&val[i]);
  80. }
  81. }
  82. int main()
  83. {
  84. freopen("chocolatebox.in","r",stdin);
  85. freopen("chocolatebox.out","w",stdout);
  86. PreDo();
  87. SetTree(1,1,n);
  88. for(int i=1;i<=m;i++)
  89. {
  90. scanf("%d%d%d",&type,&tmpl,&tmpr);
  91. if(type==1)
  92. {
  93. ll ans=QueryPro(1,tmpl,tmpr);
  94. printf("%lld",ans);
  95. }
  96. if(type==2)
  97. {
  98. int derta=tmpr-tmpl;
  99.  
  100. ll tmp=QueryPro(1,tmpl,tmpr);
  101. ll ans=QuickPow(k,tmp);
  102. printf("%lld",ans);
  103. }
  104. }
  105. return 0;
  106. }
  107.  
  108.