51nod 1061 最复杂的数V2
题目链接
题面简述
求\([1, n]\)中约数个数最多的数。
\(n \le 10^{200}\)
题解
首先,答案一定是一个反素数。
什么是反素数?
一个正整数\(x\)是反素数的充要条件是:\([1, x - 1]\)中的整数的约数个数都小于\(x\)的约数个数。
反素数有什么性质?
把一个反素数分解成\(p_1^{a_1}p_2^{a_2}...p_n^{a_n}\)的形式,则\(a_1 \ge a_2 \ge ... \ge a_n\)。
除\(1\)以外,一个反素数\(x\)一定可以由一个小于\(x\)的反素数乘上一个质数得来。(证明大概比较显然?感性理解一下 = =)
如果我们能求出\(10^{200}\)以内所有反质数,询问时直接lower_bound即可。
由此设计出算法:
维护一个堆,一开始只放\(1\)进去;
每次取堆中最小的数(堆顶元素);
判断这是不是一个反素数:根据定义,如果比它小的反素数中有约数个数与它相等或更多的,则它不是反素数。维护当前已经出堆的反素数的约数个数的最大值,如果当前堆顶元素的约数个数不比它大,则它不是反素数。
枚举各个合法的素数(要求满足性质1)和它相乘,如果没有超出\(10^{200}\)则也压到堆中。
这样就能求出\(10^{200}\)以内所有反质数了(实际上只有3810个!)
这道题需要高精度,好在只需要高精度乘低精度即可。
AC代码
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define space putchar(' ')
#define enter putchar('\n')
using namespace std;
typedef long long ll;
template <class T>
void read(T &x){
char c;
bool op = 0;
while(c = getchar(), c < '0' || c > '9')
if(c == '-') op = 1;
x = c - '0';
while(c = getchar(), c >= '0' && c <= '9')
x = x * 10 + c - '0';
if(op) x = -x;
}
template <class T>
void write(T x){
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar('0' + x % 10);
}
const int N = 1000005, maxp = 100;
int T, prime[N], pcnt, tot;
bool np[N];
char s[256];
struct big {
ll n[256], l;
big(){
memset(n, 0, sizeof(n));
n[1] = l = 1;
}
void out(){
for(int i = l; i; i--)
write(n[i]);
}
bool operator < (const big &b) const {
if(l != b.l) return l < b.l;
for(int i = l; i; i--)
if(n[i] != b.n[i]) return n[i] < b.n[i];
return 0;
}
bool operator > (const big &b) const {
if(l != b.l) return l > b.l;
for(int i = l; i; i--)
if(n[i] != b.n[i]) return n[i] > b.n[i];
return 0;
}
big operator * (ll x) const {
big c;
c.l = l;
for(int i = 1; i <= l; i++)
c.n[i] = n[i] * x;
while(x) c.l++, x /= 10;
for(int i = 1; i <= c.l; i++)
c.n[i + 1] += c.n[i] / 10, c.n[i] %= 10;
while(!c.n[c.l]) c.l--;
return c;
}
} MAX, cur;
struct data{
big num;
int cnt[maxp];
data(){
memset(cnt, 0, sizeof(cnt));
}
data(big x){
num = x;
memset(cnt, 0, sizeof(cnt));
}
bool operator < (const data &b) const{
return num > b.num;
}
data operator * (int p) const{
data c;
c.num = num * prime[p];
memcpy(c.cnt, cnt, sizeof(cnt));
c.cnt[p]++;
return c;
}
} lst[4005];
priority_queue <data> que;
void euler(int n){
np[0] = np[1] = 1;
for(int i = 2; i <= n; i++){
if(!np[i]) prime[++pcnt] = i;
for(int j = 1; j <= pcnt && i * prime[j] <= n; j++){
np[i * prime[j]] = 1;
if(i % prime[j] == 0) break;
}
}
}
int main(){
euler(1000000);
MAX.l = 201;
MAX.n[201] = MAX.n[1] = 1;
cur.n[1] = 0;
que.push(data());
while(!que.empty()){
data u = que.top();
que.pop();
big d;
for(int p = 1; u.cnt[p]; p++)
d = d * (u.cnt[p] + 1);
if(!(cur < d)) continue;
lst[++tot] = u;
cur = d;
for(int p = 1; p < maxp && (p == 1 || u.cnt[p - 1]); p++)
if((p == 1 || u.cnt[p] < u.cnt[p - 1])){
data v = u * p;
if(v.num < MAX) que.push(v);
}
}
reverse(lst + 1, lst + tot + 1);
read(T);
while(T--){
scanf("%s", s + 1);
big n;
n.l = strlen(s + 1);
for(int i = 1; i <= n.l; i++)
n.n[n.l - i + 1] = s[i] - '0';
data v = data(n);
data u = *lower_bound(lst + 1, lst + tot + 1, v);
big d;
for(int p = 1; u.cnt[p]; p++)
d = d * (u.cnt[p] + 1);
u.num.out(), space, d.out(), enter;
}
return 0;
}
51nod 1061 最复杂的数V2的更多相关文章
- 【51nod】1061 最复杂的数 V2
题解 我是榜上最后一名= = 可能高精度用vector太慢了吧--什么破题= = 这道题很简单,如果高精度熟练代码--也很简单--然而,参数调了好久 我们发现质数的指数一定是,质数越小,指数越大,这个 ...
- 51Nod 1084:矩阵取数问题 V2(多维DP)
1084 矩阵取数问题 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 关注 一个M*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励 ...
- 51nod 1218 最长递增子序列 V2——LIS+思路(套路)
题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1218 自己怎么连这种 喜闻乐见的大水题 都做不出来了…… 好像见过 ...
- 51nod 1218 最长递增子序列 V2(dp + 思维)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1218 题解:先要确定这些点是不是属于最长递增序列然后再确定这 ...
- 51nod 1053 最大M子段和 V2
N个整数组成的序列a[1],a[2],a[3],…,a[n],将这N个数划分为互不相交的M个子段,并且这M个子段的和是最大的.如果M >= N个数中正数的个数,那么输出所有正数的和. 例如:-2 ...
- 51nod 1132 覆盖数字的数量 V2
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1132 题意是给定a,b,l,r求[l,r]内有几个整数可以表示成ax+b ...
- 51nod 1479 小Y的数论题
一脸不可做题~~~233333 T<=100000,所以一定要logn出解啦. 但是完全没有头绪*&#……%*&……()……#¥*#@ 题解: 因为2^p+2^p=2^(p+1) ...
- 51nod 1060 最复杂的数
把一个数的约数个数定义为该数的复杂程度,给出一个n,求1-n中复杂程度最高的那个数. 例如:12的约数为:1 2 3 4 6 12,共6个数,所以12的复杂程度是6.如果有多个数复杂度相等,输出最 ...
- 51nod 1060 最复杂的数 反素数
1060 最复杂的数 基准时间限制:1 秒 空间限制:131072 KB 把一个数的约数个数定义为该数的复杂程度,给出一个n,求1-n中复杂程度最高的那个数. 例如:12的约数为:1 2 3 4 6 ...
随机推荐
- (转)/etc/init.d/functions详解
转自:https://www.cnblogs.com/image-eye/archive/2011/10/26/2220405.html functions这个脚本是给/etc/init.d里边的文件 ...
- ASP.NET Core 登录失败。该登录名来自不受信任的域,不能与集成身份验证一起使用。
原文:ASP.NET Core 登录失败.该登录名来自不受信任的域,不能与集成身份验证一起使用. 当进行数据迁移的时候提示 修改appsettings配置连接串的Trusted_Connection ...
- git 提交新增文件到网站
git add -A 是将所有的修改都提交.你可以用git status查看当前的变化,然后通过git add xxx有选择的提交.git commit 是将变化先提交到本地.git commit - ...
- RHEL7基本命令
Terminal TTY TTY是TeleTYpe的一个老缩写. Teletypes,或者teletypewriters,原来指的是电传打字机,是通过串行线用打印机键盘通过阅读和发送信息的东西,和古老 ...
- ngx_lua 模块
ngx_lua模块的原理: 1.每个worker(工作进程)创建一个Lua VM,worker内所有协程共享VM:2.将Nginx I/O原语封装后注入 Lua VM,允许Lua代码直接访问:3.每个 ...
- springboot+websocket 归纳收集
websocket是h5后的技术,主要实现是一个长连接跟tomcat的comet技术差不多,但websocket是基于web协议的,有更广泛的支持.当然,在处理高并发的情况下,可以结合tomcat的a ...
- Ceph常规操作及常见问题梳理
Ceph集群管理 每次用命令启动.重启.停止Ceph守护进程(或整个集群)时,必须指定至少一个选项和一个命令,还可能要指定守护进程类型或具体例程. **命令格式如 {commandline} [opt ...
- 访谈:BugPhobia’s Brief Communication
0x01 :采访的学长简介 If you weeped for the missing sunset, you would miss all the shining stars 梁野,北京航空航天大学 ...
- 《面向对象程序设计》第三次作业 Calculator
c++第三次作业 Calculator git上的作业展示点这里. ps:有一点不是很明确,作业要求:将数字和符号提取出来,得到一组string,然后才将这些string存入队列中.按我的理解是需要将 ...
- PAT 1046 划拳
https://pintia.cn/problem-sets/994805260223102976/problems/994805277847568384 划拳是古老中国酒文化的一个有趣的组成部分.酒 ...