题干:

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 %I64d specifier.

Examples

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.

题目大意:

给出一个n,m 然后给出两个串的循环节S和T(不一定等长), n代表第一个串有多少次循环,m代表第二个串有多少次循环,问两个串的Hamming distance是多少,这个距离是指对应位置的字符如果相等对结果的贡献就是0,否者就是1。(加长的两个字符串等长)

解题报告:

这是一道关于汉明距离的题目。首先涨一波知识:

这题第一眼应该就跟数论gcd,lcm相关(因为循环节和循环次数嘛最后还等长),而且数据量就知道不可能让你暴力,肯定是有重复计算,我们需要算那个最小的然后扩大相应的倍数。

于是首先想到只要比较了两个串的lcm长度,再扩大相应倍数就可以,但是这样的数据量还是会TLE,想想也知道啊,如果len1和len2互质的话,复杂度O(len1*len2),最坏为10^12。

想办法优化:

分析一下会发现,在上面的想法中,有一部分比较是没有必要的,比如:

S:  abacdcde                 T:  acdaed

S的长度为8,T的长度为6,len1与len2的gcd = 2,lcm = 24。

比较时

T开头的a并不是和S中的每一位都能进行比较的,也就是说,在一个循环结中,可以一次性将T中的每一位与其将会与S中所对应的位置的字符都比较一下,也就是预处理出来。也不难发现,T的第一个字符,总与s[1]+k*gcd比较,第二个字符,总与s[2]+k*gcd比较...以此类推。(假设从s[1]为第一个字符而不是s[0])

设g=gcd(len1,len2),设ans为S和T中对应位置相同的字符的数目,方法就是在i from 0 to gcd(len1,len2)-1 的循环中,将S分为g个小段,每段长度为len1/g,统计每次该小段上第i个字符出现的次数,然后将T中g个小段的第i个字符出现的次数加入到ans中。最后将ans扩大相应倍数,然后再用总的长度减去ans就是答案。

传送门

AC代码1:(这是一个网络的代码,但是因为dpa数组开成了longlong,所以MLE了,下面有一个优化版本)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int qq = 1e6+10;
char a[qq],b[qq];
int dpa[qq][27],dpb[qq][27];
ll gcd(ll a, ll b){return b==0?a:gcd(b, a%b);
}
ll lcm(ll a, ll b){return a/gcd(a, b)*b;
}
int main(){ll n,m;scanf("%lld%lld",&n,&m);scanf("%s%s",a,b);ll lena = strlen(a);ll lenb = strlen(b);ll g = gcd(lena, lenb);   ll l = lena/g*lenb;memset(dpa, 0, sizeof(dpa));memset(dpb, 0, sizeof(dpb));for(int i=0; i<lena; ++i)    dpa[i%g][a[i]-'a']++;for(int i=0; i<lenb; ++i)    dpb[i%g][b[i]-'a']++;ll ans = 0;for(int i=0; i<g; ++i)for(int j=0; j<26; ++j)ans+=(ll)dpa[i][j]*dpb[i][j];ans = n*lena/l*ans;   //每一个长度为l的字符串中,所含相同的字符有ans个、 // 求长度为n*lena/l 个长度为l的字符串中,所含相同字符的总数、 ans = n*lena - ans;   //求反面、 然后用总长度减去相同字符的个数、 printf("%lld\n", ans);return 0;
}

