Submission #1012576

# Submission time Handle Problem Language Result Execution time Memory
1012576 2024-07-02T11:03:08 Z Jawad_Akbar_JJ Sateliti (COCI20_satellti) C++17
110 / 110
902 ms 37716 KB
#include <iostream>

using namespace std;
#define int long long
const int N = 2005;
int calc[N][N], p = 101, q = 727, mod = 1e9 + 7, n, m;
int pP[N], pInv[N];
int qP[N], qInv[N];
char a[N][N];

int power(int a,int b){
	if (b == 1)
		return a;
	int ans = power(a,b / 2);
	ans = ans * ans % mod;
	if (b % 2)
		ans = ans * a % mod;
	return ans;
}

int get1(int i,int j,int k){
	int rs = k / m;
	if (rs == 0)
		return 0;
	int i2 = i + rs - 1;
	int j2 = j + m - 1;
	int Ans = calc[i2][j2] - calc[i2][j-1] - calc[i-1][j2] + calc[i-1][j-1];
	return ((Ans % mod) + mod) % mod;
}
int get2(int i,int j,int k){
	i += k / m;
	k %= m;
	if (k == 0)
		return 0;
	int i2 = i;
	int j2 = j + k - 1;
	int Ans = calc[i2][j2] - calc[i2][j-1] - calc[i-1][j2] + calc[i-1][j-1];
	return ((Ans % mod) + mod) % mod;
}

int get(int i,int j,int k){
	int Ans = get1(i,j,k) + get2(i,j,k);
	Ans = Ans * pInv[i-1] % mod;
	Ans = Ans * qInv[j-1] % mod;
	return Ans;
}

signed main(){

	pP[0] = qP[0] = pInv[0] = qInv[0] = 1;

	for (int i=1;i<N;i++){
		pP[i] = pP[i-1] * p % mod;
		qP[i] = qP[i-1] * q % mod;
		pInv[i] = power(pP[i],mod - 2);
		qInv[i] = power(qP[i],mod - 2);
	}

	cin>>n>>m;

	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++){
			cin>>a[i][j];
			a[i + n][j] = a[i][j + m] = a[i + n][j + m] = a[i][j];
		}

	for (int i=1;i<=2*n;i++)
		for (int j=1;j<=2*m;j++){
			calc[i][j] = calc[i-1][j] + calc[i][j-1] - calc[i-1][j-1];
			calc[i][j] += (pP[i] * qP[j] % mod) * a[i][j] % mod;
			calc[i][j] %= mod;
		}

	int bst_i = 1,bst_j = 1;

	for (int i=1;i<=n;i++){
		for (int j=1;j<=m;j++){
			if (get(i,j,n * m) == get(bst_i, bst_j, n * m))
				continue;
			int l = 0,r = n * m;

			while (l + 1 < r){
				int mid = (l + r) / 2;
				if (get(i,j,mid) == get(bst_i, bst_j, mid))
					l = mid;
				else
					r = mid;
			}
			int ja = -1 + r % m;
			if (ja == -1)
				ja += m;
			if (a[i + l / m][j + ja] < a[bst_i + l / m][bst_j + ja])
				bst_i = i,bst_j = j;
		}
	}

	for (int i=bst_i;i<bst_i + n;i++){
		for (int j=bst_j;j<bst_j + m;j++)
			cout<<a[i][j];
		cout<<'\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1112 KB Output is correct
2 Correct 3 ms 1116 KB Output is correct
3 Correct 2 ms 1116 KB Output is correct
4 Correct 2 ms 1116 KB Output is correct
5 Correct 2 ms 1116 KB Output is correct
6 Correct 2 ms 1116 KB Output is correct
7 Correct 2 ms 1032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1112 KB Output is correct
2 Correct 3 ms 1116 KB Output is correct
3 Correct 2 ms 1116 KB Output is correct
4 Correct 2 ms 1116 KB Output is correct
5 Correct 2 ms 1116 KB Output is correct
6 Correct 2 ms 1116 KB Output is correct
7 Correct 2 ms 1032 KB Output is correct
8 Correct 58 ms 6796 KB Output is correct
9 Correct 3 ms 824 KB Output is correct
10 Correct 2 ms 3932 KB Output is correct
11 Correct 59 ms 6792 KB Output is correct
12 Correct 69 ms 6908 KB Output is correct
13 Correct 59 ms 7000 KB Output is correct
14 Correct 67 ms 7000 KB Output is correct
15 Correct 58 ms 6916 KB Output is correct
16 Correct 67 ms 7008 KB Output is correct
17 Correct 71 ms 7000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1112 KB Output is correct
2 Correct 3 ms 1116 KB Output is correct
3 Correct 2 ms 1116 KB Output is correct
4 Correct 2 ms 1116 KB Output is correct
5 Correct 2 ms 1116 KB Output is correct
6 Correct 2 ms 1116 KB Output is correct
7 Correct 2 ms 1032 KB Output is correct
8 Correct 58 ms 6796 KB Output is correct
9 Correct 3 ms 824 KB Output is correct
10 Correct 2 ms 3932 KB Output is correct
11 Correct 59 ms 6792 KB Output is correct
12 Correct 69 ms 6908 KB Output is correct
13 Correct 59 ms 7000 KB Output is correct
14 Correct 67 ms 7000 KB Output is correct
15 Correct 58 ms 6916 KB Output is correct
16 Correct 67 ms 7008 KB Output is correct
17 Correct 71 ms 7000 KB Output is correct
18 Correct 773 ms 37564 KB Output is correct
19 Correct 14 ms 12632 KB Output is correct
20 Correct 12 ms 1208 KB Output is correct
21 Correct 749 ms 37544 KB Output is correct
22 Correct 902 ms 37560 KB Output is correct
23 Correct 718 ms 37712 KB Output is correct
24 Correct 895 ms 37716 KB Output is correct
25 Correct 746 ms 37716 KB Output is correct
26 Correct 789 ms 37716 KB Output is correct
27 Correct 893 ms 37504 KB Output is correct