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 ...
随机推荐
- IDEA下搭建简单的SpringBoot工程应用
(1)File->new,选择maven,创建一个空项目,直接next. (2)填写工程名,next. (3)填写项目名,next,创建一个基于maven的空Java项目. (4)在pom文件中 ...
- async-await系列翻译(一)
本篇翻译的英文链接:https://docs.microsoft.com/en-us/dotnet/articles/standard/async-in-depth 使用.NET的基于任务的异步编程模 ...
- 安装YouCompleteMe时,编译依赖的python版本不对
启动vim打开文件时出错: The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). YCM core library compile ...
- SPOJ:PATHETIC STRINGS(分配问题&贪心)
Problem statement: A string is said to be “PATHETIC” if all the characters in it are repeated the sa ...
- 非旋treap (BZOJ1895)
记个板子,还是挺好用的. #include <bits/stdc++.h> using namespace std; ]; int rt,n,m,l,r,x,A,B,C,t; struct ...
- Android进阶2之Activity之间数据交流(onActivityResult的用法) (转载)
转自:http://blog.csdn.net/sjf0115/article/details/7387467 主要功能: 在一个主界面(主Activity)上能连接往许多不同子功能模块(子Activ ...
- Codeforces Round #422 (Div. 2) C. Hacker, pack your bags!(更新数组)
传送门 题意 给出n个区间[l,r]及花费\(cost_i\),找两个区间满足 1.区间和为指定值x 2.花费最小 分析 先用vector记录(l,r,cost)和(r,l,cost),按l排序,再设 ...
- hdoj1027【STL系列。。。?】
这个太夸张了...感觉是有别的方法,但是觉得再说吧...以后碰到全排列应该也是用STL嗨的吧...嗯,,,就是这样的....?再说,再说.. 还有杭电支持c艹11,很棒 #include <bi ...
- Unity里的人物驱动/换装备/换武器/换衣服/卡通重定位(转)
Unity里的人物驱动/换装备/换武器/换衣服/动画重定位 刚学的过程被这个问题困扰最多. 首先,基本的,大家都知道驱动人物需要骨架.绑骨的Mesh和动画(这三个要是不知道的话就得考虑看看计算机图形学 ...
- CF487E Tourists【圆方树+tarjan+multiset+树剖+线段树】
圆方树不仅能解决仙人掌问题(虽然我仙人掌问题也没用过圆方树都是瞎搞过去的),还可以解决一般图的问题 一般图问题在于缩完环不是一棵树,所以就缩点双(包括双向边) 每个方点存他所在点双内除根以外的点的最小 ...