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很大
/*****************
f1+(k1-1)*d1=f2+(k2-1)*d2
=> (k1-1)*d1+(k2-1)*(-d2)=f2-f1;
=> d1*x+y*(-d2)=f2-f1 d1=>[0,n1-1] d2=>[0,n2-1]
求在给定范围内解的个数
*********************/
#include"iostream"
#include"cstdio"
#include"cstring"
#include"cmath"
#include"algorithm"
using namespace std;
typedef long long ll;
void exgcd(ll n,ll m,ll &x,ll &y,ll &d)
{
if(!m){
x=;y=;d=n;
}
else{
exgcd(m,n%m,y,x,d);
y-=(n/m)*x;
}
}
int main()
{
ll f1,n1,d1,f2,n2,d2,i,j,t;
cin>>t;
while(t--)
{
ll x,y,tmp,d;
//cin>>n1>>f1>>d1>>n2>>f2>>d2;
scanf("%lld%lld%lld%lld%lld%lld",&n1,&f1,&d1,&n2,&f2,&d2);
exgcd(d1,-d2,x,y,d);
f2-=f1;
if(f2%d){
//cout<<"0"<<endl;
puts("");
continue;
}
x=x*(f2/d);
y=y*(f2/d);
//接下来 逼近[0,n1-1],[0,n2-1]
ll t1=d2/(abs(d));
ll t2=d1/(abs(d));
if(x<||y<)
{
int i=;
while()
{
if(x+t1*i>=&&y+t2*i>=)
break;
i++;
}
x=x+t1*i;
y=y+t2*i;
}
else
{
int i=;
while()
{
if(x-t1*i<||y-t2*i<)
break;
i++;
}
x=x-t1*(i-);
y=y-t2*(i-);
}
if(x>(n1-)||y>(n2-))
{
//cout<<"0"<<endl;
puts("");
continue;
}
int ans=;
while()
{
if(x+t1*ans>(n1-)||y+t2*ans>(n2-))
break;
ans++;
}
cout<<ans<<endl;
//printf("%lld\n",ans);
}
return ;
}
AC代码
/*****************
f1+(k1-1)*d1=f2+(k2-1)*d2
=> (k1-1)*d1+(k2-1)*(-d2)=f2-f1;
=> d1*x+y*(-d2)=f2-f1 d1=>[0,n1-1] d2=>[0,n2-1]
求在给定范围内解的个数
*********************/
#include"iostream"
#include"cstdio"
#include"cstring"
#include"cmath"
#include"algorithm"
using namespace std;
typedef long long ll;
void exgcd(ll n,ll m,ll &x,ll &y,ll &d)
{
if(!m){
x=;y=;d=n;
}
else{
exgcd(m,n%m,y,x,d);
y-=(n/m)*x;
}
}
int main()
{
ll f1,n1,d1,f2,n2,d2,i,j,t;
cin>>t;
while(t--)
{
ll x,y,tmp,d;
//cin>>n1>>f1>>d1>>n2>>f2>>d2;
scanf("%lld%lld%lld%lld%lld%lld",&n1,&f1,&d1,&n2,&f2,&d2);
exgcd(d1,-d2,x,y,d);
f2-=f1;
if(f2%d){
//cout<<"0"<<endl;
puts("");
continue;
}
x=x*(f2/d);
y=y*(f2/d);
//接下来 逼近[0,n1-1],[0,n2-1]
ll t1=d2/(abs(d));
ll t2=d1/(abs(d));
if(x<||y<)
{
int i=;
while()
{
if(x+t1*i>=&&y+t2*i>=)
break;
i++;
}
x=x+t1*i;
y=y+t2*i;
}
else
{
int i=;
while()
{
if(x-t1*i<||y-t2*i<)
break;
i++;
}
x=x-t1*(i-);
y=y-t2*(i-);
}
if(x>(n1-)||y>(n2-))
{
//cout<<"0"<<endl;
puts("");
continue;
}
int ans=;
/* while(1)
{
if(x+t1*ans>(n1-1)||y+t2*ans>(n2-1))
break;
ans++;
}
*/
// 这样避免超时
ll a,b;
a=(n1--x)/t1;
b=(n2--y)/t2;
cout<<min(a,b)+<<endl;
}
return ;
}
Modified LCS的更多相关文章
- UVAlive 6763 Modified LCS
LCS stands for longest common subsequence, and it is a well known problem. A sequence in thisproblem ...
- CSU 1446 Modified LCS 扩展欧几里得
要死了,这个题竟然做了两天……各种奇葩的错误…… HNU的12831也是这个题. 题意: 给你两个等差数列,求这两个数列的公共元素的数量. 每个数列按照以下格式给出: N F D(分别表示每个数列的长 ...
- 为什么你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 ...
- [HTTP Protocol] 200 OK (from cache)和304 Not Modified
含义 200 OK (from cache)直接从缓存中获取的内容并未请求服务器 304 Not Modified 请求服务器并和服务器比较 If-Modified-Since,若文件未改变,服务器返 ...
随机推荐
- bzoj 3505 [Cqoi2014]数三角形(组合计数)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3505 [题意] 在n个格子中任选3点构成三角形的方案数. [思路] 任选3点-3点共线 ...
- leetcode@ [310] Minimum Height Trees
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- crontab读取环境变量方法
crontab如果不注意的话早晚会出问题,而且这种问题一旦出一次,就会永远记得,因为这种问题很折腾人. ...
- WINRAR评估版本弹出框消除
网上有很多WINRAR评估版本,这些版本下载安装了之后总会有些广告弹出,让人很烦恼,现在教大家一个方法消除这些弹出框. 复制以下代码: RAR registration data SeVeN U ...
- UVALive 7464 Robots (贪心)
Robots 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/K Description http://7xjob4.com1.z ...
- Gym 100507H Pair: normal and paranormal (贪心)
Pair: normal and paranormal 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/H Description ...
- jps用法
jps(Java Virtual Machine Process Status Tool)是JDK 1.5提供的一个显示当前所有java进程pid的命令,简单实用,非常适合在linux/unix平台上 ...
- 微软企业库5.0学习-Security.Cryptography模块
一.微软企业库加密应用模块提供了两种加密: 1.Hash providers :离散加密,即数据加密后无法解密 2.Symmetric Cryptography Providers:密钥(对称)加密法 ...
- UVaLive 6858 Frame (水题)
题意:给定一个矩形框架,给定一个小矩形,问你能不能正好拼起来. 析:很简单么,就三种情况,如果是1*1的矩形,或者是1*2的一定可以,然后就是上面和下面正好能是小矩形的整数倍,左右是少一,两个就是整数 ...
- UVaLive 6855 Banks (水题,暴力)
题意:给定 n 个数,让你求最少经过几次操作,把所有的数变成非负数,操作只有一种,变一个负数变成相反数,但是要把左右两边的数加上这个数. 析:由于看他们AC了,时间这么短,就暴力了一下,就AC了... ...