当前位置: 首页 > 编程日记 > 正文

POJ 2429

思路:a/n*b/n=lcm/gcd 所以这道题就是分解ans.dfs枚举每种素数情况。套Miller_Rabin和pollard_rho模板

  1 //#pragma comment(linker, "/STACK:167772160")//手动扩栈~~~~hdu 用c++交
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cstdlib>
  5 #include<iostream>
  6 #include<queue>
  7 #include<stack>
  8 #include<cmath>
  9 #include<set>
 10 #include<algorithm>
 11 #include<vector>
 12 // #include<malloc.h>
 13 using namespace std;
 14 #define clc(a,b) memset(a,b,sizeof(a))
 15 #define inf  (LL)1<<61
 16 #define LL long long
 17 const double eps = 1e-5;
 18 const double pi = acos(-1);
 19 // inline int r(){
 20 //     int x=0,f=1;char ch=getchar();
 21 //     while(ch>'9'||ch<'0'){if(ch=='-') f=-1;ch=getchar();}
 22 //     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
 23 //     return x*f;
 24 // }
 25 const int Times = 10;
 26 const int N = 5500;
 27 
 28 LL n, m, ct, cnt;
 29 LL minn, mina, minb, ans;
 30 LL fac[N], num[N];
 31 
 32 LL gcd(LL a, LL b)
 33 {
 34     return b? gcd(b, a % b) : a;
 35 }
 36 
 37 LL multi(LL a, LL b, LL m)
 38 {
 39     LL ans = 0;
 40     a %= m;
 41     while(b)
 42     {
 43         if(b & 1)
 44         {
 45             ans = (ans + a) % m;
 46             b--;
 47         }
 48         b >>= 1;
 49         a = (a + a) % m;
 50     }
 51     return ans;
 52 }
 53 
 54 LL quick_mod(LL a, LL b, LL m)
 55 {
 56     LL ans = 1;
 57     a %= m;
 58     while(b)
 59     {
 60         if(b & 1)
 61         {
 62             ans = multi(ans, a, m);
 63             b--;
 64         }
 65         b >>= 1;
 66         a = multi(a, a, m);
 67     }
 68     return ans;
 69 }
 70 
 71 bool Miller_Rabin(LL n)
 72 {
 73     if(n == 2) return true;
 74     if(n < 2 || !(n & 1)) return false;
 75     LL m = n - 1;
 76     int k = 0;
 77     while((m & 1) == 0)
 78     {
 79         k++;
 80         m >>= 1;
 81     }
 82     for(int i=0; i<Times; i++)
 83     {
 84         LL a = rand() % (n - 1) + 1;
 85         LL x = quick_mod(a, m, n);
 86         LL y = 0;
 87         for(int j=0; j<k; j++)
 88         {
 89             y = multi(x, x, n);
 90             if(y == 1 && x != 1 && x != n - 1) return false;
 91             x = y;
 92         }
 93         if(y != 1) return false;
 94     }
 95     return true;
 96 }
 97 
 98 LL pollard_rho(LL n, LL c)
 99 {
100     LL i = 1, k = 2;
101     LL x = rand() % (n - 1) + 1;
102     LL y = x;
103     while(true)
104     {
105         i++;
106         x = (multi(x, x, n) + c) % n;
107         LL d = gcd((y - x + n) % n, n);
108         if(1 < d && d < n) return d;
109         if(y == x) return n;
110         if(i == k)
111         {
112             y = x;
113             k <<= 1;
114         }
115     }
116 }
117 
118 void find(LL n, int c)
119 {
120     if(n == 1) return;
121     if(Miller_Rabin(n))
122     {
123         fac[ct++] = n;
124         return ;
125     }
126     LL p = n;
127     LL k = c;
128     while(p >= n) p = pollard_rho(p, c--);
129     find(p, k);
130     find(n / p, k);
131 }
132 
133 void dfs(LL dept, LL tem=1)
134 {
135     if(dept == cnt)
136     {
137         LL a = tem;
138         LL b = ans / a;
139         if(gcd(a, b) == 1)
140         {
141             a *= n;
142             b *= n;
143             if(a + b < minn)
144             {
145                 minn = a + b;
146                 mina = a;
147                 minb = b;
148             }
149         }
150         return ;
151     }
152     for(int i=0; i<=num[dept]; i++)
153     {
154         if(tem > minn) return;
155         dfs(dept + 1, tem);
156         tem *= fac[dept];
157     }
158 }
159 
160 int main()
161 {
162     while(~scanf("%llu %llu", &n, &m))
163     {
164         if(n == m)
165         {
166             printf("%llu %llu\n",n,m);
167             continue;
168         }
169         minn = inf;
170         ct = cnt = 0;
171         ans = m / n;
172         find(ans, 120);
173         sort(fac, fac + ct);
174         num[0] = 1;
175         int k = 1;
176         for(int i=1; i<ct; i++)
177         {
178             if(fac[i] == fac[i-1])
179                 ++num[k-1];
180             else
181             {
182                 num[k] = 1;
183                 fac[k++] = fac[i];
184             }
185         }
186         cnt = k;
187         dfs(0, 1);
188         if(mina > minb) swap(mina, minb);
189         printf("%llu %llu\n",mina, minb);
190     }
191     return 0;
192 }
View Code

