UVALive 6198 A Terribly Grimm Problem
题目大意是
给出L,H 10^10范围
为[L, H]这个连续的整数区间寻找一个序列。
序列的长度要跟[L, H]一样
然后序列中的数都是素数,并且互不相同
并且序列中第i个数 要求是L + i -1的一个素因子
最后要求序列的字典序最小
然后可以看到L,H很大
但是我们需要注意的是,这个序列长度肯定不会很大
太大了肯定满足不了题目的要求。
所以这个整数区间的数我们可以一个一个的,先把每个数都素因子分解了,放起来。
然后就发现。 这不就是二分图匹配么。
但是题目求的是字典序最小。
所以我们就对每个数。
对其所有的素因子,尝试改变匹配,然后寻找增广路。
如果能找到。就固定这条边
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#define MAXN 111111
#define N 505
using namespace std;
bool tag[MAXN];
int p[MAXN];
int cnt;
int mark[5555], used[5555];
int cx[N], cy[5555];
vector<int>g[N];
int ans[N];
int m, up;
long long l, r;
long long a[MAXN];
set<long long>s;
map<long long, int> id;
void getprime()
{
cnt = 0;
tag[1] = 1;
for(int i = 2; i < 100000; i++)
{
if(!tag[i]) p[cnt++] = i;
for(int j = 0; j < cnt && p[j] * i < 100000; j++)
{
tag[i * p[j]] = 1;
if(i % p[j] == 0) break;
}
}
}
void get(long long x)
{
for(int i = 0; i < cnt && x >= (long long)p[i] * (long long)p[i]; i++)
if(x % p[i] == 0)
{
s.insert(p[i]);
while(x % p[i] == 0) x /= p[i];
}
if(x != 1)
s.insert(x);
}
void get2(long long x)
{
long long tx = x;
for(int i = 0; i < cnt && x >= (long long)p[i] * (long long)p[i]; i++)
if(x % p[i] == 0)
{
long long tmp = (long long)p[i];
while(x % tmp == 0) x /= tmp;
g[tx - l + 1].push_back(id[tmp]);
}
if(x != 1)
g[tx - l + 1].push_back(id[x]);
}
int path(int u)
{
int sz = g[u].size();
for(int i = 0; i < sz; i++)
{
int v = g[u][i]; if(!mark[v] && !used[v])
{
mark[v] = 1;
if(cy[v] == -1 || path(cy[v]))
{
cx[u] = v;
cy[v] = u;
return 1;
}
}
}
return 0;
} bool ok(int t)
{
memset(cy, -1, sizeof(cy));
for(int i = t + 1; i <= up; i++)
{
memset(mark, 0, sizeof(mark));
if(!path(i)) return false;
}
return true;
}
void fix()
{
for(int i = 1; i <= up; i++)
{
int sz = g[i].size();
for(int j = 0; j < sz; j++)
{
int v = g[i][j];
if(used[v]) continue;
cx[i] = v;
used[v] = 1;
if(!ok(i)) used[v] = 0;
else break;
}
used[cx[i]] = 1;
}
}
void out(long long a )
{
if(a >= 10) out(a / 10);
putchar('0' + a % 10);
}
int main()
{
getprime(); while(scanf("%lld%lld", &l, &r) != EOF)
{
if(l == 0 && r == 0) break;
s.clear();
id.clear();
up = r - l + 1;
for(int i = 1; i <= up; i++) g[i].clear();
memset(ans, -1, sizeof(ans));
memset(used, 0, sizeof(used));
m = 0;
for(long long i = l; i <= r; i++)
get(i);
for(set<long long>::iterator it = s.begin(); it != s.end(); it++)
{
a[m++] = *it;
id[a[m - 1]] = m;
}
for(long long i = l; i <= r; i++)
get2(i);
fix();
for(int i = 1; i < up; i++)
{
//printf("%lld ", a[cx[i] - 1]);
out(a[cx[i] - 1]);
putchar(' ');
}
out(a[cx[up] - 1]);
putchar('\n');
//printf("%lld\n", a[cx[up] - 1]);
}
return 0;
}
UVALive 6198 A Terribly Grimm Problem的更多相关文章
- UVALive - 5107 - A hard Aoshu Problem
题目链接:https://vjudge.net/problem/UVALive-5107 题目大意:用ABCDE代表不同的数字,给出形如ABBDE___ABCCC = BDBDE的东西: 空格里面可以 ...
- UVaLive 7359 Sum Kind Of Problem (数学,水题)
题意:给定一个n,求前 n 个正整数,正奇数,正偶数之和. 析:没什么好说的,用前 n 项和公式即可. 代码如下: #pragma comment(linker, "/STACK:10240 ...
- A Boring Problem UVALive - 7676 (二项式定理+前缀和)
题目链接: I - A Boring Problem UVALive - 7676 题目大意:就是求给定的式子. 学习的网址:https://blog.csdn.net/weixin_37517391 ...
- Gym 101194D / UVALive 7900 - Ice Cream Tower - [二分+贪心][2016 EC-Final Problem D]
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...
- UVALive 7457 Discrete Logarithm Problem (暴力枚举)
Discrete Logarithm Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/D Description ...
- UVALive - 7041 The Problem to Slow Down You (回文树)
https://vjudge.net/problem/UVALive-7041 题意 给出两个仅包含小写字符的字符串 A 和 B : 求:对于 A 中的每个回文子串,B 中和该子串相同的子串个数的总和 ...
- Gym 101194A / UVALive 7897 - Number Theory Problem - [找规律水题][2016 EC-Final Problem A]
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...
- Gym 101194C / UVALive 7899 - Mr. Panda and Strips - [set][2016 EC-Final Problem C]
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...
- Gym 101194E / UVALive 7901 - Ice Cream Tower - [数学+long double][2016 EC-Final Problem E]
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...
随机推荐
- 转:SVN使用教程总结
转自:http://www.cnblogs.com/tugenhua0707/p/3969558.html SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版 ...
- asp.net 从客户端中检测到有潜在危险的 Request.Form 值错误解
从客户端(ftbContent="<P><A href="http://l...")中检测到有潜在危险的 Request.Form 值. 说明: 请求验 ...
- linux command cp.
Examples cp file1.txt newdir Copies the file1.txt in the current directory to the newdir subdirector ...
- centos 给终端设快捷键
centos 终端的快捷键是默认是禁用的 设置的话 系统-> 首选项 -> 键盘快捷键 看到运行终端 随便设置想要的快捷键!!
- StringBuilder和string.Format性能对比
本文由博主(YinaPan)原创,转载请注明出处:http://www.cnblogs.com/YinaPan/p/sbformat.html StringBuilder的性能优于string.For ...
- TalkingData Cocos2dx集成指南【最新】
续:最近终于腾出时间把TalkingData的Cocos版本好好折腾一下了,总感觉之前的各个版本在集成上都很蹩脚.给广大开发者带了很多困扰...“游戏正着急上线呢,哪还有时间去仔细看TalkingDa ...
- Zsh安装CMake补全脚本进行CMake命令补全
最近在尝试使用Zsh,发现其补全命令的功能相当厉害.但对CMake命令的补全在默认的5.0.5中好像没有看到,网上找了下关于配置Zsh补全的文章也没有多少. 于是自己动手,发现在Zsh安装目录 ...
- Oracle高级查询,事物,过程及函数
一 数值函数 数值 abs,ceil,floor,round,trunc字符串 instr,substr SQL>SELECT 'ABS':'|| ABS(-12.3) FROM DUAL; 运 ...
- js性能优化--学习笔记
<高性能网站建设进阶指南>: 1.使用局部变量,避免深入作用域查找,局部变量是读写速度最快的:把函数中使用次数超过一次的对象属性和数组存储为局部变量是一个好方法:比如for循环中的.len ...
- 个人Python常用Package及其安装
为了避免每次重装系统时又要东翻西找,现在此记录一下目前常用的Python包安装过程. 1) Python: 2.7.11, 下载地址:www.python.org.由于个人喜欢使用PyQt4(其实是不 ...