比赛的时候不知道怎么写。。。 太弱了。

看了别人的代码,觉得这个是个经典的知识点吧。 gcd的巧妙运用

自己想的时候苦苦思考怎么用dp求解。 无奈字符串太长而想不出好的算法。

其实在把a和b字符串都分成以gcd(a,b)长度为单位的字符串时。 设为la=a/gcd(a,b) ,lb=b/gcd(a,b) . 明显可以知道的是gcd(la,lb)==1, 所以la与lb长度的字符串要分别拼接lb和la次才能拼成两个相等的串. 这样就有了解法.

     for(int i=0;i<lena;i++)
  dpa[i%k][a[i]-'a']++; // 统计在a串 在gcd(a,b)长度某一位含一种字母的个数
for(int i=0;i<lenb;i++)
  dpb[i%k][b[i]-'a']++;
long long sum=0;
for(int i=0;i<k;i++)
  for(int j=0;j<26;j++)
  {
sum += (long long)dpa[i][j]*(long long )dpb[i][j]; //直接相乘就是相同的个数和了
  }

  

B. 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.

Codeforces Round #207 (Div. 1) B (gcd的巧妙运用)的更多相关文章

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

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

  2. Codeforces Round #323 (Div. 2) C. GCD Table 暴力

    C. GCD Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/583/problem/C ...

  3. Codeforces Round #323 (Div. 2) C. GCD Table map

    题目链接:http://codeforces.com/contest/583/problem/C C. GCD Table time limit per test 2 seconds memory l ...

  4. Codeforces Round #651 (Div. 2) B. GCD Compression(数论)

    题目链接:https://codeforces.com/contest/1370/problem/B 题意 给出 $2n$ 个数,选出 $2n - 2$ 个数,使得它们的 $gcd > 1$ . ...

  5. Codeforces Round #767 (Div. 2)——B. GCD Arrays

    B. GCD Arrays 题源:https://codeforces.com/contest/1629/problem/B 题目大意 给出一段区间[l, r],可以进行操作(把任意两个数拿出来,把他 ...

  6. Codeforces Round #323 (Div. 2) C.GCD Table

    C. GCD Table The GCD table G of size n × n for an array of positive integers a of length n is define ...

  7. Codeforces Round #323 (Div. 1) A. GCD Table

    A. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  8. Codeforces Round #207 (Div. 1)B(数学)

    数学so奇妙.. 这题肯定会有一个循环节 就是最小公倍数 对于公倍数内的相同的数的判断 就要借助最大公约数了 想想可以想明白 #include <iostream> #include< ...

  9. Codeforces Round #207 (Div. 2)

    A:超级大水题: 代码: #include<cstdio> #define maxn 105 using namespace std; int n,a[maxn],x,y,ans; int ...

随机推荐

  1. input-form-select-a-img-ul-dl标签

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  2. mysql表属性、索引、约束

    1.表属性 创建表的基本语法: create table [if not exists] 表名 (字段列表 [,索引或约束列表])[表选项列表] 其中,字段列表格式如下: 字段名 类型 [属性列表], ...

  3. C语言之文件操作08——总结

    C程序的文件操作共涵盖7个例题,包括格式打印,文件读取,条件查找,矩阵的文件操作,数据格式输入及调用计算等内容. 文件操作使得程序有更强的拓展性,使其能够单独保存数据.这为程序的调试和优化打下了坚实的 ...

  4. 在Unity控制台下使用富文本

    之前都不知道,最近看了csdn一位开发者的博文突然发现 <b>asd</b> <color="red">asd</color> &l ...

  5. atitit.提升性能AppCache

    atitit.提升性能AppCache 1.1. 起源1 2. 离线存储2 3. AppCache2 3.1. Appcache事件点如图2 3.2. Manifest文件4 3.3. 自动化工具4 ...

  6. Android4.4的zygote进程(上)

    1背景 前些天为了在科室做培训,我基于Android 4.4重新整理了一份关于zygote的文档.从技术的角度看,这几年zygote并没有出现什么大的变化,所以如果有人以前研究过zygote,应该不会 ...

  7. Linux基础tree命令

    tree命令以树状图列出文件目录结构.不过某些Linux上(Centos 6.4)没有tree命令,本文将介绍安装方法. 常用参数: tree -d 只显示目录. tree -L 1 只显示第一层目录 ...

  8. OSGI 面向Java的动态模型系统

    OSGI (面向Java的动态模型系统) OSGi(Open Service Gateway Initiative)技术是Java动态化模块化系统的一系列规范.OSGi一方面指维护OSGi规范的OSG ...

  9. java大数模板

    java 大数计算   这几天做了几道用大数的题,发现java来做大数运算十分方便.对acmer来说是十分实用的 1.valueOf(parament); 将参数转换为制定的类型 比如 int a=3 ...

  10. mysql替换成指定字符

    ,,, ), 'XXXX' )-- 隐藏从第四位开始的6个字符,包括第四个字符,替换成X