转载于:https://www.cnblogs.com/ITUPC/p/5465872.html

相关文章:

iOS中你可能没有完全弄清楚的(二)自己实现一个KVO源码及解析

前几天写了一篇blog&#xff08;点这里&#xff09;&#xff0c;分析了系统KVO可能的实现方式。并添加了简单代码验证。 既然系统KVO不好用&#xff0c;我们完全可以根据之前的思路&#xff0c;再造一个可以在项目中使用的KVO的轮子。 代码已经上传到github: https://github.…

js中的preventDefault与stopPropagation详解

1. preventDefault: 比如<a href"http://www.baidu.com">百度</a>,这是html中最基础的东西&#xff0c;起的作用就是点击百度链接到http://www.baidu.com,这是属于<a>标签的默认行为;preventDefault方法就是可以阻止它的默认行为的发生而发生其他…

angular过滤字符_如何使用Angular和Azure计算机视觉创建光学字符读取器

angular过滤字符介绍 (Introduction) In this article, we will create an optical character recognition (OCR) application using Angular and the Azure Computer Vision Cognitive Service. 在本文中&#xff0c;我们将使用Angular和Azure计算机视觉认知服务创建一个光学字…

javascript函数全解

0.0 概述 本文总结了js中函数相关的大部分用法&#xff0c;对函数用法不是特别清晰的同学可以了解一下。 1.0 简介 同其他语言不同的是&#xff0c;js中的函数有2种含义。 普通函数&#xff1a;同其他语言的函数一样&#xff0c;是用于封装语句块&#xff0c;执行多行语句的…

MYSQL explain详解[转载]

explain显示了mysql如何使用索引来处理select语句以及连接表。可以帮助选择更好的索引和写出更优化的查询语句。 虽然这篇文章我写的很长&#xff0c;但看起来真的不会困啊&#xff0c;真的都是干货啊&#xff01;&#xff01;&#xff01;&#xff01; 先解析一条sql语句&…

CodeForces 157A Game Outcome

A. Game Outcometime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputSherlock Holmes and Dr. Watson played some game on a checkered board n  n in size. During the game they put numbers on the boards squares…

我使用Python和Django在自己的网站上建立了一个会员专区。 这是我学到的东西。

I decided it was time to upgrade my personal website in order to allow visitors to buy and access my courses through a new portal. 我认为是时候升级我的个人网站了&#xff0c;以允许访问者通过新的门户购买和访问我的课程 。 Specifically, I wanted a place for v…

详解AFNetworking的HTTPS模块

0.0 简述 文章内容包括&#xff1a; AFNetworking简介ATS和HTTPS介绍AF中的证书验证介绍如何创建服务端和客户端自签名证书如何创建简单的https服务器对CA正式证书和自签名证书的各种情况进行代码验证 文中所涉及的文件和脚本代码请看这里。 1.0 AFNetworking简介 AFNetwo…

