进入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的更多相关文章

  1. LightOJ 1341 Aladdin and the Flying Carpet【整数分解】

    题目链接: http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1341 题意: 给定一个数,将其拆分成两个数的乘 ...

  2. Light OJ 1341 Aladdin and the Flying Carpet

    题意:求大于b的a的因数对有几组.例10  2结果为{2,5},12 2结果为{2,6}{3,4}-----不反复 解一:分解质因数+DFS #include <iostream> #in ...

  3. LightOJ 1341 Aladdin and the Flying Carpet(整数拆分定理)

    分析:题目并不难理解,就是一些细节上的优化需要我们注意,我在没有优化前跑了2000多MS,优化了一些细节后就是400多MS了,之前还TLE了好几次. 方法:将整数拆分为质因子以后,表达为这样的形式,e ...

  4. LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000 ...

  5. 1341 - Aladdin and the Flying Carpet ---light oj (唯一分解定理+素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1341 题目大意: 给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. ...

  6. LOJ 1341 Aladdin and the Flying Carpet(质因子分解)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1341 题意:给两个数a,b,求满足c * d = a且c>=b且d>=b的 ...

  7. [LightOJ 1341] Aladdin and the Flying Carpet (算数基本定理(唯一分解定理))

    题目链接: https://vjudge.net/problem/LightOJ-1341 题目描述: 问有几种边长为整数的矩形面积等于a,且矩形的短边不小于b 算数基本定理的知识点:https:// ...

  8. LightOJ 1341 Aladdin and the Flying Carpet(唯一分解定理)

    http://lightoj.com/volume_showproblem.php?problem=1341 题意:给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. 思路 ...

  9. LightOJ 1341 - Aladdin and the Flying Carpet 基本因子分解

    http://www.lightoj.com/volume_showproblem.php?problem=1341 题意:给你长方形的面积a,边最小为b,问有几种情况. 思路:对a进行素因子分解,再 ...

随机推荐

  1. centos6.4设备hadoop-2.5.1(完全分布式)

    环境介绍: 在这两种装备centos6.4(32位置)的server安装Hadoop-2.5.1分布式集群(2台机器,主要试验用.哈哈). 1.改动主机名和/etc/hosts文件 1)改动主机名(非 ...

  2. .NET API for RabbitMQ and ActiveMQ

    EasyNetQ: .NET API for RabbitMQ: https://github.com/mikehadlow/EasyNetQ/wiki/Quick-Start Or: http:// ...

  3. 【v2.x OGE课程 14】 控制使用

    在这里,精灵.动画精灵.button天才.经常使用的文本的使用 一个.相关精灵 1.加入精灵 //创建精灵 Sprite bar_up = new Sprite(400, 0, RegionRes.g ...

  4. mtk硬件项目开始关闭蓝牙功能:mtk 硬件ScanCode和keycode应用演示示例

    项目要求:该项目因为没有使用android5.0,导致启动bluetooth的蓝牙audio slave功能必须使用第三方模组,该第三方模组,启动是通过android主板通过GPIO控制.UI界面是通 ...

  5. 《SAS编程和数据挖掘商业案例》学习笔记# 19

    继续<SAS编程与数据挖掘商业案例>学习笔记,本文側重数据处理实践.包含:HASH对象.自己定义format.以及功能强大的正則表達式 一:HASH对象 Hash对象又称散列表,是依据关键 ...

  6. [LeetCode258] Add Digits 非负整数各位相加

    题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...

  7. Linux X86在下面TLB机制

    TLB - translation lookaside buffer 快表.直译为翻译后备缓冲器,也能够理解为页表缓冲.地址变换快速缓存. 因为页表存放在主存中,因此程序每次訪存至少须要两次:一次訪存 ...

  8. 《Javascript权威指南》13号学习笔记:使用日期和时间

    一.创Date示例 1.Date类的方法和属性是非常不静,故,申请书Date属性和方法之前.必须创建Date类的实例. var date = new Date();  //以当前日期和时间创建实例. ...

  9. 汉字Collection

    只是上一行Demo private static string[] HanZis = new string[]{ "啊阿呵吖嗄腌锕爱矮挨哎碍癌艾唉哀蔼隘埃皑呆嗌嫒瑷暧捱砹嗳锿霭按安暗岸俺案鞍 ...

  10. android 原生应用、Web应用、混合应用优缺点分析

    近期开发几个项目,牵涉到android的几种开发模式.对于原生态开发.web 应用开发以及混合模式开发,本人觉得并非哪一种就是最好的,哪一种就是最差的,这个全然是依据项目的实际需求,选择一种合适的开发 ...