QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#225671 | #7518. GCD of Pattern Matching | PetroTarnavskyi | WA | 0ms | 3652kb | C++17 | 1.5kb | 2023-10-24 22:41:04 | 2023-10-24 22:41:05 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
mt19937 rng(7447);
const int N = 256;
int mp[N];
void solve()
{
int m;
string s;
cin >> m >> s;
VI v(m);
iota(ALL(v), 0);
vector<ULL> pw(SZ(s));
pw.back() = 1;
RFOR (i, SZ(s) - 1, 0)
pw[i] = pw[i + 1] * m;
vector<int> alp;
FOR (i, 0, SZ(s))
alp.PB(s[i] - 'a');
sort(ALL(alp));
alp.resize(unique(ALL(alp)) - alp.begin());
ULL g = 0;
if (m > SZ(alp))
{
FOR (i, 0, SZ(alp))
{
ULL num = 0;
FOR (j, 0, SZ(s))
{
if (s[j] - 'a' == alp[i])
num += pw[j];
}
g = gcd(g, num);
}
}
ULL x = 0;
FOR (i, 0, SZ(s))
{
if (s[i] - 'a' == alp[0])
x += pw[i];
}
FOR (i, 1, SZ(alp))
{
ULL y = 0;
FOR (j, 0, SZ(s))
{
if (s[j] - 'a' == alp[i])
y += pw[j];
}
if (y > x)
y -= x;
else
y = x - y;
g = gcd(g, y);
}
cout << g << '\n';
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(15);
fill(mp, mp + N, -1);
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3596kb
input:
5 10 ccpcccpc 10 cpcpcp 10 cpc 4 cpccpc 4 dhcp
output:
10001 10101 1 65 3
result:
ok 5 number(s): "10001 10101 1 65 3"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 3652kb
input:
30 2 ab 3 abc 4 abcd 5 abcde 6 abcdef 7 abcdefg 8 abcdefgh 9 abcdefghi 10 abcdefghij 11 abcdefghijk 12 abcdefghijkl 13 abcdefghijklm 14 abcdefghijklmn 15 abcdefghijklmno 16 abcdefghijklmnop 16 a 16 ab 16 abc 16 abcd 16 abcde 16 abcdef 16 abcdefg 16 abcdefgh 16 abcdefghi 16 abcdefghij 16 abcdefghijk ...
output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
result:
wrong answer 1st numbers differ - expected: '2', found: '1'