CodeForces - 357D - Xenia and Hamming
先上题目:
1 second
256 megabytes
standard input
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.
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.
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.
100 10
a
aaaaaaaaaa
0
1 1
abacaba
abzczzz
4
2 3
rzr
az
5
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的更多相关文章
- Codeforces Round #207 (Div. 1) B. Xenia and Hamming(gcd的运用)
题目链接: B. Xenia and Hamming 题意: 要求找到复制后的两个字符串中不同样的字符 思路: 子问题: 在两串长度是最大公倍数的情况下, 求出一个串在还有一个串中反复字符的个数 CO ...
- codeforces B. Xenia and Spies 解题报告
题目链接:http://codeforces.com/problemset/problem/342/B 题目意思:有n个spy,编号从1-n,从左到右排列.现在的任务是,spy s要把信息传递到spy ...
- codeforces A. Xenia and Divisors 解题报告
题目链接:http://codeforces.com/problemset/problem/342/A 题目意思:给出n个数,找出n/3个组且每组有3个数,这三个数必须要符合两个条件:1.a < ...
- codeforces B. Xenia and Ringroad 解题报告
题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...
- cf D. Xenia and Hamming
http://codeforces.com/contest/357/problem/D 题意:给你两个数n和m,表示两个字符串的循环次数,然后给出两个字符串,求出其相同位置字符不同的个数. 先求出两个 ...
- codeforces 339C Xenia and Bit Operations(线段树水题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Xenia and Bit Operations Xenia the beginn ...
- codeforces 339C Xenia and Weights(dp或暴搜)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Xenia and Weights Xenia has a set of weig ...
- codeforces 342D Xenia and Dominoes(状压dp+容斥)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud D. Xenia and Dominoes Xenia likes puzzles ...
- [Codeforces 339D] Xenia and Bit Operations
[题目链接] https://codeforces.com/problemset/problem/339/D [算法] 线段树模拟即可 时间复杂度 :O(MN) [代码] #include<bi ...
随机推荐
- 洛谷 P1312 [ NOIP 2011 ] Mayan游戏 —— 搜索+模拟
题目:https://www.luogu.org/problemnew/show/P1312 还是不擅长这种题,所以参考了一下TJ: 其实也很好搜,按字典序,先搜右移,再搜左移: 不交换相同颜色的两个 ...
- Android SDK Manager 无法更新问题(转载)
先看看如何加快更新速度,再说如何更新. 首先更新host文件,如图,打开目录 C:\Windows\System32\drivers\etc,在目录下有hosts文件 打开方式选用“记事本”打开 将一 ...
- CSS伪类对象before和after的实例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- hash练习们
610. 数对的个数 ★★ 输入文件:dec.in 输出文件:dec.out 简单对比时间限制:1 s 内存限制:128 MB Description出题是一件痛苦的事情!题目看多了也 ...
- Akka源码分析-Actor创建
上一篇博客我们介绍了ActorSystem的创建过程,下面我们就研究一下actor的创建过程. val system = ActorSystem("firstActorSystem" ...
- mahjong
题目描述 “为什么, 你们的力量在哪里得到如此地......”“我们比 1 分钟前的我们还要进步, 虽然很微小, 但每转一圈就会前进一寸.这就是钻头啊!”“那才是通向毁灭的道路.为什么就没有意识到螺旋 ...
- python自动化测试学习笔记-1
一.什么是自动化 自动化测试是把以人为驱动的测试行为转化为机器执行的一种过程.直白的就是为了节省人力.时间或硬件资源,提高测试效率,便引入了通过软件或程序自动化执行测试用例进行测试: 二.python ...
- Spring思维课程导图——bean属性的设置
- [Windows Server 2012] 安装Apache+PHP+MySQL
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:Win2012 ...
- 详谈java集合框架
1.为什么使用集合框架 当我们并不知道程序运行时会需要多少对象,或者需要更复杂方式存储对象——可以使用Java集合框架 2.Java集合框架包含的内容 接口:(父类)Collection接口下包含Li ...