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 ...
随机推荐
- 动态用javascript来修改单选框性别
<script> window.onload=function(){ if(<{$data.sex}>==0){//<{$data.sex}>是在数据读出来: do ...
- CSS实现背景透明而背景上的文字不透明完美解决
在我们设计制作一些网页的时候可能会用到半透明的效果,首先我们可能会想到用PNG图片处理,当然这是一个不错的办法,唯一的兼容性问题就是ie6 下的BUG,但这也不困难,加上一段js处理就行了.但假如我们 ...
- Linq101-Projection
using System; using System.Linq; namespace Linq101 { class Projection { /// <summary> /// This ...
- GridView的初级使用
使用GridView自带的分页功能,需要激发PageIndexChanging protected void gvNewsList_PageIndexChanging(object sender, G ...
- (转)一步一步学习PHP(4)——函数
相信每个人在学习PHP之前至少都有着一定的C语言,或者是C++/Java/C#等其他语言的基础,所以在这里也不从头开始说起,只是来谈谈PHP方法的独特之处. 1. 解决作用域问题 在上一节谈到了PHP ...
- 关于mtk Android打开串口权限问题
最近在做一个测试串口读写回路的APK,jni代码部分遇到一个小小问题: fd = open(path_utf, O_RDWR);返回值是-1,要么就是权限问题,要么就是文件不存在所以需要打印错误信息, ...
- iOS真机测试种可能遇到的问题
1. Reason- image not found 用模拟器是没有问题的,不过在真机好像是有问题,不确定是否是所有机型. 崩溃日志 1 2 3 4 5 dyld: Library not l ...
- 二、C# 数据类型
C#语言的基本类型包括8种整数类型.2种用于科学计算的二进制浮点类型.1种用于金融计算的十 进制浮点类型.1种布尔类型以及1种字符类型. 2.1 基本数值类型 C#中的基本数据类型都有关键字和它们关联 ...
- 简单的js反选,全选,全不选
<html> <head> <base href="<%=basePath%>"> <title>My JSP 'che ...
- 嘟!数字三角形 W WW WWW集合!
哔!数字三角形全体集合! 数字三角形!到! 数字三角形W!到! 数字三角形WW!到! 数字三角形WWW!到! --------------------------------------------- ...