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内核中工作队列的使用work_struct,delayed_work【转】
本文转载自:http://blog.csdn.net/zahuopuboss/article/details/43268983 初始化工作队列 调度工作队列 取消工作队列 #include <l ...
- iOS 生成随机数
arc4random 1.获取一个随机整数范围在:[0,100)包括0,不包括100 int x = arc4random() % 100; 2. 获取一个随机数范围在:[500,1000],包括5 ...
- CodeForces 1103C. Johnny Solving
题目简述:给定简单(无自环.无重边)连通无向图$G = (V, E), 1 \leq n = |V| \leq 2.5 \times 10^5, 1 \leq m = |E| \leq 5 \time ...
- iOS---UICollectionView Class Reference---UICollectionView 类参考文档
UICollectionView 类: Inherits from UIScrollView : UIView : UIResponder : NSObject Conforms to NSCodin ...
- C#中的Webservice实例代码(vs2013)
2.1首先创建一个最基本的web service服务端,顾名思义就是提供服务,这儿实现一个简单的加法计算. 首先,vs2013--文件---新建项目---Asp.net 空Web 应用程序 (V ...
- 如何实现session的共享?
1.以cookie加密的方式保存在客户端. 优点是减轻服务器端的压力 缺点是受到cookie的大小限制,可能占用一定带宽,因为每次请求会在头部附带一定大小的cookie信息,另外这种方式在用户禁止使用 ...
- Unity3D教程:换装方法
http://www.manew.com/4136.html 游戏内的角色,能够像纸娃娃换装那样子让玩家可以为自己的角色改变外观,一直是相当受欢迎的功能:一般而言,我们建好的 3D 模型,如果要将其中 ...
- 关于国债的一些计算: 理论TF价格1(缴款日前无付息)
计算 ExpectedTFPrice 是一个比较复杂的计算,我们这里讨论简单的一种情况. 给定一只可交割国债bond(一般为CTD),一个国债期货tf,一个日期t(表示tf的一个交易日期,我们通过t日 ...
- P4171 [JSOI2010]满汉全席(2-SAT)
传送门 2-SAT裸题 把每一道菜拆成两个点分别表示用汉式或满式 连边可以参考板子->这里 然后最尴尬的是我没发现$n<=100$然后化成整数的时候只考虑了$s[1]$结果炸掉了2333 ...
- WPS Office 2019 for Linux来了
难得啊,焕然一新. WPS Office 2019 For Linux更新说明 11.1.0.8392 版本主要更新: 修复wpsoffice进程存在时不能关机的问题 修复WPS文字模块web版式下拖 ...