字符串专题:map POJ 1002

第一次用到是在‘校内赛总结’扫地那道题里面&#xff0c;大同小异 map<string,int>str 可以专用做做字符串的匹配之类的处理 string donser; str [donser] 自动存donser到map并且值加一&#xff0c;如果发现重复元素不新建直接加一&#xff0c; map第一个参数是key&…

【洛谷P1508】吃吃吃

题目背景 问世间&#xff0c;青春期为何物&#xff1f; 答曰&#xff1a;“甲亢&#xff0c;甲亢&#xff0c;再甲亢&#xff1b;挨饿&#xff0c;挨饿&#xff0c;再挨饿&#xff01;” 题目描述 正处在某一特定时期之中的李大水牛由于消化系统比较发达&#xff0c;最近一直处…

前端和后端开发人员比例_前端开发人员vs后端开发人员–实践中的定义和含义

前端和后端开发人员比例Websites and applications are complex! Buttons and images are just the tip of the iceberg. With this kind of complexity, you need people to manage it, but which parts are the front end developers and back end developers responsible fo…

Linux 创建子进程执行任务

Linux 操作系统紧紧依赖进程创建来满足用户的需求。例如&#xff0c;只要用户输入一条命令&#xff0c;shell 进程就创建一个新进程&#xff0c;新进程运行 shell 的另一个拷贝并执行用户输入的命令。Linux 系统中通过 fork/vfork 系统调用来创建新进程。本文将介绍如何使用 fo…

metasploit-smb扫描获取系统信息

1.msfconsle 2.use auxiliary/scanner/smb/smb_version 3. msf auxiliary(smb_version) > set RHOSTS 172.16.62.1-200RHOSTS > 172.16.62.1-200msf auxiliary(smb_version) > set THREADS 100THREADS > 100msf auxiliary(smb_version) > run 4.扫描结果&#x…

算法(1)斐波那契数列

1.0 问题描述 实现斐波那契数列&#xff0c;求第N项的值 2.0 问题分析 斐波那契数列最简单的方法是使用递归&#xff0c;递归和查表法同时使用&#xff0c;可以降低复杂度。根据数列特点&#xff0c;同时进行计算的数值其实只有3个&#xff0c;所以可以使用3个变量循环递进计…

主键SQL教程–如何在数据库中定义主键

Every great story starts with an identity crisis. Luke, the great Jedi Master, begins unsure - "Who am I?" - and how could I be anyone important? It takes Yoda, the one with the Force, to teach him how to harness his powers.每个伟大的故事都始于…

算法(2)KMP算法

1.0 问题描述 实现KMP算法查找字符串。 2.0 问题分析 “KMP算法”是对字符串查找“简单算法”的优化。字符串查找“简单算法”是源字符串每个字符分别使用匹配串进行匹配&#xff0c;一旦失配&#xff0c;模式串下标归0&#xff0c;源字符串下标加1。可以很容易计算字符串查…

告别无止境的增删改查:Java代码生成器

对于一个比较大的业务系统&#xff0c;我们总是无止境的增加&#xff0c;删除&#xff0c;修改&#xff0c;粘贴&#xff0c;复制&#xff0c;想想总让人产生一种抗拒的心里。那有什么办法可以在正常的开发进度下自动生成一些类&#xff0c;配置文件&#xff0c;或者接口呢&…

Maven国内源设置 - OSChina国内源失效了,别更新了

Maven国内源设置 - OSChina国内源失效了&#xff0c;别更新了 原文&#xff1a;http://blog.csdn.net/chwshuang/article/details/52198932 最近在写一个Spring4.x SpringMVCMybatis零配置的文章&#xff0c;使用的源配的是公司的私有仓库&#xff0c;但是为了让其他人能够通过…

如何使用Next.js创建动态的Rick and Morty Wiki Web App

Building web apps with dynamic APIs and server side rendering are a way to give people a great experience both with content and speed. How can we use Next.js to easily build those apps?使用动态API和服务器端渲染来构建Web应用程序是一种使人们在内容和速度上都…

