All submissions for this problem are available.

Read problems statements in Mandarin Chinese and Russian.

This problem's statement is really a short one.

You are given an integer S. Consider an infinite sequence S, 2S, 3S, ... . Find the first number in this sequence that can be represented as Q3, where Q is some positive integer number. As the sought number could be very large, please print modulo (109 + 7).

The number S will be given to you as a product of N positive integer numbers A1, A2, ..., AN, namely S = A1 * A2 * ... * AN

Input

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.

The first line of each test case contains an integer N.

Then there is a line, containing N space separated integers, denoting the numbers A1, A2, ..., AN.

Output

For each test case, output a single line containing the first term of the sequence which is the perfect cube, modulo 109+7.

Constraints

  • 1T10
  • (Subtask 1): N = 1, 1S109 - 15 points.
  • (Subtask 2): N = 1, 1S1018 - 31 point.
  • (Subtask 3): 1N100, 1Ai1018 - 54 points.

Example

  1. Input:
  2. 2
  3. 2
  4. 2 2
  5. 2
  6. 2 3
  7. Output:
  8. 8
  9. 216

Explanation

Example case 1. First few numbers in the infinite sequence 4, 8, 12, 16, 20, , etc. In this sequence, 8 is the first number which is also a cube (as 23 = 8).

Example case 2. First few numbers in the infinite sequence 6, 12, 18, 24, , etc. In this sequence, 216 is the first number which is also a cube (as 63 = 216).

【分析】

挺模板的东西,就当复习一下了。

  1. /*
  2. 宋代李冠
  3. 《蝶恋花·春暮》
  4. 遥夜亭皋闲信步。
  5. 才过清明,渐觉伤春暮。
  6. 数点雨声风约住。朦胧淡月云来去。
  7. 桃杏依稀香暗渡。
  8. 谁在秋千,笑里轻轻语。
  9. 一寸相思千万绪。人间没个安排处。
  10. */
  11. #include <cstdio>
  12. #include <cstring>
  13. #include <algorithm>
  14. #include <cmath>
  15. #include <queue>
  16. #include <vector>
  17. #include <iostream>
  18. #include <string>
  19. #include <ctime>
  20. #include <map>
  21. #define LOCAL
  22. const int MAXN = + ;
  23. const long long MOD = ;
  24. const double Pi = acos(-1.0);
  25. long long G = ;//原根
  26. const int MAXM = * + ;
  27. using namespace std;
  28. typedef long long ll;
  29. ll read(){
  30. ll flag = , x = ;
  31. char ch;
  32. ch = getchar();
  33. while (ch < '' || ch > '') {if (ch == '-') flag = -; ch = getchar();}
  34. while (ch >= '' && ch <= '') {x = x * + (ch - ''); ch = getchar();}
  35. return x * flag;
  36. }
  37. map<ll, int>Num;//记录个数
  38. ll data[MAXN];
  39. ll n;
  40.  
  41. ll mul(ll a, ll b, ll c){//又要用快速乘QAQ
  42. if (b == ) return 0ll;
  43. if (b == ) return a % c;
  44. ll tmp = mul(a, b / , c);
  45. if (b % == ) return (tmp + tmp) % c;
  46. else return (((tmp + tmp) % c) + (a % c)) % c;
  47. }
  48. ll pow(ll a, ll b, ll c){
  49. if (b == ) return 1ll;
  50. if (b == ) return a % c;
  51. ll tmp = pow(a, b / , c);
  52. if (b % == ) return mul(tmp, tmp, c);
  53. else return mul(mul(tmp, tmp, c), a, c);
  54. }
  55. bool Sec_check(ll a, ll b, ll c){
  56. ll tmp = pow(a, b, c);
  57. if (tmp != && tmp != (c - )) return ;
  58. if (tmp == (c - ) || (b % != )) return ;
  59. return Sec_check(a, b / , c);
  60. }
  61. //判断n是否是素数
  62. bool miller_rabin(ll n){
  63. int cnt = ;
  64. while (cnt--){
  65. ll a = (ll)rand() % (n - ) + ;
  66. if (!Sec_check(a, n - , n)) return ;
  67. }
  68. return ;
  69. }
  70. ll gcd(ll a, ll b){return b == 0ll ? a : gcd(b, a % b);}
  71. ll pollard_rho(ll a, ll c){
  72. ll i = , k = ;
  73. ll x, y, d;
  74. x = (ll)((double)(rand() / RAND_MAX) * (a - ) + 0.5) + 1ll;
  75. y = x;
  76. while (){
  77. i++;
  78. x = (mul(x, x, a) % a + c) % a;
  79. d = gcd(y - x + a, a);
  80. if (d > && d < a) return d;
  81. if (y == x) return a;//失败
  82. if (i == k){
  83. k <<= ;
  84. y = x;
  85. }
  86. }
  87. }
  88. void find(ll a, ll c){
  89. if (a == ) return;
  90. if (miller_rabin(a)){
  91. Num[a]++;
  92. return;
  93. }
  94. ll p = a;
  95. while (p >= a) pollard_rho(a, c--);
  96. pollard_rho(p, c);
  97. pollard_rho(a / p, c);
  98. }
  99. void init(){
  100. Num.clear();
  101. scanf("%d", &n);
  102. for (int i = ; i <= n; i++) {
  103. data[i] = read();
  104. find(data[i], );
  105. }
  106. }
  107. void work(){
  108. ll Ans = ;
  109. for (int i = ; i <= n; i++) Ans = (Ans * data[i]) % MOD;
  110. for (map<ll, int>::iterator it = Num.begin(); it != Num.end(); it++){
  111. it->second %= ;
  112. if (it->second){
  113. for (int i = it->second; i < ; i++) Ans = (Ans * ((it->first) % MOD)) % MOD;
  114. }
  115. }
  116. printf("%lld\n", Ans);
  117. }
  118.  
  119. int main(){
  120. int T;
  121.  
  122. scanf("%d", &T);
  123. while (T--){
  124. init();
  125. work();
  126. }
  127. return ;
  128. }

