Codeforces 417E Square Table(随机算法)
来源:互联网 发布:赛鸽记录软件 编辑:程序博客网 时间:2024/06/06 12:52
转载自:http://blog.csdn.net/keshuai19940722/article/details/24105103,多谢这位给的思路
今天学了个新姿势,随机算法。
有时简单粗暴的办法往往胜于苦心经营的办法。后者往往还出不来结果呢。
题目大意:给出n和m,要求给出一个矩阵,要求每一列每一行的元素的平方总和是一个平方数。
解题思路:构造,按照
a a a b
a a a b
a a a b
c c c d
的方式取构造,然后a,b,c,d的值用随机生成数去枚举,不过我觉得用暴力也是可以的。
用随机算法做,218ms
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> bool judge (int s) { int k = sqrt(s); return k * k == s; } int main () { int n, m; int a, b, c, d; scanf("%d%d", &n, &m); while (true) { a = rand()%100 + 1; b = rand()%100 + 1; c = rand()%100 + 1; d = rand()%100 + 1; if (judge(a * a * (m-1) + b * b) && judge(a * a * (n-1) + c * c) && judge(b * b * (n-1) + d * d) && judge(c * c * (m-1) + d * d) ) break; } for (int i = 1; i < n; i++) { for (int j = 1; j < m; j++) printf("%d ", a); printf("%d\n", b); } for (int i = 1; i < m; i++) printf("%d ", c); printf("%d\n", d); return 0; }
不用随机算法做,46ms
//Hello. I'm Peter.#include<cstdio>#include<iostream>#include<sstream>#include<iomanip>#include<cstring>#include<string>#include<cmath>#include<cstdlib>#include<algorithm>#include<functional>#include<cctype>#include<ctime>#include<stack>#include<queue>#include<deque>#include<vector>#include<set>#include<map>#include<limits>using namespace std;#define input freopen("data.txt","r",stdin)#define output freopen("output.txt","r",stdin)#define INT (0x3f3f3f3f)*2#define LL (0x3f3f3f3f3f3f3f3f)*2#define clr(a) memset(a,0,sizeof(a))#define clr_minus1(a) memset(a,-1,sizeof(a))#define clr_NULL(a) memset(a,NULL,sizeof(a))#define clr_INT(a) memset(a,INT,sizeof(a))#define clr_true(a) memset(a,true,sizeof(a))#define clr_false(a) memset(a,false,sizeof(a))#define clr_queue(q) while(!q.empty()) q.pop()#define clr_stack(s) while(!s.empty()) s.pop()#define rep(i, a, b) for (int i = a; i < b; i++)#define dep(i, a, b) for (int i = a; i > b; i--)#define repin(i, a, b) for (int i = a; i <= b; i++)#define depin(i, a, b) for (int i = a; i >= b; i--)#define ll long long#define eps#define MOD#define MAXN 100#define N#define Mbool is_square(int x){ int tt=(int)sqrt((double)x); if(tt*tt==x) return true; else return false;}int main(){ int n,m; cin>>n>>m; bool ok=false; int ansa=0,ansb=0,ansc=0,ansd=0; repin(d,1,MAXN) { if(ok) break; repin(a,1,MAXN) { if(ok) break; repin(b,1,MAXN) { if(ok) break; repin(c,1,MAXN) { if(is_square((m-1)*a*a+b*b) && is_square((m-1)*c*c+d*d) && is_square((n-1)*a*a+c*c) && is_square((n-1)*b*b+d*d) ) { ok=true; ansa=a; ansb=b; ansc=c; ansd=d; break; } } } } } repin(i,1,n-1) { repin(j,1,m-1) { printf("%d ",ansa); } printf("%d\n",ansb); } repin(i,1,m-1) { printf("%d ",ansc); } printf("%d\n",ansd);}
0 0
- Codeforces 417E Square Table(随机算法)
- Codeforces 417E Square Table(随机算法)
- codeforces 418 C Square Table (随机算法)
- codeforces E Square Table
- [构造 随机乱搞] Codeforces Gym 100608 ASC 47 E. Elegant Square
- Codeforces Round #192 (Div. 2) E. Graph Reconstruction (随机算法)
- Codeforces 432E Square Tiling(构造+贪心)
- codeforces 432E Square Tiling 贪心
- Codeforces 612 E Square Root of Permutation
- Codeforces 612E - Square Root of Permutation
- codeforces 432E Square Tiling(构造)
- Codeforces 651E:Table Compression
- Codeforces 612E Square Root of Permutation 【置换群】
- CodeForces 612e Square Root of Permutation(置换+构造)
- Codeforces Round #356 (Div. 2)E. Bear and Square Grid
- Codeforces 40E Number Table (计数)
- CodeForces 663E - Binary Table FWT
- Codeforces 651E Table Compression 拓扑序
- [Linux 命令] date
- Cyborg Genes - UVa 10723 dp
- mfc中,当点击一个button后,如何使其它某些button变为不可用啊?也就是变灰!
- UIApplicationl类
- JDK源码学习系列08----HashMap
- Codeforces 417E Square Table(随机算法)
- VirtualBox 虚拟机的网络设置
- ZOJ-1290
- 【4920Matrix multiplication】矩阵乘法优化+输入挂
- MySql截取DateTime字段的日期值
- OpenMAX介绍(总括)
- [WebGL入门]十一,着色器的编译和连接
- 蚊子132来到一个大型互联网公司
- xml的两种解析方式