安装部署Spark 1.x Standalone模式集群

Configuration spark-env.sh HADOOP_CONF_DIR/opt/data02/hadoop-2.6.0-cdh5.4.0/etc/hadoop JAVA_HOME/opt/modules/jdk1.7.0_67 SCALA_HOME/opt/modules/scala-2.10.4 ####################################################### #主节点 …

算法(3)简单四则运算

1.0 问题描述 实现10以内四则运算&#xff08;只包含数字&#xff0c;*/和小括号&#xff09; 2.0 问题分析 四则运算使用“后缀表达式”算法来计算&#xff0c;后缀表达式可以无需考虑运算符优先级&#xff0c;直接从左至右依次计算。问题分解成2部分&#xff0c;一是将“中…

调用短信接口,先var_dump()看数据类型是object需要json_decode(json_encode( $resp),true)转换成array...

返回的数据.先看类型,如果是object类型 先json_encode, 再json_decode,加true 转换成数组 $resp $c->execute($req); var_dump($resp); object(stdClass)#12 (2) { ["result"]> object(stdClass)#13 (3) { ["err_code"]> string(1) "0"…

nlp文本数据增强_如何使用Texthero为您的NLP项目准备基于文本的数据集

nlp文本数据增强Natural Language Processing (NLP) is one of the most important fields of study and research in today’s world. It has many applications in the business sector such as chatbots, sentiment analysis, and document classification.Preprocessing an…

R语言-基础解析

二、操作基础%%取余%/%整数除法(1)eigen(...)求解方阵的特征值和特征向量(2)solve(D,A)求解DXA(3)data<-list(...)取里面的对象data[["列名称"]]&#xff1b;data[[下标]]&#xff1b;data$列名称(4)unlist(列表对象)把列表对象转化为向量对象(5)names(数据框)读取…

算法(4)数据结构:堆

1.0 问题描述 实现数据结构&#xff1a;堆。 2.0 问题分析 堆一般使用数组来表示&#xff0c;其中某个节点下标i的两个子节点的下标为 2i1 和 2i2。堆是一棵完全二叉树。堆有3种基本操作&#xff1a;创建&#xff0c;插入&#xff0c;删除。这3种操作都需要通过“调整堆”的…

cookie 和session 的区别详解

转自 https://www.cnblogs.com/shiyangxt/archive/2008/10/07/1305506.html 这些都是基础知识&#xff0c;不过有必要做深入了解。先简单介绍一下。 二者的定义&#xff1a; 当你在浏览网站的时候&#xff0c;WEB 服务器会先送一小小资料放在你的计算机上&#xff0c;Cookie 会…

如何设置Java Spring Boot JWT授权和认证

In the past month, I had a chance to implement JWT auth for a side project. I have previously worked with JWT in Ruby on Rails, but this was my first time in Spring. 在过去的一个月中&#xff0c;我有机会为辅助项目实现JWT auth。 我以前曾在Ruby on Rails中使用…

算法(5)哈希表

1.0 问题描述 实现数据结构&#xff1a;哈希表。 2.0 问题分析 哈希表可以看作我们经常使用的字典&#xff08;swift&#xff09;或对象&#xff08;js&#xff09;&#xff0c;可以让一个key&value对一一对应&#xff0c;可以快速根据key找到value。哈希表内部使用数组…

《面向对象程序设计》c++第五次作业___calculator plus plus

c第五次作业 Calculator plusplus 代码传送门 PS:这次作业仍然orz感谢一位同学与一位学长的windows帮助&#xff0c;同时再次吐槽作业对Mac系统用户的不友好。&#xff08;没朋友千万别用Mac&#xff01;&#xff01;&#xff01;&#xff09; 还有想吐槽作业对规范的要求大大超…

联合体union和大小端(big-endian、little-endian)

1.联合体union的基本特性——和struct的同与不同union&#xff0c;中文名“联合体、共用体”&#xff0c;在某种程度上类似结构体struct的一种数据结构&#xff0c;共用体(union)和结构体(struct)同样可以包含很多种数据类型和变量。在成员完全相同的情况下&#xff0c;struct比…