AC代码2:(这样不需要二维数组,时间慢了一丢丢200+ms,但是剩下了很多空间复杂度)

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX = 1e6 + 5;
char s1[MAX],s2[MAX];
ll cnt[50];
ll Gcd(ll a,ll b) {while(a^=b^=a^=b%=a);return b;
}int main()
{ll n,m;cin>>n>>m;cin>>s1>>s2;ll len1 = strlen(s1);ll len2 = strlen(s2);ll gcd = Gcd(len1,len2);ll lcm = len1 * (len2 / gcd);ll l1=len1/gcd,l2=len2/gcd;ll ans = 0;for(ll i = 0; i<gcd; i++) {memset(cnt,0,sizeof cnt);for(ll j = 0; j<l1; j++) {cnt[s1[gcd*j + i]-'a'] ++;}for(ll j = 0; j<l2; j++) {ans += cnt[s2[gcd*j+i]-'a'];}}ans *=len1*n/ lcm ;
//  ans *= (m*gcd)/len1;ans = len1*n-ans;printf("%lld\n",ans);return 0 ;} 

总结:

这题用的思维主要是:

1.求问题的反面。

2.cnt数组运用了桶计数的思想。空间换时间,1e6正好也开的下(对这个数字还是不太敏感啊你、、)(不对这题好像不是那个桶计数,,,这题cnt数组就开27 就够了)

【CodeForces - 357D】Xenia and Hamming (字符串问题,数论,思维)相关推荐

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

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

  2. 线段树——思维(Codeforces 339D Xenia and Bit Operations/Billboard HDU - 2795)

    Codeforces 339D Xenia and Bit Operations vj地址 题意:给出2的n次方个数,每次将现在这个序列中相邻的两个数运算后合并为一个数,得到一个新的序列,这个新序列的 ...

  3. Codeforces Round #630 (Div. 2) A~D【思维,数论,字符串,位运算】

    A. Exercising Walk 水题一道:在指定空间内你一定要向各个方向走a,b,c,d步问你能否在规定空间内走完这题的坑点样例都给出来了qwq #include <iostream> ...

  4. 【CodeForces - 155C】Hometask (字符串,思维,贪心,熟悉句式)(总结)

    题干: Sergey attends lessons of the N-ish language. Each lesson he receives a hometask. This time the ...

  5. cf 356 - Xenia and Hamming

    Codeforces Round #207 (Div. 2)的D题, 超赞的题目:(思路from:http://www.cnblogs.com/yours1103/p/3371375.html) 假设 ...

  6. 【CodeForces】961 F. k-substrings 字符串哈希+二分

    [题目]F. k-substrings [题意]给定长度为n的串S,对于S的每个k-子串$s_ks_{k+1}...s_{n-k+1},k\in[1,\left \lceil \frac{n}{2} ...

  7. 【题解】 Codeforces Edu41 F. k-substrings (字符串Hash)

    题面戳我 Solution 我们正着每次都要枚举从长到短,时间复杂度承受不了,但是我们可以发现一个规律,假设某次的答案为\(x\),那么这个字符串为\(A+X+B\)组成,无论中间的\(X\)是重叠还 ...

  8. CodeForces - 1497E2 Square-free division (hard version)(dp+数论)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的数列,现在最多可以修改 kkk 个数字为任意数值,现在问最少可以将数列划分成多少个连续的数列,使得每一个单独的段中,任意两个数的乘积都不能是完全 ...

  9. 【CodeForces - 151D】Quantity of Strings (字符串问题,思维推导,有坑)

    题干: Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one g ...

最新文章

  1. 霸气侧漏HTML5--之--canvas(1) api + 弹球例子
  2. 基本概念,BGP协议的特征和消息类型,状态转换?
  3. 【转载】阿里云ECS服务器监控资源使用情况
  4. matlab png转02,matlab把图片pgm格式转换成png格式
  5. python基础编程题、积分面积_Python基础编程题100列目录
  6. 【论文笔记】node2vec:可扩展的网络特征学习
  7. mac安装rstudio_在Windows / Linux / Mac OS上安装R和RStudio入门
  8. hibernate中cascade和inverse中的设置问题
  9. Matlab图像处理系列4———图像傅立叶变换与反变换
  10. css如何实现自动换行,CSS实现自动换行的方法
  11. # Okhttp解析—Interceptor详解
  12. dis的前缀单词有哪些_按前后缀分类单词——dis前缀的名词
  13. 计算机内无法使用搜狗,电脑搜狗输入法不能用怎么办
  14. sysprep无法验证你的windows安装_Sysprep无法验证你的windows 安装。
  15. 谭浩强《C语言》学习1
  16. Ubuntu 输入法fcitx方块乱码解决设置
  17. WebJars简介 —— 前端资源的jar包形式
  18. 调度——特殊生产线介绍
  19. #数据挖掘--第3章:建模调参之支持向量机SVM初体验
  20. python运用深度学习垃圾分类,acc达到98以上

热门文章

  1. 120. Triangle
  2. python 字符串比较忽略大小写的方法_python实现忽略大小写对字符串列表排序的方法...
  3. 5自适应单页源码_超详细!如何建立一个CPA单页网站,附高转化CPA模板源码
  4. l启动进程 linux,《日子》. linux 查看进程启动路径
  5. 1200兆路由器网速_如何选购路由器才能发挥宽带的网速?
  6. 7-1 叶节点求和 (30 分)
  7. html全局浮窗,Html 实现浮动窗口
  8. tp3 普通模式url模式_[tp3.2.1]开启URL(重写模式),省略URL中的index.php
  9. python3.6sysos_求大佬,这是什么情况啊
  10. pppd 源码修改1