先上题目:

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. 使用 Jenkins + GitHub + Nginx + HTTPS 搭建静态网站

    参考https://www.imooc.com/article/20079 http://www.haoduoyu.cc/

  2. redis-数据结构以及使用场景分析

    目录 redis 常见数据结构以及使用场景分析 key String Hash List Set Sorted Set Bitmap和HyperLogLog Pub/Sub redis 常见数据结构以 ...

  3. [转]HTML5 Day 4: Add Drop Down Menu to ASP.NET MVC HTML5 Template using CSS and jQuery

    本文转自:http://pietschsoft.com/post/2010/11/17/HTML5-Day-4-Add-DropDown-Menu-ASPNET-MVC-HTML5-Template- ...

  4. 使用Jquery.form.js ajax表单提交插件弹出下载提示框

    现象: 使用jquery的from做ajax表单提交的时候,后台处理完毕返回json字符串,此时浏览器提示下载一个json文件而不是在success里面继续解析该json对象. 具体的原因: 浏览器兼 ...

  5. Android通过透明度设置背景变暗

    变暗 WindowManager.LayoutParams lp=getWindow().getAttributes(); lp.alpha=0.3f; getWindow().addFlags(Wi ...

  6. JS——缓慢动画封装

    在知道如何获取内嵌式和外链式的标签属性值之后,我们再次封装缓慢动画: 单个属性 <!DOCTYPE html> <html> <head lang="en&qu ...

  7. python 求一个文件中每个字符出现的次数

    import pprint import collections filename = input('Input filename') with open(filename) as info: cou ...

  8. Python语言之函数

    1.函数的定义与调用 def function(x): print("function(%s)"%x) function("hello") #call the ...

  9. Caffe FCN:可视化featureMaps和Weights(C++)、获取FCN结果

    为何不使用C++版本FCN获取最后的分割掩模,何必要使用python呢!因此需要获取网络最后层的featureMaps,featureMaps的结果直接对应了segmentation的最终结果,可以直 ...

  10. ARM处理器的寄存器,ARM与Thumb状态,7中运行模式

     ** ARM处理器的寄存器,ARM与Thumb状态,7中运行模式  分类: 嵌入式 ARM处理器工作模式一共有 7 种 : USR  模式    正常用户模式,程序正常执行模式 FIQ模式(Fast ...