要死了,这个题竟然做了两天……各种奇葩的错误……

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

最新文章

  1. tyvj1189 盖房子
  2. Spring整理
  3. Location对象、History对象
  4. jQuery—选择器
  5. ActiveMQ第三弹:在Spring中使用内置的Message Broker
  6. php开发网站编码统一问题
  7. Change An Item Property Using Set_Item_Property In Oracle Forms
  8. 盘点十大最流行的Linux服务器发行版
  9. Spring中的设计模式
  10. linux下strace命令详解
  11. wget 测试cdn
  12. ORA-12012: error on auto execute of job &amp;quot;ORACLE_OCM
  13. DEV 打印gridcontrl
  14. poj 1035KINA Is Not Abbreviation
  15. Redis数据结构之简单动态字符串SDS
  16. input01.sh: line 11: warning: here-document at line 4 delimited by end-of-file (wanted `EOF&#39;) input01.sh: line 12: syntax error: unexpected end of file
  17. Spring中通过Annotation来实现AOP
  18. spring boot 启动类一定要放置到包的根目录下,也就是和所有包含java文件的包在同一级目录。如果不放置在根目录下,将会提示 no mybatis mapper was found
  19. springboot深入学习(三)-----docker
  20. java实现黑客帝国数字雨特效(转)

热门文章

  1. 新版Eclipse找不到Java EE Module Dependencies选项
  2. Android ScrollView 滚动到顶部
  3. load多个数据文件的yaml
  4. C#多播委托和事件的区别与关系
  5. C语言的常用printf打印占位符%d, %u, %f, %s, %c, %o, %x
  6. 关于JWT(Json Web Token)的思考及使用心得
  7. 动态Axios配置
  8. 紫书 习题 8-16 UVa 1618 (中途相遇法)
  9. redhat下搭建jdk+tomcat环境
  10. CodeForces 52B Right Triangles 矩阵上的计数