【CODECHEF】【phollard rho + miller_rabin】The First Cube的更多相关文章

  1. 最短路(模板)【CodeChef CLIQUED,洛谷P3371】

    自TG滚粗后咕咕咕了这么久,最近重新开始学OI,也会慢慢开始更博了.... 最短路算法经典的就是SPFA(Bellman-Ford),Dijkstra,Floyd: 本期先讲两个经典的单源最短路算法: ...

  2. 【CodeChef】Querying on a Grid(分治,最短路)

    [CodeChef]Querying on a Grid(分治,最短路) 题面 Vjudge CodeChef 题解 考虑分治处理这个问题,每次取一个\(mid\),对于\(mid\)上的三个点构建最 ...

  3. 【CodeChef】Palindromeness(回文树)

    [CodeChef]Palindromeness(回文树) 题面 Vjudge CodeChef 中文版题面 题解 构建回文树,现在的问题就是要求出当前回文串节点的长度的一半的那个回文串所代表的节点 ...

  4. HDU1164_Eddy&#39;s research I【Miller Rabin素数测试】【Pollar Rho整数分解】

    Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. 【CodeChef】Find a special connected block - CONNECT(斯坦纳树)

    [CodeChef]Find a special connected block - CONNECT(斯坦纳树) 题面 Vjudge 题解 还是一样的套路题,把每个数字映射到\([0,K)\)的整数, ...

  6. 【OpenCV入门教程之十四】OpenCV霍夫变换:霍夫线变换,霍夫圆变换合辑

    http://blog.csdn.net/poem_qianmo/article/details/26977557 本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog ...

  7. 【OpenCV新手教程之十四】OpenCV霍夫变换:霍夫线变换,霍夫圆变换合辑

    本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/26977557 作者:毛星云(浅墨) ...

  8. 【Knockout.js 学习体验之旅】(3)模板绑定

    本文是[Knockout.js 学习体验之旅]系列文章的第3篇,所有demo均基于目前knockout.js的最新版本(3.4.0).小茄才识有限,文中若有不当之处,还望大家指出. 目录: [Knoc ...

  9. 【Knockout.js 学习体验之旅】(2)花式捆绑

    本文是[Knockout.js 学习体验之旅]系列文章的第2篇,所有demo均基于目前knockout.js的最新版本(3.4.0).小茄才识有限,文中若有不当之处,还望大家指出. 目录: [Knoc ...

随机推荐

  1. POJ2229 - Sumsets(完全背包)

    题目大意 给定一个数N,问由不同的2的幂之和能组成N的方法有多少种 题解 看完题目立马想到完全背包...敲完代码上去超时了....后来发现是%的原因...改成减法就A了...%也太他妈耗时了吧!!!( ...

  2. ASP.NET--ListBox初始化时设置多个选中项

    public void SetSelectedListItem(ListBox lst, List<DBServerIPBind> source) { ; i < source.Co ...

  3. linux文件权限查看及修改-chmod

    查看linux文件的权限:ls -l 文件名称 查看linux文件夹的权限:ls -ld 文件夹名称(所在目录) 修改文件及文件夹权限: sudo chmod -(代表类型)×××(所有者)×××(组 ...

  4. Modelsim初级使用教程

    来源 http://blog.sina.com.cn/s/blog_6c7b6f030101ctlh.html 一. Modelsim简介 Modelsim仿真工具是Model公司开发的.它支持Ver ...

  5. 让BOOTSTRAP默认SLIDER支持触屏设备

    var isTouch=('ontouchstart' in window); if(isTouch){ $(".carousel").on('touchstart', funct ...

  6. 【JAVA - SSM】之MyBatis逆向工程的使用

    MyBatis逆向工程可以方便的从数据库中将表自动映射到JAVA POJO类,并同时生成Mapper.xml和Mapper接口,方便实用.下面介绍一下逆向工程的使用方法. 使用逆向工程,我们最好是新建 ...

  7. 三目运算符 改变<a>标签的class属性

    <s:iterator value="funcList" status="status" id="bean"> <a id ...

  8. lucene创建索引简单示例

    利用空闲时间写了一个使用lucene创建索引简单示例, 1.使用maven创建的项目 2.需要用到的jar如下: 废话不多说,直接贴代码如下: 1.创建索引的类(HelloLucene): packa ...

  9. 2假动作,数据缓冲,CCEaseExponential,CCEaseElastic,CCEaseBounce,CCCallFunc,funcNCallBack,funcNDCallBack,funcO

     1 缓冲动作 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/4 ...

  10. 文件和目录之chdir、fchdir和getcwd函数

    每个进程都有一个当前工作目录,此目录是搜索所有相对路径名的起点(不以斜杠开始的路径名为相对路径名).当用户登录到UNIX系统时,其当前工作目录通常是口令文件(/etc/passwd)中该用户登录项的第 ...