素因子分解超快的Pollard_rho算法
来源:互联网 发布:淘宝口令是什么意思啊 编辑:程序博客网 时间:2024/06/03 02:32
Description
It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery.
Aladdin was about to enter to a magical cave, led by the evil sorcerer who disguised himself as Aladdin's uncle, found a strange magical flying carpet at the entrance. There were some strange creatures guarding the entrance of the cave. Aladdin could run, but he knew that there was a high chance of getting caught. So, he decided to use the magical flying carpet. The carpet was rectangular shaped, but not square shaped. Aladdin took the carpet and with the help of it he passed the entrance.
Now you are given the area of the carpet and the length of the minimum possible side of the carpet, your task is to find how many types of carpets are possible. For example, the area of the carpet 12, and the minimum possible side of the carpet is 2, then there can be two types of carpets and their sides are: {2, 6} and {3, 4}.
Input
Input starts with an integer T (≤ 4000), denoting the number of test cases.
Each case starts with a line containing two integers: ab(1 ≤ b ≤ a ≤ 1012) where a denotes the area of the carpet and b denotes the minimum possible side of the carpet.
Output
For each case, print the case number and the number of possible carpets.
Sample Input
2
10 2
12 2
Sample Output
Case 1: 1
Case 2: 2
/**随机数素因子分解法大数素数判断*/#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <iostream>#include <cmath>using namespace std;typedef long long LL;const int Times = 25;LL factor[100], f[100];int l, ll, ans, num[100];LL a, b;LL gcd(LL a, LL b){return b ? gcd(b, a%b):a;}///(a*b)%nLL add_mod(LL a, LL b, LL n){ LL ans = 0; while(b) { if(b&1) ans = (ans + a)%n; b >>= 1; a = (a<<1)%n; } return ans;}///a^m%nLL pow_mod(LL a, LL m, LL n){ LL ans = 1; while(m) { if(m&1) ans = add_mod(ans, a, n); m >>= 1; a = add_mod(a, a, n); } return ans;}///以a为基,n-1=x*2^t a^(n-1)=1(mod n) 验证n是不是合数///一定是合数返回true,不一定返回falsebool Witness(LL a, LL n){ int j = 0; LL m = n-1; while(!(m&1)) { j++; m >>= 1; } LL x = pow_mod(a, m, n); if(x == 1 || x == n-1) return true; while(j--) { x = add_mod(x, x, n); if(x == n-1) return true; } return false;}///判定是否是素数bool Miller_Rabin(LL n){ if(n < 2) return false; if(n == 2) return true; if(!(n&1)) return false; for(int i = 0; i < Times; i++) { LL a = rand()%(n-1)+1; if(!Witness(a, n)) return false; } return true;}///关键算法 Pollard_rho 算法,求解素因子LL Pollard_rho(LL n, LL c){LL i = 1, x = rand()%(n-1)+1, y = x, k = 2, d;//srand(time(NULL));while(true){i++;x = (add_mod(x,x,n)+c)%n;d = gcd(y-x,n);if(d > 1 && d < n)return d;if(y == x)return n;if(i == k){y = x;k <<= 1;}}}///对n进行素因子分解void get_fact(LL n, LL k){if(n == 1)return;if(Miller_Rabin(n))///是素数{factor[l++] = n;return;}LL p = n;while(p >= n){p = Pollard_rho(p, k--);}get_fact(p, k);get_fact(n/p, k);}///得到指数num[]void get_num(){ sort(factor, factor+l);/**for(int i=0;i<l;i++) cout<<factor[i]<<" "; cout<<endl;*/f[0] = factor[0];num[0] = 1;ll = 1;for(int i = 1; i < l; i++){if(factor[i] != factor[i-1]){ll++;f[ll-1] = factor[i];num[ll-1] = 0;}num[ll-1]++;}}///搜索查找 适合的值void dfs(LL x, int p, LL m){if(x > m)return;if(p == ll){if(x >= b && a/x > x)ans++;//printf("%lld\n", x);return;}LL y = 1;for(int i = 0; i <= num[p]; i++){dfs(x*y, p+1, m);y *= f[p];}}int main(){int cas = 1;int T;scanf("%d", &T);while(T--){scanf("%lld %lld", &a, &b);LL m = sqrt(a+0.5);ans = 0;l = 0;get_fact(a, 120); get_num();//for(int i = 0; i < ll; i++)//printf("%lld %d\n", f[i], num[i]);dfs(1, 0, m);printf("Case %d: %d\n", cas++, ans);}return 0;}
- 素因子分解超快的Pollard_rho算法
- Pollard_rho算法模板(大整数的因子分解)
- miller_rabin素数判断和pollard_rho的素数因子分解算法
- Pollard_rho算法(大整数的因子分解或判断是否为素数)
- 大素数判断和素因子分解(miller-rabin,Pollard_rho算法)
- 大素数判断和素因子分解(miller-rabin,Pollard_rho算法)
- 大素数判断和素因子分解(miller-rabin,Pollard_rho算法)
- 大素数判断和素因子分解(miller-rabin,Pollard_rho算法)
- 大素数判断和素因子分解(miller-rabin,Pollard_rho算法)
- 大素数判断和素因子分解(miller-rabin,Pollard_rho算法)
- 大素数判断和素因子分解(miller-rabin,Pollard_rho算法)
- 大素数判断和素因子分解(miller-rabin,Pollard_rho算法)
- 大素数判断和素因子分解(miller-rabin,Pollard_rho算法)
- 大素数判断和素因子分解模板(miller-rabin,Pollard_rho算法)
- 大素数判断和素因子分解(miller-rabin,Pollard_rho算法)
- pollard_rho(大数质因子分解)
- POJ 2429 -- miller-rabin素数测试,Pollard_rho素因子分解
- HDU 3864 pollard_rho大数质因子分解
- NvidiaCUDA通用计算简介及优化方法概述
- [转]在Eclipse中使用JUnit4进行单元测试(中级篇)
- 【郑轻】[1748]Math One!
- 图形学优化_3: bridge(桥接模式)和proxy(代理)
- Canva绘制时钟
- 素因子分解超快的Pollard_rho算法
- 理解矩阵(二)
- 浙江理工大学校赛 JugHard 4216(gcd)
- [转]在Eclipse中使用JUnit4进行单元测试(高级篇)
- 2015浙江理工校赛G Jug Hard (数论orBFS)
- 素数环(Prime Ring Problem,UVa 524)
- 时区解析
- 音频技术之调节音量及设置静音
- Wamp配置pthreads extension