Light OJ 1341 Aladdin and the Flying Carpet Pollard_rho整数分解+DFS
进入a b 多少努力p, q 使p*q == a && p < q && p >= b
直接大整数分解 然后dfs所有可能的解决方案劫持
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#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;
}
LL 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;
}
LL 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;
}
bool 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;
}
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;
}
}
}
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);
} 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);
sort(factor, factor+l);
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]++;
}
//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;
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
Light OJ 1341 Aladdin and the Flying Carpet Pollard_rho整数分解+DFS的更多相关文章
- LightOJ 1341 Aladdin and the Flying Carpet【整数分解】
题目链接: http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1341 题意: 给定一个数,将其拆分成两个数的乘 ...
- Light OJ 1341 Aladdin and the Flying Carpet
题意:求大于b的a的因数对有几组.例10 2结果为{2,5},12 2结果为{2,6}{3,4}-----不反复 解一:分解质因数+DFS #include <iostream> #in ...
- LightOJ 1341 Aladdin and the Flying Carpet(整数拆分定理)
分析:题目并不难理解,就是一些细节上的优化需要我们注意,我在没有优化前跑了2000多MS,优化了一些细节后就是400多MS了,之前还TLE了好几次. 方法:将整数拆分为质因子以后,表达为这样的形式,e ...
- LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000 ...
- 1341 - Aladdin and the Flying Carpet ---light oj (唯一分解定理+素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1341 题目大意: 给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. ...
- LOJ 1341 Aladdin and the Flying Carpet(质因子分解)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1341 题意:给两个数a,b,求满足c * d = a且c>=b且d>=b的 ...
- [LightOJ 1341] Aladdin and the Flying Carpet (算数基本定理(唯一分解定理))
题目链接: https://vjudge.net/problem/LightOJ-1341 题目描述: 问有几种边长为整数的矩形面积等于a,且矩形的短边不小于b 算数基本定理的知识点:https:// ...
- LightOJ 1341 Aladdin and the Flying Carpet(唯一分解定理)
http://lightoj.com/volume_showproblem.php?problem=1341 题意:给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. 思路 ...
- LightOJ 1341 - Aladdin and the Flying Carpet 基本因子分解
http://www.lightoj.com/volume_showproblem.php?problem=1341 题意:给你长方形的面积a,边最小为b,问有几种情况. 思路:对a进行素因子分解,再 ...
随机推荐
- 成都传智职工high翻竞赛场
日前,由石羊街道总工会.天府新谷园区党委联合主办的“2013年职工趣味竞赛”盛大开幕.传智播客成都java培训中心员工积极参与,活跃在各大项目的比赛中,员工们用笑脸.身影告诉大家:竞赛场上,我们hig ...
- swift排序算法和数据结构
var arrayNumber: [Int] = [2, 4, 6, 7, 3, 8, 1] //冒泡排序 func maopao(var array: [Int]) -> [Int] { fo ...
- port与大全portClose方法
在网络技术,port(Port)通常,有两种含义:首先,物理意义port,例,ADSL Modem.枢纽.开关.路由器连接其他网络设备的接口,如RJ-45port.SCport等等.第二个是逻辑意义p ...
- OCP解决问题052-- DROP PROFILE app_user
133.You created a profile APP_USER and assigned it to the users. After a month, you decide to drop t ...
- [Android]Can't create handler inside thread that has not called Looper.prepare()
更新是由于在新的线程来打开UI只有一个错误.子线程更新主线程UI需要使用Handler. 还有比如今天出现以下错误.码,如以下: send.setOnClickListener(new OnClick ...
- Hibernate-----5、持久化对象
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGVrZXdhbmd6aQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- 行政歌节 · 萧谱1
4之前听 陈越 的<绿野仙踪> 版权声明:本文博客原创文章,博客,未经同意,不得转载.
- MVC下判断用户登录和授权状态方法
MVC下判断用户登录和授权状态方法 在我们日常开发的绝大多数系统中,都涉及到管理用户的登录和授权问题.登录功能(Authentication),针对于所有用户都开放:而授权(Authorization ...
- 具体的了解“>/dev/null 2>&1”
Linux系统中不管是crontab里面.还是平时使用的命令.常常会碰到">/dev/null 2>&1".比方说:在Crontab Job里面,假设不想发送邮 ...
- IOS-QQ登陆之苹果程序流程
1.新建项目,通过main函数循环执行代码,直到应用被关闭. 2.点击项目,建立storyboard文件,并在info文件夹中指定第一个storyboard文件 3.建立Controller文件. 组 ...