PKU_campus_2018_H Safe Upper Bound
思路:
题目链接http://poj.openjudge.cn/practice/C18H/
用2147483647除以最大素因子。
这里用了Pollard_rho因子分解算法,模板参考了http://www.cnblogs.com/kuangbin/archive/2012/08/19/2646396.html
实现:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
typedef long long ll;
//****************************************************************
// Miller_Rabin 算法进行素数测试
//速度快,而且可以判断 <2^63的数
//****************************************************************
const int S = ;//随机算法判定次数,S越大,判错概率越小 //计算 (a*b)%c. a,b都是ll的数,直接相乘可能溢出的
// a,b,c <2^63
ll mult_mod(ll a, ll b, ll c)
{
a %= c;
b %= c;
ll ret = ;
while (b)
{
if (b & ) { ret += a; ret %= c; }
a <<= ;
if (a >= c) a %= c;
b >>= ;
}
return ret;
} //计算 x^n %c
ll pow_mod(ll x, ll n, ll mod)//x^n%c
{
if (n == ) return x % mod;
x %= mod;
ll tmp = x;
ll ret = ;
while (n)
{
if (n & ) ret = mult_mod(ret, tmp, mod);
tmp = mult_mod(tmp, tmp, mod);
n >>= ;
}
return ret;
} //以a为基,n-1=x*2^t a^(n-1)=1(mod n) 验证n是不是合数
//一定是合数返回true,不一定返回false
bool check(ll a, ll n, ll x, ll t)
{
ll ret = pow_mod(a, x, n);
ll last = ret;
for (int i = ; i <= t; i++)
{
ret = mult_mod(ret, ret, n);
if (ret == && last != && last != n - ) return true;//合数
last = ret;
}
if (ret != ) return true;
return false;
} // Miller_Rabin()算法素数判定
//是素数返回true.(可能是伪素数,但概率极小)
//合数返回false; bool Miller_Rabin(ll n)
{
if (n < ) return false;
if (n == ) return true;
if ((n & ) == ) return false;//偶数
ll x = n - ;
ll t = ;
while ((x & ) == ) { x >>= ; t++; }
for (int i = ; i < S; i++)
{
ll a = rand() % (n - ) + ;//rand()需要stdlib.h头文件
if (check(a, n, x, t))
return false;//合数
}
return true;
} //************************************************
//pollard_rho 算法进行质因数分解
//************************************************
ll factor[];//质因数分解结果(刚返回时是无序的)
int tol;//质因数的个数。数组小标从0开始 ll gcd(ll a, ll b)
{
if (a == ) return ;
if (a < ) return gcd(-a, b);
while (b)
{
ll t = a % b;
a = b;
b = t;
}
return a;
} ll Pollard_rho(ll x, ll c)
{
ll i = , k = ;
ll x0 = rand() % x;
ll y = x0;
while ()
{
i++;
x0 = (mult_mod(x0, x0, x) + c) % x;
ll d = gcd(y - x0, x);
if (d != && d != x) return d;
if (y == x0) return x;
if (i == k) { y = x0; k += k; }
}
}
//对n进行素因子分解
void findfac(ll n)
{
if (Miller_Rabin(n))//素数
{
factor[tol++] = n;
return;
}
ll p = n;
while (p >= n) p = Pollard_rho(p, rand() % (n - ) + );
findfac(p);
findfac(n / p);
} int main()
{
srand(time(NULL));//需要time.h头文件//POJ上G++不能加这句话
ll n;
while (scanf("%lld", &n) != EOF && n)
{
tol = ;
findfac(n);
sort(factor, factor + tol);
ll ans = ((1ll << ) - ) / factor[tol - ];
printf("%lld\n", ans);
}
return ;
}
PKU_campus_2018_H Safe Upper Bound的更多相关文章
- PKU2018校赛 H题 Safe Upper Bound
http://poj.openjudge.cn/practice/C18H 题目 算平均数用到公式\[\bar{x}=\frac{x_1+x_2+x_3+\cdots+x_n}{n}\] 但如果用in ...
- 二分查找里的upper bound与lower bound的实现与分析
1. 问题引入 最近参选了学堂在线的课程数据结构(2015秋).课程由清华大学的邓俊辉老师主讲,在完成课后作业时,遇到了这样一个题目范围查询.在这个题目中,我需要解决这样一个子问题:给定了一组已经排好 ...
- my understanding of (lower bound,upper bound) binary search, in C++, thanks to two post 分类: leetcode 2015-08-01 14:35 113人阅读 评论(0) 收藏
If you understand the comments below, never will you make mistakes with binary search! thanks to A s ...
- poj 2566 Bound Found(尺取法 好题)
Description Signals of most probably extra-terrestrial origin have been received and digitalized by ...
- poj 2566 Bound Found
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4384 Accepted: 1377 Spe ...
- POJ2566 Bound Found 2017-05-25 20:05 32人阅读 评论(0) 收藏
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4056 Accepted: 1249 Spe ...
- POJ 2566 Bound Found 尺取 难度:1
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 1651 Accepted: 544 Spec ...
- POJ 2566 Bound Found(尺取法,前缀和)
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5207 Accepted: 1667 Spe ...
- Bound Found(思维+尺取)
Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronaut ...
随机推荐
- (linux)platform_driver_probe与platform_driver_register的区别
[驱动注册]platform_driver_register()与platform_device_register() 设备与驱动的两种绑定方式:在设备注册时进行绑定及在驱动注册 ...
- mpvue——实现点击数组内的某一元素进行置顶(排序第一)操作
前言 其实很简单只是用了js的几个函数 substr unshift splice 完整代码 | mpvue模仿QQ 代码 思路很简单,获取当前元素下标然后通过unshift函数将该值插入到数组第一位 ...
- 【转载】Android Studio简单设置
界面设置 默认的 Android Studio 为灰色界面,可以选择使用炫酷的黑色界面.Settings --> Appearance --> Theme ,选择 Darcula 主题即可 ...
- CollapsingToolbarLayout 收缩显示tilte
final CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id. ...
- Healthy Holsteins
链接 分析:因为数据范围比较小,我们可以通过二进制枚举子集,然后找出所需饲料种数最小的并记录下来,同时记录一下路径,也就是字典序最小的 /* PROB:holstein ID:wanghan LANG ...
- Spring中Bean获取IOC容器服务的方法
Spring 依赖注入可以让所有的Bean对其IOC容器的存在是没有意识的,甚至可以将容器换成其它的.但实际开发中如果某个Bean对象要用到Spring 容器本身的功能资源,需要意识到IOC容器的存在 ...
- View Controller Programming Guide for iOS---(六)---Responding to Display-Related Notifications
Responding to Display-Related Notifications 响应跟显示相关的通知 When the visibility of a view controller’s vi ...
- ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 06. Controller 返回View
Controller父类会提供很多上下文的相关信息,还提供了很多封装的方法 返回的对象要求实现了IActionResult接口 继承父类,并引入命名空间 写this点就出现很多东西,这些就是上下文的信 ...
- [WPF自定义控件库] 自定义控件的代码如何与ControlTemplate交互
1. 前言 WPF有一个灵活的UI框架,用户可以轻松地使用代码控制控件的外观.例设我需要一个控件在鼠标进入的时候背景变成蓝色,我可以用下面这段代码实现: protected override void ...
- Codeforces Round #331 (Div. 2)【未完待续】
http://codeforces.com/problemset/problem/596/B GGGGGGGGGGGGGGGGGGG