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. MFC学习20160718(GetModuleFileName&&GetAppDataPath)

    1.标题栏设置 一.对话框标题栏内容为静态 直接在对话框属性“General”的“Caption”中修改. 二.对话框标题栏内容为动态生成的 在对应对话框的初始化函数OnInitDialog()中添加 ...

  2. 设置sonar 界面为中文环境

    sonar 默认是英文的界面 1.下载http://repository.codehaus.org/org/codehaus/sonar-plugins/l10n/sonar-l10n-zh-plug ...

  3. unsupported dynamic reloc R_ARM_REL32 AND hidden symbol '__dso_handle' is not defined

    项目里编译codec src\makefiles\android\codec\Makefileline 25 原本用 4.6 不会报错-L/data/android/android-ndk/sourc ...

  4. iOS 使用FMDB SQLCipher给数据库加密

    关于SQLite,SQLCipher和FMDB SQLite是一个轻量的.跨平台的.开源的数据库引擎,它的在读写效率.消耗总量.延迟时间和整体简单性上具有的优越性,使其成为移动平台数据库的最佳解决方案 ...

  5. Spring AOP Interceptor transaction is not working

    Problem The Spring AOP transaction is not working in following interceptors? <bean id="testA ...

  6. 写的一个判断注册Email是否是个人邮件,而不是公司邮件的方法

    以下这个方法其实也不是很全面,它只判断了hotmail, gmail和yahoo 如果你还需要加上其他认为是私人Email的Email, 只要按照同样的方法自己加上就可以了 Public void C ...

  7. 【Linux常用工具】02. 创建启动定时任务工具cron

    一. cron 1. cron是一个守护程序,它提供定时器的功能,让用户在特定的时间得以执行默认的指令或程序.只要用户会编辑定时器的设置文件,就可以使用定时器的功能. 定时器文件格式: 2. cron ...

  8. ActiveMQ学习笔记(二) JMS与Spring

    上文可见,JMS Native API使用起来不是特别方便.好在Spring提供了很好的JMS支持. (一)配置ConnectionFactory 如果使用连接池的话,不要忘记activemq-poo ...

  9. matlab eps中文乱码的解决方法

    直接存成eps总是乱码 最优解决方法是matlab print 保存成jpg,之后用adobe  acrobat pro 打开jpg文件另存为eps

  10. 如何同时启动多个Tomcat服务

    在项目开发中,有时会需要同时启动多个Tomcat服务,如果直接启动多个的话,会报以下错误: Port busy xxxx java.net.SocketException: Unrecognized ...