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 ;
}

最新文章

  1. 小白Linux入门 二
  2. 【转】MySQL性能优化的21个最佳实践
  3. 【bzoj1407】 Noi2002—Savage
  4. 将/home目录从单独的分区迁移回/目录下
  5. 在win server 2003上安装SQL Server 2008的步骤
  6. NGUI无法按住鼠标按住时无法监听OnHover事件
  7. MTK6577 Android源代码目录
  8. PHP中include和require绝对路径、相对路径问题
  9. Chp18: Hard
  10. AS3 Signals
  11. Qt之文件操作 QFile
  12. MyEclipse中如何去掉JS/JSP语法错误提示
  13. 微服务架构 - SpringBoot整合Jooq和Flyway
  14. BUAAOO第二单元多线程电梯作业总结
  15. 解决jenkins运行磁盘满的问题
  16. X5中CSS设置
  17. ui-sref
  18. Kafka跨网络访问设置
  19. 移动平台的meta标签
  20. linux 下处理大文件

热门文章

  1. openstack stein部署手册 8. neutron-api
  2. Windows 好用的护眼软件
  3. Windows 10 系统获取密钥方法
  4. [IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序
  5. bzoj5518 &amp; loj3046 「ZJOI2019」语言 线段树合并+树链的并
  6. 伊朗Cisco路由器遭黑客攻击 全国互联网几乎瘫痪
  7. JUC并发工具类
  8. asp.net+扫描仪+图片上传
  9. 史上最全最实用HBuilder快捷键大全
  10. 转载:TypeError: Cannot read property &#39;compilation&#39; of undefined vue 打包运行npm run build 报错