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 ...
随机推荐
- vijos P1375 大整数(高精不熟的一定要做!)
/* 我尼玛这题不想说啥了 亏了高精写的熟..... 加减乘除max都写了 高精二分 */ #include<iostream> #include<cstdio> #inclu ...
- css3响应式布局
响应式布局 分栏布局,-webkit-column-width(定义每栏的宽度,会根据每栏宽度自动分成若干栏) <style> .wrap {width: 900px; border: 1 ...
- (转)dedecms入门
学dedecms一段时间了,把我的入门体会和大家分享一下. 什么是dedecm cms(内容管理系统):现在有各种内容模型,如书评(包括书名,出版社,评论等字段).cms一般有用户后台,网页的用户可以 ...
- c-函数指针(求奇数偶数的和)
#include <stdio.h> /* 编写一个函数,输入 n 为偶数时,调用函数求 1/2+1/4+...+1/n,当输入 n 为奇数时,调用函数1/1+1/3+...+1/n(利用 ...
- oracle死锁模拟
环境介绍: 用户test01 创建表tab01,用户test02创建表tab02.Test01 更新tab01不提交,test02 更新表tab02不提交.然后test01 更新test02下的表ta ...
- javascript之typeof、constructor、instanceof
ref: http://jingyan.baidu.com/article/29697b912f9939ab20de3c8c.html
- js 实现win7任务栏拖动效果
前言 在某个时刻, 我认识了一个朋友. 此人在我的教唆下, 踏上了js的不归路. 前天他问我, Win7任务栏拖动效果怎么实现. 我随口就跟他说, 这简单的一逼. 在我一晚上的折腾之后, 一份潦草的代 ...
- ubuntu忘记登录账户以及密码
笔者在诸多方面仍然是初学者.感兴趣的方面也很多,电脑装上ubuntu14.04也有一段时间了,但仍然在不断学习更多基础的东西. 因为对于命令行界面还有些不习惯,所以一直依赖于图形界面,需要使用终端的时 ...
- OOCSS学习(一)
OOCSS —— 面向对象CSS 搜集一些该搜集的,然后汇总一下. 1.OOCSS 概念篇: 1)什么是面向对象 确定“对象”,并给这个对象创建CSS样式规则. 2)面向对象的CSS理论 OOCSS最 ...
- jquery 插件模版
;(function ($) { //插件的默认值属性 var defaults = { Weight: '300px', height: '230px', nextId: 'nextBtn', ne ...