这题目一看很牛逼,其实非常easy。求求最小公倍数,最大公约数,均摊复杂度其实就是O(n)。

 /* 356B */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = 1e6+;
char s[maxn], d[maxn];
int cs[];
int cd[]; int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif __int64 n, m; scanf("%I64d %I64d", &n, &m);
scanf("%s %s", s, d);
int slen = strlen(s);
int dlen = strlen(d); int g = __gcd(slen, dlen);
__int64 lcm = 1LL * slen / g * dlen;
__int64 n_ = lcm / slen;
__int64 m_ = lcm / dlen; #ifndef ONLINE_JUDGE
printf("n_ = %I64d, m_ = %I64d\n", n_, m_);
#endif __int64 t = n / n_;
__int64 tot; __int64 ans = ;
int i, j, k; for (i=; i<g; ++i) {
// count s
memset(cs, , sizeof(cs));
for (j=i,tot=; j<slen; j+=g,++tot) {
++cs[s[j]-'a'];
}
for (j=i; j<dlen; j+=g) {
ans += (tot - cs[d[j]-'a']);
}
} ans *= t;
printf("%I64d\n", ans); #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
05-08 08:21