您的位置:首页 > 其它

【t050】方程求解

2017-10-04 18:45 190 查看

Time Limit: 1 second
Memory Limit: 128 MB

【问题描述】

要求Xi(i = 1,2,3,4)是一个[-T..T]中的整数,满足方程AX1 + BX2 + CX3 + DX4 = P的解有多少组?
【输入格式】

六个整数分别表示P、T、A、B、C、D。
【输出格式】

一个整数表示方程的解的个数。
|P| < 109
|A| < 104
|B| < 104
|C| < 104
|D| < 104
0 <= T <= 500

Sample Input

0 10 -1 -1 0 -1

Sample Output

6951

【题目链接】:http://noi.qz5z.com/viewtask.asp?id=t050

【题解】

可以把等式移项一下;
得到
Ax1+Bx2=p-cx3-dx4
则我们可以先两层循环枚举x1,x2获得Ax1+Bx2;
存在map里面;
然后再两层循环枚举x3,x4,获得p-cx3-dx4;
看看和p-cx3-dx4相同的有多少个;有多少个则就有相应的多少个解;递增答案就好^_^;
时间复杂度O(2*(2T)^2);

【完整代码】

#include <cstdio>
#include <cmath>
#include <map>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int MAXN = 110;

int p,t,a,b,c,d,ans = 0;
map <int,int> dic;

int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(p);rei(t);rei(a);rei(b);rei(c);rei(d);
rep1(x1,-t,t)
rep1(x2,-t,t)
dic[x1*a+b*x2]++;
rep1(x3,-t,t)
rep1(x4,-t,t)
ans+=dic[p-c*x3-d*x4];
printf("%d\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: