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的更多相关文章

  1. UVAlive 6763 Modified LCS

    LCS stands for longest common subsequence, and it is a well known problem. A sequence in thisproblem ...

  2. CSU 1446 Modified LCS 扩展欧几里得

    要死了,这个题竟然做了两天……各种奇葩的错误…… HNU的12831也是这个题. 题意: 给你两个等差数列,求这两个数列的公共元素的数量. 每个数列按照以下格式给出: N F D(分别表示每个数列的长 ...

  3. 为什么你SQL Server的数据库文件的Date modified没有变化呢?

    在SQL Server数据库中,数据文件与事务日志文件的修改日期(Date Modified)是会变化的,但是有时候你会发现你的数据文件或日志文件的修改日期(Date Modified)几个月甚至是半 ...

  4. 我的第一篇博客----LCS学习笔记

    LCS引论 在这篇博文中,博主要给大家讲一个算法----最长公共子序列(LCS)算法.我最初接触这个算法是在高中学信息学竞赛的时候.那时候花了好长时间理解这个算法.老师经常说,这种算法是母算法,即从这 ...

  5. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  6. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  7. 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 ...

  8. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  9. [HTTP Protocol] 200 OK (from cache)和304 Not Modified

    含义 200 OK (from cache)直接从缓存中获取的内容并未请求服务器 304 Not Modified 请求服务器并和服务器比较 If-Modified-Since,若文件未改变,服务器返 ...

随机推荐

  1. 运行时环境(The Runtime Environment)

    App Engine应用响应网络请求.当一个客户端(典型的是用户的Web浏览器)使用HTTP请求(比如获取在URL上的网页)连接上应用的时候,网络请求就开始了.当App Engine接收到请求时,它会 ...

  2. .net组件技术

    .NET是什么? •.NET是一个平台,而不是一种语言. •.NET是Microsoft的用以创建XML Web服务(下一代软件)平台,该平台将信息.设备和人以一种统一的.个性化的方式联系起来.   ...

  3. PyBayes的安装和使用

    PyBayes 主页 文档 PyBayes is an object-oriented Python library for recursive Bayesian estimation (Bayesi ...

  4. ProtoBuffer 简单例子

    最近学了一下protobuf,写了一个简单的例子,如下: person.proto文件 message Person{ required string name = 1; required int32 ...

  5. ocp 1Z0-051 141-175题解析

    141. View the Exhibitand examine the structure of CUSTOMERS and GRADES tables. You need to displayna ...

  6. jdk的wsimport方法实现webservice客户端调用服务

    1.配置好jdk环境,打开命令行,输入wsimport回车能看到很多该命令的参数, -s:要生成客户端代码的存储路径 -p:对生成的代码从新打包 这两个最常用. 在打开的命令行中输入:wsimport ...

  7. 从Jetty、Tomcat和Mina中提炼NIO构架网络服务器的经典模式(一)

    本文转载自 http://blog.csdn.net/cutesource/article/details/6192016 如何正确使用NIO来构架网络服务器一直是最近思考的一个问题,于是乎分析了一下 ...

  8. MSSQL导入数据时,出现“无法截断表 因为表正由Foreign key引用”错误

    * 错误 0xc002f210: 准备 SQL 任务: 执行查询“TRUNCATE TABLE [dsc100552_db].[dbo].[ALV_SalesBigClass] ”失败,错误如下:“无 ...

  9. python视频教程大全

    python3英文视频教程(全87集) http://pan.baidu.com/s/1dDnGBvV python从入门到精通视频(全60集)链接:http://pan.baidu.com/s/1e ...

  10. 项目经验之:再来一章:excel导入数据 封装成最棒的不容易!!!

    我见过很的系统,包括OA,ERP,CRM等,在常用的功能当中,从外部导入数据是最常用到的.因为很多客户需要以excel的形式提供数据,,这样的方式我们又如何做呢, 大家最常见的做法可能是这样的,在需要 ...