【CF】207 Div.1 B.Xenia and Hamming
这题目一看很牛逼,其实非常easy。求求最小公倍数,最大公约数,均摊复杂度其实就是O(n)。
1 /* 356B */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #include <vector> 9 #include <deque> 10 #include <algorithm> 11 #include <cstdio> 12 #include <cmath> 13 #include <ctime> 14 #include <cstring> 15 #include <climits> 16 #include <cctype> 17 #include <cassert> 18 #include <functional> 19 #include <iterator> 20 #include <iomanip> 21 using namespace std; 22 //#pragma comment(linker,"/STACK:102400000,1024000") 23 24 #define sti set<int> 25 #define stpii set<pair<int, int> > 26 #define mpii map<int,int> 27 #define vi vector<int> 28 #define pii pair<int,int> 29 #define vpii vector<pair<int,int> > 30 #define rep(i, a, n) for (int i=a;i<n;++i) 31 #define per(i, a, n) for (int i=n-1;i>=a;--i) 32 #define clr clear 33 #define pb push_back 34 #define mp make_pair 35 #define fir first 36 #define sec second 37 #define all(x) (x).begin(),(x).end() 38 #define SZ(x) ((int)(x).size()) 39 #define lson l, mid, rt<<1 40 #define rson mid+1, r, rt<<1|1 41 42 const int maxn = 1e6+5; 43 char s[maxn], d[maxn]; 44 int cs[26]; 45 int cd[26]; 46 47 int main() { 48 ios::sync_with_stdio(false); 49 #ifndef ONLINE_JUDGE 50 freopen("data.in", "r", stdin); 51 freopen("data.out", "w", stdout); 52 #endif 53 54 __int64 n, m; 55 56 scanf("%I64d %I64d", &n, &m); 57 scanf("%s %s", s, d); 58 int slen = strlen(s); 59 int dlen = strlen(d); 60 61 int g = __gcd(slen, dlen); 62 __int64 lcm = 1LL * slen / g * dlen; 63 __int64 n_ = lcm / slen; 64 __int64 m_ = lcm / dlen; 65 66 #ifndef ONLINE_JUDGE 67 printf("n_ = %I64d, m_ = %I64d\n", n_, m_); 68 #endif 69 70 __int64 t = n / n_; 71 __int64 tot; 72 73 __int64 ans = 0; 74 int i, j, k; 75 76 for (i=0; i<g; ++i) { 77 // count s 78 memset(cs, 0, sizeof(cs)); 79 for (j=i,tot=0; j<slen; j+=g,++tot) { 80 ++cs[s[j]-'a']; 81 } 82 for (j=i; j<dlen; j+=g) { 83 ans += (tot - cs[d[j]-'a']); 84 } 85 } 86 87 ans *= t; 88 printf("%I64d\n", ans); 89 90 #ifndef ONLINE_JUDGE 91 printf("time = %d.\n", (int)clock()); 92 #endif 93 94 return 0; 95 }