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的更多相关文章

  1. 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很大 ...

  2. CSU 1446 Modified LCS 扩展欧几里得

    要死了,这个题竟然做了两天……各种奇葩的错误…… HNU的12831也是这个题. 题意: 给你两个等差数列,求这两个数列的公共元素的数量. 每个数列按照以下格式给出: N F D(分别表示每个数列的长 ...

  3. UVALive 6763 / CSU 1446

    今天比赛的时候拿到的第一道题,其实挺简单的,求两等差序列中相同元素的个数,我想了一下就觉得,只要找到了第一个相等的点,然后后面求最大公约数就可以直接得到结果了 网上叫什么拓展欧几里得,我反正是按照我们 ...

  4. 为什么你SQL Server的数据库文件的Date modified没有变化呢?

    在SQL Server数据库中,数据文件与事务日志文件的修改日期(Date Modified)是会变化的,但是有时候你会发现你的数据文件或日志文件的修改日期(Date Modified)几个月甚至是半 ...

  5. 我的第一篇博客----LCS学习笔记

    LCS引论 在这篇博文中,博主要给大家讲一个算法----最长公共子序列(LCS)算法.我最初接触这个算法是在高中学信息学竞赛的时候.那时候花了好长时间理解这个算法.老师经常说,这种算法是母算法,即从这 ...

  6. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  7. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  8. 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 ...

  9. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

随机推荐

  1. ln创建软链接方式

    ln -s 目标文件 软链接

  2. Linux 内核层和 用户层 配置 GPIO 引脚

    Linux BSP 开发的基础就是和GPIO打交道, 下面总结下这几天对某家开发板的GPIO控制的知识. 公司的开发板用的是 DTB  模式 ,首先,进入 dts,dtsi文件查看关于GPIO 的模块 ...

  3. Python not and or

    刷题时候,有道题目的答案是 return(num and (num % 9 or 9)) 看的有点懵逼,看来解释如下: 1.首先,’and’.’or’.’not’的优先级是not>and> ...

  4. [web 安全] xxe

    一.探测漏洞 1.是否支持实体解析. 2.是否支持外部实体解析. 2.1 直接读取本地文件: 2.2 远程文件: 3.不回显错误,则用 blind xxe.(先获取本地数据,然后带着本地数据去访问恶意 ...

  5. Java中最基本的集合接口:初识Collection

    Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements). 一些 Collection允许相同的 ...

  6. CBV和FBV

    CBV和FBV 刚开始写的视图都是基于函数版本的,称为FBV,后来写了一个NB的叫CBV,就是基于类的 FBV就是在URL中的一个路径对应一个函数 urlpatterns = [ url(r'^adm ...

  7. B/S大文件上传解决方案

    第一点:Java代码实现文件上传 FormFile file = manform.getFile(); String newfileName = null; String newpathname =  ...

  8. 暴力&打表

    _LH巨神好像不太会打表,这里来普及一下 还有暴力这么重要的东西网上讲的人竟然不多…… 一.打表 打表,就是针对一些输入数据比较小的题目的一种骗分技巧,当然有时候也可以在正解或暴力中起一定优化作用. ...

  9. Best Practices For Running On The PS4

    原文:https://forums.unrealengine.com/showthread.php?54448-Best-Practices-For-Running-On-The-PS4 Hey gu ...

  10. UE4在PSVR中的抗锯齿和优化相关知识

    UE4目前版本(4.15)在PS平台上并不支持MSAA,在未来的版本会加入.也就是说目前没有办法在PS平台上使用Forward Rendering + MSAA的组合 FXAA效率最高,但效果最差,只 ...