先上题目:

D. Xenia and Hamming
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Xenia is an amateur programmer. Today on the IT lesson she learned about the Hamming distance.

The Hamming distance between two strings s = s1s2... sn and t = t1t2... tn of equal length n is value . Record [si ≠ ti] is the Iverson notation and represents the following: if si ≠ ti, it is one, otherwise — zero.

Now Xenia wants to calculate the Hamming distance between two long strings a and b. The first string a is the concatenation of n copies of string x, that is, . The second string b is the concatenation of m copies of string y.

Help Xenia, calculate the required Hamming distance, given n, x, m, y.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 1012). The second line contains a non-empty string x. The third line contains a non-empty string y. Both strings consist of at most 106 lowercase English letters.

It is guaranteed that strings a and b that you obtain from the input have the same length.

Output

Print a single integer — the required Hamming distance.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.

Sample test(s)
input
100 10
a
aaaaaaaaaa
output
0
input
1 1
abacaba
abzczzz
output
4
input
2 3
rzr
az
output
5
Note

In the first test case string a is the same as string b and equals 100 letters a. As both strings are equal, the Hamming distance between them is zero.

In the second test case strings a and b differ in their 3-rd, 5-th, 6-th and 7-th characters. Thus, the Hamming distance equals 4.

In the third test case string a is rzrrzr and string b is azazaz. The strings differ in all characters apart for the second one, the Hamming distance between them equals 5.

  题意:给出两个串的循环节以及循环次数,求这两个串的汉明距离,这里的汉明距离指的是对应位置的字符如果不一样就是加1。

  这里的数据有点大,循环节的长度就有10^6,循环次数最大10^12,所以不能直接暴搜。这里的做法是先求出两个循环节的最小公倍数l和最大公约数g。然后统计面每个字符串某个位置i是哪个字符,同时保存在i%g的位置,因为在一个l的范围里面只有在g的倍数的位置两个字符串的字符才会有比较,然后统计一下l的范围里面两个字符串每一个位置的不同的字符的对数。然后因为最大公约数l的距离以后字符串的异同又会和前面一样,所以我们只要再乘以一个字符串的总长度对于l的倍数就可以了。

  但是这里需要优化,因为比较同一个位置的字符异同的时候比较次数是26*26-26如果再乘上g的话有可能会非常大,这样就会超时,所以我们需要考虑它的反面,我们可以用一个字符串的总长度剪去位置上有相同字符的数量,这样我们就可以减少一维变成g*26了。

  但是这样做可能还是会wa,因为我们还需要 注意到数据大小(10^6)*(10^12)快要到达long long 的极限了,如果我们是先用前面的到的结果ans先乘上这里的数再做后面的除法的话会有溢出的可能,所以我们需要先求了字符串的总长度对于l的倍数,然后再将ans乘以倍数,这样就不会爆long long了。

上代码:

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX 1000002
#define ll long long
using namespace std; char a[MAX],b[MAX];
ll n,m,r,le;
ll la,lb,g,l,ans;
int dpa[MAX][];
int dpb[MAX][]; ll gcd(ll a,ll b){
return b== ? a : gcd(b,a%b);
} int main()
{
//freopen("data.txt","r",stdin);
while(scanf("%I64d %I64d",&n,&m)!=EOF){
scanf("%s",a);
scanf("%s",b);
la=strlen(a);
lb=strlen(b);
g = a>b ? gcd(la,lb) : gcd(lb,la);
l=la*lb/g;
memset(dpa,,sizeof(dpa));
memset(dpb,,sizeof(dpb));
for(int i=;i<la;i++){
dpa[i%g][a[i]-'a']++;
}
for(int i=;i<lb;i++){
dpb[i%g][b[i]-'a']++;
}
ans=;
for(int i=;i<g;i++){
for(int j=;j<;j++){
ans+=(ll)dpa[i][j]*(ll)dpb[i][j];
}
}
ans=n*la/l*ans;
ans=n*la-ans;
printf("%I64d\n",ans);
}
return ;
}

/*357D*/

CodeForces - 357D - Xenia and Hamming的更多相关文章

  1. Codeforces Round #207 (Div. 1) B. Xenia and Hamming(gcd的运用)

    题目链接: B. Xenia and Hamming 题意: 要求找到复制后的两个字符串中不同样的字符 思路: 子问题: 在两串长度是最大公倍数的情况下, 求出一个串在还有一个串中反复字符的个数 CO ...

  2. codeforces B. Xenia and Spies 解题报告

    题目链接:http://codeforces.com/problemset/problem/342/B 题目意思:有n个spy,编号从1-n,从左到右排列.现在的任务是,spy s要把信息传递到spy ...

  3. codeforces A. Xenia and Divisors 解题报告

    题目链接:http://codeforces.com/problemset/problem/342/A 题目意思:给出n个数,找出n/3个组且每组有3个数,这三个数必须要符合两个条件:1.a < ...

  4. codeforces B. Xenia and Ringroad 解题报告

    题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...

  5. cf D. Xenia and Hamming

    http://codeforces.com/contest/357/problem/D 题意:给你两个数n和m,表示两个字符串的循环次数,然后给出两个字符串,求出其相同位置字符不同的个数. 先求出两个 ...

  6. codeforces 339C Xenia and Bit Operations(线段树水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Xenia and Bit Operations Xenia the beginn ...

  7. codeforces 339C Xenia and Weights(dp或暴搜)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Xenia and Weights Xenia has a set of weig ...

  8. codeforces 342D Xenia and Dominoes(状压dp+容斥)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Xenia and Dominoes Xenia likes puzzles ...

  9. [Codeforces 339D] Xenia and Bit Operations

    [题目链接] https://codeforces.com/problemset/problem/339/D [算法] 线段树模拟即可 时间复杂度 :O(MN) [代码] #include<bi ...

随机推荐

  1. JAVA不让类实例化的方法

    虽然java是面向对象编程,但也要尽可能避免创建不必要的对象,因为创建过多的对象不仅占用系统资源,而且多了很多不必要的创建销毁对象开销. 那么有哪些避免类创建对象的方法吗? 1,定义私有构造函数.这在 ...

  2. [转]完整教程--idea使用git进行项目管理

    完整教程--idea使用git进行项目管理 第一部分: 安装 1. 下载地址:  https://git-scm.com/download/win; 如果速度慢, 使用 迅雷下载; 2. 点击安装, ...

  3. 利用SSL For Free工具3分钟获取Let's Encrypt免费SSL证书

    https://www.sslforfree.com/

  4. 客户端JavaScript基础 网页上的输入输出

    创建: 2017/10/06 完成: 2017/10/07   更新: 2017/10/15 增加了通过事件监听器登陆事件的方法的链接   # TODO: 补充 基于Canvas的图象 客户端Java ...

  5. [Swift通天遁地]六、智能布局-(3)添加edges/top/bottom/leading/trailing的约束

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  6. [Swift通天遁地]九、拔剑吧-(16)搭建卡片页面:Card Peek/Pop动态切换界面

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. curl 采集的时候遇到301怎么办

    采集的时候遇到301,采集数据有错误 $ch = curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt( ...

  8. 基于Android SDK安装PhoneGap框架

    下载zip文件PhoneGap 2.0.0 PhoneGap 2.0.0 Released 20 Jul 2012http://phonegap.com/download/ 解压缩后的目录结构:Dir ...

  9. BZOJ 2300凸包+离线

    思路: 倒着加显然吧  动态维护这个凸包就好了 //By SiriusRen #include <bits/stdc++.h> using namespace std; ; int n,m ...

  10. ansible基础知识

    安装ansible epel源 第一步: 下载epel源 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel- ...