cf D. Xenia and Hamming

http://codeforces.com/contest/357/problem/D

题意:给你两个数n和m,表示两个字符串的循环次数,然后给出两个字符串,求出其相同位置字符不同的个数。

先求出两个字符串长度的最大公约数和最小公倍数,然后求出在最小公倍数范围内的不同字符的个数,后面的和前面的一样,最终的个数也就求出了。

 1     #include <cstdio>
 2     #include <cstring>
 3     #include <algorithm>
 4     #define LL __int64
 5     using namespace std;
 6     char s1[10010000],s2[10010000];
 7 
 8     int count1[1000010][26];
 9 
10     LL GCD(LL a,LL b)
11     {
12         return b==0?a:GCD(b,a%b);
13     }
14 
15     int main()
16     {
17         LL n,m;
18         while(scanf("%I64d%I64d",&n,&m)!=EOF)
19         {
20             scanf("%s",s1);
21             scanf("%s",s2);
22             LL k1=(LL)strlen(s1);
23             LL k2=(LL)strlen(s2);
24             LL c=GCD(k1,k2);
25             LL m=k1*k2/c;
26             LL m1=m;
27             for(int i=0; i<k1; i++)
28             {
29                 count1[i%c][s1[i]-'a']++;
30             }
31             for(int j=0; j<k2; j++)
32             {
33                 m-=count1[j%c][s2[j]-'a'];
34             }
35             printf("%I64d
",m*((k1*n)/m1));
36         }
37         return 0;
38     }
View Code
原文地址:https://www.cnblogs.com/fanminghui/p/3935584.html