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进行素因子分解,再 ...
随机推荐
- ueditor问题解决
ueditor图片无法上传? 解决: imageUp.ashx 去掉这一行 <%@ Assembly Src="Uploader.cs" %> 参考: http://w ...
- java ClassLoader static
package init; class Person { private static Person person = new Person(); public static int count2 = ...
- 构造NFS
一.设备nfs-utils 伺服器: [root@server05 ftp]# yum install nfs-utils 这时会自己主动安装rpcbind需将此服务重新启动nfs服务才干启动 cli ...
- SecureCRT 6.7.1 RI和谐 皴 补丁 方法
它之前被使用SecureCRT 6.5.3 版本号,咋看和谐补丁,即使中国版本也可(现在才发现SecureCRT.6.2.0) 可是换为 6.7.1 后就怎么也注冊不了了.. 没办法试了各种办法: 先 ...
- BZOJ 1324 Exca神剑 最小割
标题效果:给定一个n*m矩阵.所有的格宝石之子,人们可选择起始位置,后除去宝石的当前位置的周围消失,然后你就可以走两步,重复上述过程 easy发现格儿子把它周围格孩子不能拿 因此,党格访问问题 黑白染 ...
- Spark 1.0.0版本发布
前言 如今Spark终于迈出了里程碑一步,1.0.0标记的版本号出版物Spark1.0时代.1.0.0版本号不仅增加了非常多新特性.而且提供了更好的API支持.Spark SQL作为一个新的组件增加. ...
- 怎样解决No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=i386).
怎样解决No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=i386). 错误 ...
- JS call与apply
JS的call与apply call和apply是JS中比较重要的两个方法, 一般在框架和组件设计中用的较多,比如jQuery Code. 那么这两个方法是做什么的呢,下面我们通过代码来了解: 1 f ...
- Zen Coding in Visual Studio 2012
http://www.johnpapa.net/zen-coding-in-visual-studio-2012 Zen Coding is a faster way to write HTML us ...
- cocos2dX 它CCScene创建原则和切换模式
今天, 让我们来看看现场CCScene创建原则和切换模式, 首先, 个什么样子: 我们先来看看效果: 啥也没有: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZX ...