SPOJ - NUMTRYE

测试了Miller_Rabin和pollard_rho

题目里这个g(n)是有结论的,但我搞不太懂

最后f(n)/g(n) = (p1 + 1)(p2 + 1)...(pk + 1)

  1 #include <cstdio>
  2 #include <time.h>
  3 #include <algorithm>
  4 #include <cstring>
  5 #define MOD 1000000007
  6 using namespace std;
  7 typedef long long LL;
  8
  9 const int S = 8;
 10
 11 LL mul(LL a, LL b, LL c) {
 12     a %= c;
 13     b %= c;
 14     LL res = 0;
 15     while (b) {
 16         if (b & 1) {
 17             res += a;
 18             if (res > c) res -= c;
 19         }
 20         a <<= 1;
 21         if (a > c) a -= c;
 22         b >>= 1;
 23     }
 24     return res;
 25 }
 26
 27 LL QP(LL x, LL n, LL p) {
 28     LL res = 1;
 29     x %= p;
 30     while (n) {
 31         if (n & 1) res = mul(res, x, p);
 32         x = mul(x, x, p);
 33         n >>= 1;
 34     }
 35     return res;
 36 }
 37
 38 bool ck(LL a, LL n, LL x, LL t) {
 39     LL res = QP(a, x, n);
 40     LL last = res;
 41     for (int i = 1; i <= t; i++) {
 42         res = mul(res, res, n);
 43         if (res == 1 && last != 1 && last != n - 1) return true;
 44         last = res;
 45     }
 46     if (res != 1) return true;
 47     else return false;
 48 }
 49
 50 bool Miller_Rabin(LL n) {
 51     if (n < 2) return false;
 52     if (n == 2) return true;
 53     if ( (n & 1) == 0) return false;
 54     LL x = n - 1;
 55     LL t = 0;
 56     while ((x & 1) == 0) {x >>= 1; t++;}
 57     srand(time(NULL));
 58     for (int i = 0; i < S; i++) {
 59         LL a = rand() % (n - 1) + 1;
 60         if (ck(a, n, x, t)) return false;
 61     }
 62     return true;
 63 }
 64
 65 LL fact[100];
 66 int tol;
 67
 68 LL gcd(LL a, LL b) {
 69     LL t;
 70     while (b) {
 71         t = a;
 72         a = b;
 73         b = t % b;
 74     }
 75     if (a >= 0) return a;
 76     else return -a;
 77 }
 78
 79
 80 LL pollard_rho(LL x, LL c) {
 81     LL i = 1, k = 2;
 82     srand(time(NULL));
 83     LL x0 = rand() % (x - 1) + 1;
 84     LL y = x0;
 85     while (1) {
 86         i++;
 87         x0 = (mul(x0, x0, x) + c) % x;
 88         LL d = gcd(y - x0, x);
 89         if (d != 1 && d != x) return d;
 90         if (y == x0) return x;
 91         if (i == k) {y = x0; k += k;}
 92     }
 93 }
 94
 95 void findfac(LL n, int k) {
 96     if (n == 1) return;
 97     if (Miller_Rabin(n)) {
 98         fact[tol++] = n;
 99         return;
100     }
101     LL p = n;
102     int c = k;
103     while (p >= n) p = pollard_rho(p, c--);
104     findfac(p, k);
105     findfac(n / p, k);
106 }
107
108 int main() {
109
110     int T;
111     LL n;
112     scanf("%d", &T);
113     while (T--) {
114         scanf("%lld", &n);
115         if (Miller_Rabin(n)) {
116             tol = 1; fact[0] = n;
117         } else {
118             tol = 0;
119             findfac(n, 107);
120         }
121         sort(fact, fact + tol);
122         tol = unique(fact, fact + tol) - fact;
123         LL ans = 1;
124         for (int i = 0; i < tol; i++) {
125             ans = ans * (fact[i] + 1) % MOD;
126         }
127         printf("%lld\n", ans);
128     }
129
130
131     return 0;
132 }

