Topcoder SRM 668 DIV 2
VerySecureEncryption 模拟
题意:
给你个串message,然后一个置换key,输出置换K次后的结果。
题解:
直接模拟就好。
代码:
#include<iostream> #include<vector> #include<string> #include<algorithm> using namespace std; class VerySecureEncryption { public: string encrypt(string message, vector<int> key, int K) { char res[2][12]; for (int i = 0; i < message.length(); i++)res[1][i] = message[i]; for (int i = 0; i < K; i++) for (int j = 0; j < message.length(); j++) res[i & 1][key[j]] = res[(i + 1) & 1][j]; string r; for (int i = 0; i < message.length(); i++) r = r + res[(K - 1) & 1][i]; return r; } };
IsItASquare 计算几何
题意:
给你平面上四个点,问你是否能够构成正方形
题解:
取三个点,看是否能够构成等腰直角三角形,然后再check一下最后一个点。
代码:
#include<iostream> #include<vector> #include<string> #include<algorithm> using namespace std; class Coordinate { public: double xCoordinate; double yCoordinate; Coordinate(double x = 0,double y = 0) { this->xCoordinate = x; this->yCoordinate = y; } bool operator!=(Coordinate const &comp) const { return (this->xCoordinate != comp.xCoordinate || this->yCoordinate != comp.yCoordinate); } }; /** * @explanation 判断是否为等腰直角三角形. */ bool Judge(Coordinate const x,Coordinate const y,Coordinate const z) { Coordinate *mVector = new Coordinate(x.xCoordinate - y.xCoordinate, x.yCoordinate - y.yCoordinate); Coordinate *nVector = new Coordinate(z.xCoordinate - (x.xCoordinate + y.xCoordinate)/2, z.yCoordinate - (x.yCoordinate + y.yCoordinate)/2); //判断是否为等腰三角形 bool result = ((mVector->xCoordinate * nVector->xCoordinate + mVector->yCoordinate * nVector->yCoordinate) == 0); //判断是否是直角三角形 if(result) result = (mVector->xCoordinate * mVector->xCoordinate + mVector->yCoordinate * mVector->yCoordinate) == ((nVector->xCoordinate * nVector->xCoordinate + nVector->yCoordinate * nVector->yCoordinate) * 4); delete mVector; delete nVector; return result; } bool IsSquare(Coordinate *array,int length) { if(length != 4) return false; int a,b,c; if(Judge(array[0],array[1],array[2])) { a = 0; b = 1; c = 2; } else if(Judge(array[0],array[2],array[1])) { a = 0; b = 2; c = 1; } else if(Judge(array[2],array[1],array[0])) { a = 1; b = 2; c = 0; } else return false; return (array[3] != array[c] && Judge(array[a],array[b],array[3])); } class IsItASquare { public: string isSquare(vector <int> x, vector <int> y) { Coordinate ar[4]; for(int i=0;i<4;i++)ar[i].xCoordinate=x[i],ar[i].yCoordinate=y[i]; return IsSquare(ar,4)?"It's a square":"Not a square"; } };
AnArra 乱搞
题意:
给你个序列A,统计A[i]*A[j]*A[k]%K==0的个数,i<j<k
题解:
采用存一半搜一半的思想,预处理出每个因子有多少倍数。然后枚举A[i],A[j],令g=gcd(A[i]*A[j],K),t=K/g,那么问题就是有多少数是t的倍数,这个刚刚已经预处理好了。
代码:
#include<iostream> #include<map> #include<algorithm> using namespace std; typedef long long ll; int ma[1000006]; ll gcd(ll a,ll b) { return b == 0 ? a : gcd(b, a % b); } class AnArray { public: int solveProblem(vector<int> A, int K) { for (auto a:A) { for (int i = 1; i * i <= a; i++) { if (a % i == 0) { ma[i]++; int t = a / i; if (i * i != a && t <= 1000000)ma[t]++; } } } ll res = 0; for (int i = 0; i < A.size(); i++) for (int j = 0; j < A.size(); j++) { if (i == j)continue; ll tmp = 1; tmp = tmp * A[i] * A[j]; int g = gcd(tmp, K); int t = K / g; res = res + ma[K / g]; if (A[i] % t == 0)res--; if (A[j] % t == 0)res--; } return res / 6; } };