ACPC 2013 J.Modified LCS

本文介绍了一个经典的计算机科学问题——最长公共子序列(LCS)问题的一个变种,并提供了一种解决该问题的方法。该问题涉及寻找两个特定整数序列的最长共同子序列的长度,其中序列由初始值、长度及增量定义。文章还提供了实现这一算法的C++代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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.
In this problem you are given two sequences and your task is to find 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 first element in the sequence. Each element except the first 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 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值