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进行素因子分解,再 ...
随机推荐
- 【原创】纯OO:从设计到编码写一个FlappyBird (六)
第五部分请看这里 终于到了最后一个部分了! 这里使用SimpleJudge类来实现Judge接口. 首先是SimpleJudge需要的实例变量: 0.final LinkedList<Pilla ...
- oracle表空间查询维护命令大全之三(暂时表空间)史上最全
--UNDO表空间汇总 --查看全部的表空间名字 SELECT NAME FROM V$TABLESPACE; --创建新的UNDO表空间,并设置自己主动扩展參数; CREATE UNDO TABLE ...
- Spring.net-业务层仓储
Spring.net-业务层仓储 本系列目录:ASP.NET MVC4入门到精通系列目录汇总 上一节,我们已经把项目框架的雏形搭建好了,那么现在我来开始业务实现,在业务实现的过程当中,不断的来完善我们 ...
- java提高篇(五)-----使用序列化实现对象的拷贝
我们知道在Java中存在这个接口Cloneable,实现该接口的类都会具备被拷贝的能力,同时拷贝是在内存中进行,在性能方面比我们直接通过new生成对象来的快,特别是在大对象的生成上,使得性 ...
- TCP连接建立过程中为什么需要“三次握手”(转)
传输控制协议(Transmission Control Protocol, TCP)是一种面向连接的.可靠的.基于字节流的运输层(Transport layer)通信协议.是专门为了在不可靠的互联网络 ...
- CodeForces 22D Segments 排序水问题
主题链接:点击打开链接 升序右键点.采取正确的点 删边暴力 #include <cstdio> #include <cstring> #include <algorith ...
- Extjs Web Desktop申请书
今天我Web Desktop应用基本完成.多语言支持.现有asp,php,jsp版本号. 废话拍了几张照片让大家有一个直观的了解: watermark/2/text/aHR0cDovL2Jsb2cuY ...
- SQL SERVER2005事务日志已满 解决方法
DUMP TRANSACTION 数据库名称 WITH NO_LOG alter database 数据库名称 set recovery simple 3.右键你要压缩的数据库--所有任务--收缩数据 ...
- Python 基金会 —— 模块和包简介
一.模块(Module) 1.模块的作用 在交互模式下输出的变量和函数定义,一旦终端重新启动后,这些定义就都不存在了,为了持久保存这些变量.函数等的定义,Python中引入了模块(Modul ...
- css两种动态显示星星等级的比较(一星、两星、三星、四星、五星)
原文:css两种动态显示星星等级的比较(一星.两星.三星.四星.五星) 以下是显示后的图片,相信在很多网站上都能看到这种效果,目前我知道两种实现方式 1.background-position加上一张 ...