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 ...
随机推荐
- React-请求篇
请求方式: (1)后台API:HttpPost [FromForm] UserRetisterDTO dto 前端请求: { body:qs.stringify(dto), header ...
- C# 获取系统环境数据
using System; using System.Data; using System.Text.RegularExpressions; using System.Threading; names ...
- Oracle Set操作
并集合 union/uinon all union 会去重,uinon all 不去重 交集 intersect 差集 minus
- console.log 不起作用
devtool console.log 突然不起作用了
- idea中ehcahe配置中 Cannot find the declaration of element 'ehcache'.
ehcahe.xml 中报错: Cannot find the declaration of element 'ehcache'. 打开settings->languages&frame ...
- golang API
1.server端程序 package main //简单的JSON Restful API演示(服务端) //author: Xiong Chuan Liang //date: 2015-2-28 ...
- 一些vue 响应式系统的底层的细节
当你把一个普通的 JavaScript 对象传给 Vue 实例的 data 选项,Vue 将遍历此对象所有的属性,并使用 Object.defineProperty 把这些属性全部转为 getter/ ...
- __getattr__属性查找
from datetime import date """ __getattr__ : 在查找不到对象的属性时调用 __getattribute__ : 在查找属性之前调 ...
- UIWebView和WKWebView一些琐事
WebViewJavascriptBridge 1.load加载 ,去本地查找html路径方式 NSString* htmlPath = [[NSBundle mainBundle] pathForR ...
- vscode中执行gulp task的简便方法
本文重点是gulp在vscode中执行task任务的方法 如何像webstorm那样简便操作gulp 的task 第1步:安装node.下载地址:https://nodejs.org/zh-cn/ 检 ...