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,若文件未改变,服务器返 ...
随机推荐
- 多校6 1010 HDU5802 Windows 10 dfs
// 多校6 1010 HDU5802 Windows 10 // 题意:从p到q有三种操作,要么往上升只能1步,要么往下降,如果连续往下降就是2^n, // 中途停顿或者向上,下次再降的时候从1开始 ...
- Codeforces Round #363
http://codeforces.com/contest/699 ALaunch of Collider 题意:n个球,每个球向左或右,速度都为1米每秒,问第一次碰撞的时间,否则输出-1 贪心最短时 ...
- Hello, Github Blog
hello, I am using github to write a post, I am so exciting- 原文地址: http://vblog.vell001.ml/2014/03/08 ...
- 获取week of year的小程序
#coding=utf8 import urllib, BeautifulSoup web=urllib.urlopen("http://whatweekisit.com/") s ...
- caldera
Caldera International星期一宣布将公司名称变更为SCO Group,交易代码则改为SCOX,希望SCO可以在客户群当中建立更好的品牌认同. Caldera除了有自己的Linux版本 ...
- Delphi开发ocx插件的调试
Delphi开发ocx苦于调试,网上看了下大概配置: IE调用ocx调试配置,在当前ocx工程 run-->parameters-->host application 里面配置IE的程序 ...
- HDU 1142 A Walk Through the Forest (求最短路条数)
A Walk Through the Forest 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1142 Description Jimmy exp ...
- thymeleaf中的th:assert用法
th:assert 断言标签 th:assert属性可以指定一个以逗号分隔的表达式对其进行评估并生产适用于每一个评价,如果不抛出异常 <div th:assert="${onevar} ...
- codeforces 630KIndivisibility(容斥原理)
K. Indivisibility time limit per test 0.5 seconds memory limit per test 64 megabytes input standard ...
- threading模块
threading — Higher-level threading interface¶ Source code: Lib/threading.py This module constructs h ...