Modified LCS |
Time Limit: 10000ms, Special Time Limit:25000ms, Memory Limit:65536KB |
Total submit users: 29, Accepted users: 17 |
Problem 12831 : No special judgement |
Problem description |
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. |
Input |
Your program will be tested on one or more test cases. The first line of the input will be a single integer T, the number of test cases T ranges in(1,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 (N1,N2 ranges in(1,10^18) and F1,D1,F2,D2 ranges in(1,10^9)) representing the length of the first sequence, the first element in the first sequence, the incremental value of the first sequence, the length of the second sequence, the first 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 50 |
Problem Source |
ACPC 2013 |
Submit Discuss Judge Status Problems Ranklist |
hnu挂的比赛,比赛时候纠结于扩展欧几里得没做出来简直是罪恶(hnu支持%I64d注意一下).......
代码如下:
#include<stdio.h>
#include<iostream>
using namespace std;
typedef long long LL;
LL gcd(LL a,LL b)
{
return b == 0?a:gcd(b,a%b);
}
LL exgcd(LL a,LL b,LL &x,LL &y)
{
if(b == 0)
{
x = 1;
y = 0;
return a;
}
LL g = exgcd(b,a%b,x,y);
LL temp;
temp = x;
x = y;
y = temp - a/b*y;
return g;
}
int main()
{
LL a,b,c,n1,f1,d1,n2,f2,d2;
LL xx,yy;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&n1,&f1,&d1,&n2,&f2,&d2);
a = d1;
b = d2;
c = (f2-f1)-(d2-d1);
LL g = gcd(a,b);
exgcd(a,b,xx,yy);
xx = (((xx%(b/g)*(c/g)%(b/g)))%(b/g)+(b/g))%(b/g);
if(xx == 0) xx+=b/g;
yy = (c - a*xx)/b;
yy = -yy;
LL sum1 = f1+(n1-1)*d1;
LL sum2 = f2+(n2-1)*d2;
LL sum = 0,sum_first = 0,dd = 0;
LL ans = 0;
dd = d1/gcd(d1,d2)*d2;
sum = sum2<sum1?sum2:sum1;
sum_first = d1*(xx-1)+f1;
ans = ((sum-sum_first)/dd)+1;
printf("%I64d\n",ans);
}
return 0;
}