LightOJ 1306 Solutions to an Equation
来源:互联网 发布:samba源码下载 编辑:程序博客网 时间:2024/06/07 21:03
LightOJ 1306 Solutions to an Equation
You have to find the number of solutions of the following equation:
Ax + By + C = 0
Where A, B, C, x, y are integers and x1 ≤ x ≤ x2 andy1 ≤ y ≤ y2.
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case starts with a line containing seven integers A, B, C, x1, x2, y1, y2 (x1 ≤ x2, y1 ≤ y2). The value of each integer will lie in the range [-108, 108].
Output
For each case, print the case number and the total number of solutions.
Sample Input
5
1 1 -5 -5 10 2 4
-10 -8 80 -100 100 -90 90
2 3 -4 1 7 0 8
-2 -3 6 -2 5 -10 5
1 8 -32 0 0 1 10
Sample Output
Case 1: 3
Case 2: 37
Case 3: 1
Case 4: 2
Case 5: 1
题意: 给你一个方程求有多少组解。 给定的数正负零都可以。
巨怕这种题(orz)
思路: 扩欧的应用,注意正负零的特判,还有 其他的一些情况
#pragma comment(linker, "/STACK:1024000000,1024000000")//#include <bits/stdc++.h>#include<string>#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<queue>#include<stack>#include<vector>#include<algorithm>#define maxn 10010#define INF 0x3f3f3f3f#define eps 1e-8#define MOD 1000000007#define ll long longusing namespace std;ll exgcd(ll a, ll b, ll &x, ll &y){ if(b == 0) { x = 1; y = 0; return a; } ll d = exgcd(b, a % b, y, x); y -= (a / b)*x; return d;} int main(){ int T; int cnt = 0; cin >> T; while(T--) { ll a, b, c, x1, x2, y1, y2; scanf("%lld%lld%lld", &a, &b, &c); scanf("%lld%lld%lld%lld",&x1, &x2, &y1, &y2); c = -c; if(a < 0) { a = -a; swap(x1, x2); x1 = -x1; x2 = -x2; } if(b < 0) { b = -b; swap(y1, y2); y1 = -y1; y2 = -y2; } printf("Case %d: ", ++cnt); ll x0 = 0 , y0 = 0; ll g = exgcd(a, b, x0, y0); if(g!=0 && c % g != 0) { printf("0\n"); continue; } if(a == 0 && b == 0) { if(!c) printf("%lld\n", (x2 - x1 + 1) * (y2 - y1 + 1)); else printf("0\n"); continue; } if(a == 0) { if(c / b >= y1 && c / b <= y2) printf("%lld\n", x2 - x1 + 1); else printf("0\n"); continue; } if(b == 0) { if(c / a >= x1 && c / a <= x2) printf("%lld\n", y2 - y1 + 1); else printf("0\n"); continue; } x0 = x0 * c / g; y0 = y0 * c / g; a /= g; b /= g; ll l = ceil((double)(x1 - x0)/(double)(b)); ll r = floor((double)(x2 - x0)/(double)(b)); ll w = ceil((double)(y0 - y2)/(double)(a)); ll e = floor((double)(y0 - y1)/(double)(a)); ll q = max(l, w); ll p = min(r, e); if(p < q) printf("0\n"); else printf("%lld\n", p - q + 1); } return 0;}//向上取整(ceil()) 向下取整(floor)
- LightOJ 1306 Solutions to an Equation
- Solutions to an Equation LightOJ
- d224: 11296 - Counting Solutions to an Integral Equation
- UVa11296 - Counting Solutions to an Integral Equation(枚举技巧)
- Solutions: Using Mac OSX Terminal as an alternative to PuTTY
- A proof of an equation
- Solutions to big data
- Jordan Lecture Note-6: The Solutions of Nonlinear Equation.
- 论文阅读:Reboots are for Hardware: Challenges and Solutions to Updating an Operating System on the Fly
- FZU 1909 An Equation 解题报告
- fzu 1909 An Equation(水题)
- LightOj 1110 An Easy LCS
- LightOJ 1110 An Easy LCS
- Where to Run LightOJ
- solutions
- Solutions to Software Engineering Foundation-PartⅠ
- Solutions to Software Engineering Foundation-PartⅡ
- Solutions to <Learning jQuery> Chapter 3
- 上架iPad应用
- java中方法的重载
- 机器学习中的损失函数
- Android获取屏幕宽高像素
- redhat中配置Terminal终端快捷键的方法
- LightOJ 1306 Solutions to an Equation
- JSON数据格式
- C——printf和scanf函数
- 酸辣粉卡死
- js下拉列表实现增加和移除选项
- 再谈PHP单引号和双引号区别
- ORACLE Rman删除归档日志
- Python 学习File、os模块操作文件
- soj3160: Clear And Present Danger_最短路Floyd