250 points:
You are going to send a message to your friend. The message is given as the stringmessage. To confuse potential eavesdroppers, you are going to scramble the message. Scrambling of a message is performed using the vector <int> key. If a letter is at the (0-based) position i in the original message, it will appear at the positionkey[i] in the scrambled message. (The constraints given below guarantee that this process will produce a valid scrambled message.) To make the encryption even more confusing, you are going to repeat the above processK times in a row. Given message, key, and the intK, find and return the final encrypted message. |
/*
"abcde"
{4, 3, 2, 1, 0}
1
Returns: "edcba"
*/
#include <iostream>
#include <fstream>
#include <cstring>
#include <climits>
#include <cmath>
#include <queue>
#include <ctime>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <algorithm>
typedef long long LL;
using namespace std;
class VerySecureEncryption {
public:
string encrypt( string message, vector <int> key, int K ) {
int sz=key.size();
string ans;
for(int i=0;i<K;i++)
{
for(int j=0;j<sz;j++)
ans[key[j]]=message[j];
for(int j=0;j<sz;j++)
message[j]=ans[j];
}
return message;
}
};
// Powered by FileEdit
// Powered by moj 4.12 [modified TZTester]
// Powered by CodeProcessor
600 points:
It's a bird! It's a plane! No, it's a square in a plane! Wait, is it really a square? There are four distinct points in the plane. You are given their coordinates in the vector <int>sx and y: for each i between 0 and 3, inclusive, there is a point at (x[i],y[i]). Return "It's a square" (quotes for clarity) if the four points are the vertices of a square. Otherwise, return "Not a square". |
上次BC看到一個較好的方法:連上對角兩條邊共六條邊排序,前4條邊相等,後兩條邊相等前是前4條邊平方的2倍。
/*
{0, 0, 2, 2}
{0, 2, 0, 2}
Returns: "It's a square"
*/
#include <iostream>
#include <fstream>
#include <cstring>
#include <climits>
#include <cmath>
#include <queue>
#include <ctime>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <algorithm>
typedef long long LL;
using namespace std;
double A[10];
double dis(int x1,int y1,int x2,int y2)
{
double x=x1-x2;
double y=y1-y2;
return x*x+y*y;
}
class IsItASquare {
public:
string isSquare( vector <int> x, vector <int> y ) {
int tot=0;
for(int i=0;i<4;i++)
for(int j=i+1;j<4;j++)
A[tot++]=dis(x[i],y[i],x[j],y[j]);
sort(A,A+tot);
int flag;
if(A[0]==A[1]&&A[1]==A[2]&&A[2]==A[3]&&A[4]==A[5]&&A[5]==2*A[3])
flag=1;
else
flag=0;
return flag?"It's a square":"Not a square";
}
};
// Powered by FileEdit
// Powered by moj 4.12 [modified TZTester]
// Powered by CodeProcessor
1000 points:
One day, Bob the Coder was wondering whether abstract programming problems can have applications in practice. The next day, he was selected to be on a quiz show. He will win one million dollars if he answers the following question: Given a vector <int> A with N elements and an int K, count the number of tuples (p, q, r) such that 0 <= p < q < r < N andA[p] * A[q] * A[r] is divisible byK. Please compute and return the answer to Bob's question. |
做下預處理
/*
-
A will contain between 3 and 2,000 elements, inclusive.
K will be between 1 and 1,000,000, inclusive.
Each element of A will be between 1 and 100,000,000, inclusive.
{4, 5, 2, 25}
100
Returns: 2
*/
#include <iostream>
#include <fstream>
#include <cstring>
#include <climits>
#include <cmath>
#include <queue>
#include <ctime>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <algorithm>
typedef long long LL;
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
LL sum[1010][2010];
int b[2010];
LL a[2020];
class AnArray {
public:
int solveProblem( vector <int> A, int K ) {
CLEAR(sum,0);
int tot=0;
int n=A.size();
REP(i,n) a[i+1]=1LL*A[i];
for(int i=1;i<=K;i++)
{
if(K%i) continue;
b[tot]=i;
for(int j=1;j<=n;j++)
{
if(a[j]%i==0)
sum[tot][j]=1;
sum[tot][j]+=sum[tot][j-1];
}
tot++;
}
int ans=0;
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
LL x=a[i]*a[j];
int t=K/__gcd(1LL*K,x);
int pos=lower_bound(b,b+tot,t)-b;
ans+=sum[pos][n]-sum[pos][j];
}
}
return ans;
}
};
// Powered by FileEdit
// Powered by moj 4.12 [modified TZTester]
// Powered by CodeProcessor