CSU 1446 Modified LCS 扩展欧几里得
要死了,这个题竟然做了两天……各种奇葩的错误……
HNU的12831也是这个题。
题意:
给你两个等差数列,求这两个数列的公共元素的数量。
每个数列按照以下格式给出: N F D(分别表示每个数列的长度,首项,公差)。
思路:
先用扩展欧几里得得到两个数列的一个交点,然后求出两个数列的第一个交点。然后分别得到从第一个交点,到末项的可能交点数量。
假设 F1+K1*D1 = F2+K2*D2 是某一个交点, 移向得到 F1 - F2 = K2*D2 - K1*D1。 由扩展欧几里得定理的结论 x*a + y*b = K*gcd(a, b)。
所以 只有 F1-F2 = K*gcd(D1, D2) 时 才存在交点。
并且由此可以求得某一个交点。 然后就是求第一个交点,这个我跪了两天,不想说了 0 0
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <time.h> using namespace std; typedef long long ll; const int INF = <<; ll N1, F1, D1, N2, F2, D2; ll exgcd(ll a, ll b, ll &x, ll &y) {
if (b==) {
x = ; y = ;
return a;
}
ll res = exgcd(b, a%b, y, x);
y -= x*(a/b);
return res;
} ll myAbs(ll x) {
return x> ? x : -x;
} void input() {
scanf("%lld%lld%lld%lld%lld%lld", &N1, &F1, &D1, &N2, &F2, &D2);
} void solve() {
ll x, y;
ll d = exgcd(D1, D2, x, y);
if (!=(myAbs(F1-F2)%d)) {
puts("");
return ;
}
ll k = (F1-F2)/d;
ll k1 = -k*x, k2 = k*y; ll gg = max(F1, F2); //第一个交点必然大于等于两个起点
ll lcm = D1/d*D2; gg = ((F1+k1*D1-gg)%lcm + lcm)%lcm + gg; //求出第一个·交点 ll f1 = (gg-F1)/D1 + ; //计算出第一个交点分别在两个数列中的下标
ll f2 = (gg-F2)/D2 + ; ll n1 = (N1-f1+(D2/d))/(D2/d);
ll n2 = (N2-f2+(D1/d))/(D1/d);
ll ans = min(n1, n2);
if (ans<) ans = ;
printf("%lld\n", ans);
} int main() {
#ifdef Phantom01
freopen("HNU12831.txt", "r", stdin);
#endif //Phantom01 int Case;
scanf("%d", &Case);
while (Case--) {
input();
solve();
} return ;
}
CSU 1446
CSU 1446 Modified LCS 扩展欧几里得的更多相关文章
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)
http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...
- UVA 12169 Disgruntled Judge 枚举+扩展欧几里得
题目大意:有3个整数 x[1], a, b 满足递推式x[i]=(a*x[i-1]+b)mod 10001.由这个递推式计算出了长度为2T的数列,现在要求输入x[1],x[3],......x[2T- ...
- UVA 10090 Marbles 扩展欧几里得
来源:http://www.cnblogs.com/zxhl/p/5106678.html 大致题意:给你n个球,给你两种盒子.第一种盒子每个盒子c1美元,可以恰好装n1个球:第二种盒子每个盒子c2元 ...
- POJ 1061 青蛙的约会 扩展欧几里得
扩展欧几里得模板套一下就A了,不过要注意刚好整除的时候,代码中有注释 #include <iostream> #include <cstdio> #include <cs ...
- 【64测试20161112】【Catalan数】【数论】【扩展欧几里得】【逆】
Problem: n个人(偶数)排队,排两行,每一行的身高依次递增,且第二行的人的身高大于对应的第一行的人,问有多少种方案.mod 1e9+9 Solution: 这道题由1,2,5,14 应该想到C ...
- poj 2891 扩展欧几里得迭代解同余方程组
Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互 ...
- poj 2142 扩展欧几里得解ax+by=c
原题实际上就是求方程a*x+b*y=d的一个特解,要求这个特解满足|x|+|y|最小 套模式+一点YY就行了 总结一下这类问题的解法: 对于方程ax+by=c 设tm=gcd(a,b) 先用扩展欧几里 ...
- poj 1061 扩展欧几里得解同余方程(求最小非负整数解)
题目可以转化成求关于t的同余方程的最小非负数解: x+m*t≡y+n*t (mod L) 该方程又可以转化成: k*L+(n-m)*t=x-y 利用扩展欧几里得可以解决这个问题: eg:对于方程ax+ ...
- Codeforces7C 扩展欧几里得
Line Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Status ...
随机推荐
- setUserVisibleHint的使用.执行顺序和viewPager.setOffscreenPageLimit(0)不管用还是默认会加载第二个fragment
处理问题一:viewPager.setOffscreenPageLimit(0)不管用还是默认会加载第二个fragment的原因(源码解读); 处理问题二:setUserVisibleHint的使用场 ...
- 简单暴力的TP5多主题方案
一个小项目,需要配置多套前端主题.解决的思路是根据域名加载不同的主题配置. 一.在应用目录 application 下创建 common 目录. 二.application/common 目录下创建 ...
- python对比图片
通过python的PIL模块可以对比两张图片是否相同,具体源码如下 from PIL import Image from PIL import ImageChops def compare_image ...
- NOIp2018模拟赛四十二
今天看标题终于回到了“NOIP模拟赛”,十分高兴啊! 然后一打开题目: ********** 所以今天又是一场NOIPlus模拟赛(微笑) 成绩:0+70+0=70 A题想了个贪心被myh两分钟cha ...
- k8s日志收集配置
容器日志样例 172.101.32.1 - - [03/Jun/2019:17:14:10 +0800] "POST /ajaxVideoQueues!queryAllUser.action ...
- centos7.x86_64搭建饥荒服务器
http://blog.ttionya.com/article-1233.html 在centos7下找不到 libcurl-gnutls.so.4,而且必须要安装32位的才行 yum install ...
- find the safest road HDU杭电1596【Dijkstra || SPFA】
pid=1596">http://acm.hdu.edu.cn/showproblem.php?pid=1596 Problem Description XX星球有非常多城市,每一个城 ...
- 适配 iOS 8 时遇到的问题两则:远程推送和 Unwind Segue
原文:http://imtx.me/archives/1910.html 昨天我在微博上吐槽:iOS 8 / Xcode 6 真是史上对开发人员最糟糕的版本号了.收到非常多朋友表达同感. 之所以这么说 ...
- sass08 if while for each
scss @function getzIndex($layer: default){ $zindexMap: (default: 1, modal: 1000, dropdown: 500, grid ...
- 语法错误: unexpected ''); ?></span></span></h2> ' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';'
语法错误: unexpected ''); ?></span></span></h2>' (T_CONSTANT_ENCAPSED_STRING), expe ...