UVAlive 6763 Modified LCS
LCS stands for longest common subsequence, and it is a well known problem. A sequence in this
problem means a list of integers, and a sequence X is considered a subsequence of another sequence Y ,
when the sequence X can be obtained by deleting zero or more elements from the sequence Y without
changing the order of the remaining elements.
In this problem you are given two sequences and your task is to nd the length of the longest
sequence which is a subsequence of both the given sequences.
You are not given the sequences themselves. For each sequence you are given three integers N, F
and D, where N is the length of the sequence, F is the rst element in the sequence. Each element
except the rst element is greater than the element before it by D.
For example N = 5, F = 3 and D = 4 represents the following sequence: [3, 7, 11, 15, 19].
There will be at least one integer which belongs to both sequences and it is not greater than
1,000,000.
Input
Your program will be tested on one or more test cases. The rst line of the input will be a single integer
T, the number of test cases (1 T 100). Followed by the test cases, each test case is described in one
line which contains 6 integers separated by a single space N1 F1 D1 N2 F2 D2 (1 N1;N2 1018)
and (1 F1;D1; F2;D2 109) representing the length of the rst sequence, the rst element in the
rst sequence, the incremental value of the rst sequence, the length of the second sequence, the rst
element in the second sequence and the incremental value of the second sequence, respectively.
Output
For each test case, print a single line which contains a single integer representing the length of the
longest common subsequence between the given two sequences.
Sample Input
3
5 3 4 15 3 1
10 2 2 7 3 3
100 1 1 100 1 2
Sample Output
4
3
这条题是组队排位的中下题。小邪卡了很久。
解法其实就是用扩展欧几里得算法求出第一个出现的相等的位置。
f1 + (x-1)*d1 = f2 + (y1 -1 )*d2;
除去前面的位置,接下来就是算两个等差序列有多少段出现LCS,然后取较小那个就可以了
#include<bits/stdc++.h>
using namespace std;
typedef long long LL; void e_gcd(LL a,LL b,LL &x,LL &y,LL &d)
{
if( !b ){ d = a,x = ,y = ; return ;}
e_gcd(b,a%b,y,x,d);
y -= x * (a / b);
}
int main()
{
int _;
LL k1,k2,c;
LL x,y,L1,L2,R1,R2,ML,MR,d;
LL f1,f2,d1,d2,n1,n2;
scanf("%d",&_);
while(_--){
LL ans=;
scanf("%lld%lld%lld%lld%lld%lld",&n1,&f1,&d1,&n2,&f2,&d2);
c = f1 -f2;
d = __gcd(d1,d2);
if( c % d ){puts("");continue;} e_gcd(d1,d2,x,y,d);
k1 = -x * (c/d);
k2 = y * (c/d);
d1 /= d , d2 /= d;
L1 = ceil( (-k1*1.0)/d2 );
L2 = ceil( (-k2*1.0)/d1 );
R1 = floor( (n1-k1) * 1.0 / d2);
R2 = floor( (n2-k2) * 1.0 / d1);
if( (n1 - k1) %d2 == )R1 --;
if( (n2 - k2) %d1 == )R2 --; ML = max(L1,L2);
MR = min(R1,R2);
ans = max(0LL,MR - ML + );
printf("%lld\n",ans);
}
return ;
}
UVAlive 6763 Modified LCS的更多相关文章
- Modified LCS
Input Output Sample Input 3 5 3 4 15 3 1 10 2 2 7 3 3 100 1 1 100 1 2 Sample Output 4 3 50超时代码,因为K很大 ...
- CSU 1446 Modified LCS 扩展欧几里得
要死了,这个题竟然做了两天……各种奇葩的错误…… HNU的12831也是这个题. 题意: 给你两个等差数列,求这两个数列的公共元素的数量. 每个数列按照以下格式给出: N F D(分别表示每个数列的长 ...
- UVALive 6763 / CSU 1446
今天比赛的时候拿到的第一道题,其实挺简单的,求两等差序列中相同元素的个数,我想了一下就觉得,只要找到了第一个相等的点,然后后面求最大公约数就可以直接得到结果了 网上叫什么拓展欧几里得,我反正是按照我们 ...
- 为什么你SQL Server的数据库文件的Date modified没有变化呢?
在SQL Server数据库中,数据文件与事务日志文件的修改日期(Date Modified)是会变化的,但是有时候你会发现你的数据文件或日志文件的修改日期(Date Modified)几个月甚至是半 ...
- 我的第一篇博客----LCS学习笔记
LCS引论 在这篇博文中,博主要给大家讲一个算法----最长公共子序列(LCS)算法.我最初接触这个算法是在高中学信息学竞赛的时候.那时候花了好长时间理解这个算法.老师经常说,这种算法是母算法,即从这 ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- Hackerrank11 LCS Returns 枚举+LCS
Given two strings, a and , b find and print the total number of ways to insert a character at any p ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
随机推荐
- 关于post xml的请求和响应
关于post的请求作为开发者应该常用到,post请求的数据包含了参数和data,post参数相对比较容易理解,比如我们一个form提交,其实就是调用后台方法的,发送相关参数,这里我单独说一下关于pos ...
- VS #include 【bits/bstdc++.h】出错
目录 1. 本文地址 2. 按 3. 操作步骤 1. 本文地址 博客园:https://www.cnblogs.com/coco56/p/11163142.html 简书:https://www.ji ...
- python面向对象--类的内置方法
#isinstance(obj,cls)判断obj是否是类cls的实例 #issubclass(cls,cls1)判断cls是否是cls1的子类或派生类 class Foo: pass class B ...
- cd 切换目录
1. 功能说明 cd是“change directory”中每个氮气的首字母缩写功能是重当前工作目录切换到指定的工作目录:cd是内建命令. 2. 语法格式 cd [option] [dir] cd ...
- oracle汇编03
.long expression1, expression2, ..., expressionNThe .long directive generates a long integer (32-bit ...
- 【LGR-062】洛谷10月月赛 III div.2 (A-C)
前言 100+100+46+0=246pts 300多名 以后每次比赛都要有进步哦!qwq 小D与笔试 水题 Code #include<algorithm> #include<io ...
- 插头$DP$学习小结
插头\(DP\)学习小结 这种辣鸡毒瘤东西也能叫算法... 很优秀的一个算法. 最基本的适用范围主要是数据范围极小的网格图路径计数问题. 如果是像\(Noi2018\)那种的话建议考生在其他两道题难度 ...
- 三栏布局的三个典型方法(圣杯、双飞翼、flex)
聊聊三栏布局----左右定宽,中间自适应. 效果图: 圣杯布局 <!DOCTYPE html> <html> <head lang="en"> ...
- opencv 环境配置-windowsx64 +VS2017
opencv版本为4.1.1官方最新版本, https://sourceforge.net/projects/opencvlibrary/ 先配置本地工程环境: 右键我的电脑-属性 将bin的路径添加 ...
- 详解zabbix中文版安装部署
一.zabbix简介(摘自百度百科) zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供柔软 ...