博客
关于我
解方程
阅读量:306 次
发布时间:2019-03-03

本文共 2599 字,大约阅读时间需要 8 分钟。


哈希解法:

#include <iostream>#include<cstring>#include<cmath>#define ll long longusing namespace std;const int maxn=1e6+7;int hash1[maxn],hash2[maxn];int main(int argc, char** argv) {   	int a,b,c,d;	while(~scanf("%d%d%d%d",&a,&b,&c,&d)){   		if((a>0&&b>0&&c>0&&d>0)||(a<0&&b<0&&d<0&&c<0)){   			printf("0\n");			continue;		} 		int ans=0;		memset(hash1,0,sizeof(hash1));		memset(hash2,0,sizeof(hash2));		for(int i=1;i<=100;++i){   			for(int j=1;j<=100;++j){   				int x=a*i*i+b*j*j;				if(x>=0) hash1[x]++;				else hash2[-x]++;			}		}		for(int i=1;i<=100;++i){   			for(int j=1;j<=100;++j){   				int x=c*i*i+d*j*j;				if(x>0) ans+=hash2[x];				else ans+=hash1[-x];			}		}		printf("%d\n",ans*16);	}	return 0;}

二分能用stl的就哟个stl,自己写容易炸

二分解方程

#include<bits/stdc++.h>using namespace std;const int N = 1e5 + 9;int x[N], y[N];int a, b, c, d;int main(){   	while(~scanf("%d %d %d %d", &a, &b, &c, &d))	{   		if(a>0&&b>0&&c>0&&d>0 || a<0&&b<0&&c<0&&d<0)		{   			cout << "0\n";continue;		}		long long ans = 0;		int cnt = 0;		for(int i = 1; i <= 100; ++i)			for(int j = 1; j <= 100; ++j)				x[cnt] = a*i*i + b*j*j,				y[cnt++] = c*i*i + d*j*j;		sort(x,x+cnt);		sort(y,y+cnt);		for(int i = 0; i < cnt; ++i)		{   			int l = 0, r = cnt - 1;			while(l < r)			{   				int mid = (l + r) / 2;				if(x[i] + y[mid] < 0) l = mid + 1;				else r = mid;			}			long long tmp = 0;			if(x[i] + y[r] == 0)			{   				++tmp;				int k = r-1;				while(y[k] == y[r] && k >= 0) --k,++tmp;				k = r + 1;				while(y[k] == y[r] && k < cnt) ++k, ++tmp;				ans += tmp;				while(x[i] == x[i+1] && i+1 < k) ans+=tmp, ++i;			}		}		printf("%lld\n",ans<<4);		// ans是4个数全部正的情况		// 4个数可正可负  , 2的4次方	}	return 0;}

#include<bits/stdc++.h>using namespace std;const int N = 1e7 + 9;int x[N], y[N];int main(){   	int t, n, k;	cin >> t;	while(t--)	{   		long long ans = 0;		int cnt = 0;		cin >> n >> k;		for(int i = -n; i <= n; ++i)			for(int j = -n; j <= n; ++j)				x[cnt] = i * i + i + j * j + j,				y[cnt++] = k * (i * i + i + j * j + j);		sort(x,x+cnt);		sort(y,y+cnt);		//for(int i = 0; i < cnt; ++i) cout << x[i] << " ";cout << endl;		//for(int i = 0; i < cnt; ++i) cout << y[i] << " ";cout << endl;		for(int i = 0; i < cnt; ++i)		{   			int pos = lower_bound(y,y+cnt, x[i]) - y;			if(pos == cnt)			{   				while(x[i] == x[i+1]) ++i;continue;			}			long long tmp = 0;			if(y[pos] == x[i])			{   				++tmp;				int k = pos - 1;				while(y[pos] == y[k] && k >= 0) --k, ++tmp;				k = pos + 1;				while(y[pos] == y[k] && k < cnt) ++k, ++tmp;				ans += tmp;				while(x[i] == x[i+1]) ans += tmp, ++i; 			}		}		cout << ans << endl;	}	return 0;}/*31 11 21 3*/

转载地址:http://twfm.baihongyu.com/

你可能感兴趣的文章
mysql generic安装_MySQL 5.6 Generic Binary安装与配置_MySQL
查看>>
Mysql group by
查看>>
MySQL I 有福啦,窗口函数大大提高了取数的效率!
查看>>
mysql id自动增长 初始值 Mysql重置auto_increment初始值
查看>>
MySQL in 太多过慢的 3 种解决方案
查看>>
MySQL InnoDB 三大文件日志,看完秒懂
查看>>
Mysql InnoDB 数据更新导致锁表
查看>>
Mysql Innodb 锁机制
查看>>
MySQL InnoDB中意向锁的作用及原理探
查看>>
MySQL InnoDB事务隔离级别与锁机制深入解析
查看>>
Mysql InnoDB存储引擎 —— 数据页
查看>>
Mysql InnoDB存储引擎中的checkpoint技术
查看>>
Mysql InnoDB存储引擎中缓冲池Buffer Pool、Redo Log、Bin Log、Undo Log、Channge Buffer
查看>>
MySQL InnoDB引擎的锁机制详解
查看>>
Mysql INNODB引擎行锁的3种算法 Record Lock Next-Key Lock Grap Lock
查看>>
mysql InnoDB数据存储引擎 的B+树索引原理
查看>>
mysql innodb通过使用mvcc来实现可重复读
查看>>
mysql insert update 同时执行_MySQL进阶三板斧(三)看清“触发器 (Trigger)”的真实面目...
查看>>
mysql interval显示条件值_MySQL INTERVAL关键字可以使用哪些不同的单位值?
查看>>
Mysql join原理
查看>>