codevs1213 解的个数
已知整数x,y满足如下面的条件:
ax+by+c = 0
p<=x<=q
r<=y<=s
求满足这些条件的x,y的个数。
第一行有一个整数n(n<=10),表示有n个任务。n<=10
以下有n行,每行有7个整数,分别为:a,b,c,p,q,r,s。均不超过108。
共n行,第i行是第i个任务的解的个数。
2
2 3 -7 0 10 0 10
1 1 1 -10 10 -9 9
1
19
#include<iostream>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
ll x,y,a,b,c,g,p,q,r,s;
ll exgcd(ll a,ll b,ll &x,ll &y){
if(b == ){
x = ;
y = ;
return a;
}
ll ans = exgcd(b,a%b,x,y);
ll t = x;
x = y;
y = t - (a/b) * y;
return ans;
}
ll gcd(ll a,ll b){
return b == ? a : gcd(b,a%b);
}
int main(){
int n;
cin>>n;
for(int i = ;i <= n;i++){
cin>>a>>b>>c>>p>>q>>r>>s;
c = -c;
if(a == && b == && c!= ){
cout<<<<endl;
continue;
}
if(q < p || s < r){
cout<<<<endl;
continue;
}
if(a == && b== && c==){
cout<<(q-p+)*(s-r+)<<endl;
continue;
}
g = exgcd(a,b,x,y);
x *= c/g;
y *= c/g;
if(x*a + y*b != c){
cout<<<<endl;
continue;
}
ll plusx = b / g,plusy = a / g,dx,dy;
ll acc = ;
bool fu;
if(plusx < ) fu = true;
else fu = false;
while(x < p){
if(plusx == ) break;
if(!fu) x+=plusx,y-=plusy;
else x-=plusx,y+=plusy;
}
while(x > q){
if(plusx == ) break;
if(!fu) x-= plusx,y+=plusy;
else x+=plusx,y-=plusy;
}
if(x >= p && x <= q && y >= r && y <= s)acc++;
dx = x;
dy = y;
while(dx >= p){
if(plusx == ) break;
if(!fu){
dx -= plusx;
dy += plusy;
}else{
dx += plusx;
dy -= plusy;
}
if(dx >= p && dx <= q && dy >= r && dy <= s) acc++;
}
dx = x;
dy = y;
while(dx <= q){
if(plusx == ) break;
if(!fu){
dx += plusx;
dy -= plusy;
}else{
dx -= plusx;
dy += plusy;
}
if(dx >= p && dx <= q && dy >= r && dy <= s) acc++;
}
cout<<acc<<endl;
}
return ;
}
codevs1213 解的个数的更多相关文章
- 解的个数(codevs 1213)
题目描述 Description 已知整数x,y满足如下面的条件: ax+by+c = 0 p<=x<=q r<=y<=s 求满足这些条件的x,y的个数. 输入描述 Input ...
- 扩展gcd codevs 1213 解的个数
codevs 1213 解的个数 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 已知整数x,y满足如下面的条件: ax+by ...
- n元线性方程非负整数解的个数问题
设方程x1+x2+x3+...+xn = m(m是常数) 这个方程的非负整数解的个数有(m+n-1)!/((n-1)!m!),也就是C(n+m-1,m). 具体解释就是m个1和n-1个0做重集的全排列 ...
- codevs 1213 解的个数
1213 解的个数 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 已知整数x,y满足如下面的条件: ax+by+c = ...
- Codevs 1213 解的个数(exgcd)
1213 解的个数 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 已知整数x,y满足如下面的条件: ax+by+c=0 p< ...
- P1098 方程解的个数
题目描述 给出一个正整数N,请你求出x+y+z=N这个方程的正整数解的组数(1<=x<=y<=z<1000).其中,1<=x<=y<=z<=N . 输入 ...
- HDU1573 线性同余方程(解的个数)
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 计蒜客 18487.Divisions-大数的所有因子个数-Miller_Rabin+Pollard_rho-超快的(大数质因解+因子个数求解公式) (German Collegiate Programming Contest 2015 ACM-ICPC Asia Training League 暑假第一阶段第三场 F)
这一场两个和大数有关的题目,都用到了米勒拉宾算法,有点东西,备忘一下. 题目传送门 F. Divisions 传送门 这个题是求一个数的所有因子个数,但是数据比较大,1e18,所以是大数的题目,正常的 ...
- hdu4282 x^z+y^z+x*y*z=k 解的个数
题意: x^z + y^z + x*y*z = k; (x < y ,z > 1),给你一个k问有多少组解. 思路: 暴力枚举z,y,然后二分查找x.注意一点最好用 ...
随机推荐
- eccharts-gl 3D立体柱状图
echarts-gl继承于echarts echarts-gl官方实例https://echarts.baidu.com/examples/index.html#chart-type-globe 代码 ...
- (博弈论)51NOD 1066 Bash游戏
有一堆石子共有N个.A B两个人轮流拿,A先拿.每次最少拿1颗,最多拿K颗,拿到最后1颗石子的人获胜.假设A B都非常聪明,拿石子的过程中不会出现失误.给出N和K,问最后谁能赢得比赛. 例如N = 3 ...
- Linux 搭建Discuz论坛
title: Linux 搭建Discuz论坛 Welcome to Fofade's Blog! 这里是Linux 搭建论坛的一些命令记录 命令摘记: 下载文件:Discuz 安装环境:PHP Ap ...
- P4451 [国家集训队]整数的lqp拆分
#include <bits/stdc++.h> using namespace std; typedef long long LL; inline LL read () { LL res ...
- vue开发环境和生产环境里面解决跨域的几种方法
什么是跨域 跨域指浏览器不允许当前页面的所在的源去请求另一个源的数据.源指协议,端口,域名.只要这个3个中有一个不同就是跨域. 这里列举一个经典的列子: #协议跨域 http://a.baidu. ...
- 状态压缩+枚举 UVA 11464 Even Parity
题目传送门 /* 题意:求最少改变多少个0成1,使得每一个元素四周的和为偶数 状态压缩+枚举:枚举第一行的所有可能(1<<n),下一行完全能够由上一行递推出来,b数组保存该位置需要填什么 ...
- 构造 Codeforces Round #107 (Div. 2) B. Phone Numbers
题目传送门 /* 构造:结构体排个序,写的有些啰嗦,主要想用用流,少些了判断条件WA好几次:( */ #include <cstdio> #include <algorithm> ...
- 数据传递-------ajaxJson------spring3mvc中使用ajax传json中文乱码解决
参考来源:http://blog.csdn.net/dangerous_fire/article/details/25904225 第一种解决方法,适用所有情况 因为在controller中返回jso ...
- C# 生成 bmp 格式的图片
using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; usin ...
- 401 Binary Watch 二进制手表
详见:https://leetcode.com/problems/binary-watch/description/ C++: class Solution { public: vector<s ...