原文地址:https://www.cnblogs.com/xFANx/p/9477922.html

时间: 2024-08-08 15:09:17

SPOJ - NUMTRYE的相关文章

SPOJ 705 Distinct Substrings(后缀数组)

[题目链接] http://www.spoj.com/problems/SUBST1/ [题目大意] 给出一个串,求出不相同的子串的个数. [题解] 对原串做一遍后缀数组,按照后缀的名次进行遍历, 每个后缀对答案的贡献为n-sa[i]+1-h[i], 因为排名相邻的后缀一定是公共前缀最长的, 那么就可以有效地通过LCP去除重复计算的子串. [代码] #include <cstdio> #include <cstring> #include <algorithm> usi

SPOJ 3273

传送门: 这是一道treap的模板题,不要问我为什么一直在写模板题 依旧只放代码 1 //SPOJ 3273 2 //by Cydiater 3 //2016.8.31 4 #include <iostream> 5 #include <cstring> 6 #include <ctime> 7 #include <cmath> 8 #include <cstdlib> 9 #include <string> 10 #include

SPOJ CRAN02 - Roommate Agreement

题目链接:http://www.spoj.com/problems/CRAN02/ 题目大意:N个数字组成的序列,和为0的连续子序列的个数.N<1e6 解题思路:计算前缀和,统计每个数字出现的次数,那么对于数字sum[i], 如果存在k个sum[i],则代表有C(k, 2)个序列和为0,而如果sum[i] = 0,则还要累加上对应的k值. 代码: 1 ll n; 2 int a[maxn]; 3 ll sum[maxn]; 4 map<int, int> mmp; 5 6 void so

spoj GCJ1C09C Bribe the Prisoners

题目链接: http://www.spoj.com/problems/GCJ1C09C/ 题意: In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. Cells number i and i+1 are adjacent, and prisoners in adjacent cells are called "neighbours." A wall wi

SPOJ QTREE Query on a tree ——树链剖分 线段树

[题目分析] 垃圾vjudge又挂了. 树链剖分裸题. 垃圾spoj,交了好几次,基本没改动却过了. [代码](自带常数,是别人的2倍左右) #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 20005 int T,n,fr[maxn],h[maxn],to[maxn],ne[maxn]

BZOJ 2588: Spoj 10628. Count on a tree 主席树+lca

2588: Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个询问的u是明文. Input 第一行两个整数N,M. 第二行有N个整数,其中第i个整数表示点i的权值. 后面N-1行每行两个整数(x,y),表示点x到点y有一条边. 最后M行每行两个整数(u,v,k),表示一组询问.

BZOJ 1002 + SPOJ 104 基尔霍夫矩阵 + 一个递推式。

BZOJ 1002 高精度 + 递推 f[1] = 1; f[2] = 5; f[i] = f[i - 1] * 3 - f[i - 2] + 2; SPOJ 104 裸 + 不用Mod 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <algorithm> 5 #include <iostream> 6 7 using namespace std;

SPOJ QTREE 系列解题报告

题目一 : SPOJ 375 Query On a Tree http://www.spoj.com/problems/QTREE/ 给一个树,求a,b路径上最大边权,或者修改a,b边权为t. 1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 7 using namespace s

BZOJ 2226: [Spoj 5971] LCMSum( 数论 )

∑lcm(i,n) = ∑ i*n/(i,n) = ∑d|n∑(x,n)=d x*n/d = ∑d|n∑(t,n/d)=1t*n = n∑d|nf(d). f(d)表示1~d中与d互质的数的和, 即f(d) = d*φ(d)/2(d>=2). 然后O(n)筛φ, 每次询问暴力算即可...最大是100w,sqrt(100w)=1000内的质数是168个, 所以复杂度是O(n + T*168), 可以AC  ----------------------------------------------