这题目一看很牛逼,其实非常easy。求求最小公倍数,最大公约数,均摊复杂度其实就是O(n)。

  1. /* 356B */
  2. #include <iostream>
  3. #include <string>
  4. #include <map>
  5. #include <queue>
  6. #include <set>
  7. #include <stack>
  8. #include <vector>
  9. #include <deque>
  10. #include <algorithm>
  11. #include <cstdio>
  12. #include <cmath>
  13. #include <ctime>
  14. #include <cstring>
  15. #include <climits>
  16. #include <cctype>
  17. #include <cassert>
  18. #include <functional>
  19. #include <iterator>
  20. #include <iomanip>
  21. using namespace std;
  22. //#pragma comment(linker,"/STACK:102400000,1024000")
  23.  
  24. #define sti set<int>
  25. #define stpii set<pair<int, int> >
  26. #define mpii map<int,int>
  27. #define vi vector<int>
  28. #define pii pair<int,int>
  29. #define vpii vector<pair<int,int> >
  30. #define rep(i, a, n) for (int i=a;i<n;++i)
  31. #define per(i, a, n) for (int i=n-1;i>=a;--i)
  32. #define clr clear
  33. #define pb push_back
  34. #define mp make_pair
  35. #define fir first
  36. #define sec second
  37. #define all(x) (x).begin(),(x).end()
  38. #define SZ(x) ((int)(x).size())
  39. #define lson l, mid, rt<<1
  40. #define rson mid+1, r, rt<<1|1
  41.  
  42. const int maxn = 1e6+;
  43. char s[maxn], d[maxn];
  44. int cs[];
  45. int cd[];
  46.  
  47. int main() {
  48. ios::sync_with_stdio(false);
  49. #ifndef ONLINE_JUDGE
  50. freopen("data.in", "r", stdin);
  51. freopen("data.out", "w", stdout);
  52. #endif
  53.  
  54. __int64 n, m;
  55.  
  56. scanf("%I64d %I64d", &n, &m);
  57. scanf("%s %s", s, d);
  58. int slen = strlen(s);
  59. int dlen = strlen(d);
  60.  
  61. int g = __gcd(slen, dlen);
  62. __int64 lcm = 1LL * slen / g * dlen;
  63. __int64 n_ = lcm / slen;
  64. __int64 m_ = lcm / dlen;
  65.  
  66. #ifndef ONLINE_JUDGE
  67. printf("n_ = %I64d, m_ = %I64d\n", n_, m_);
  68. #endif
  69.  
  70. __int64 t = n / n_;
  71. __int64 tot;
  72.  
  73. __int64 ans = ;
  74. int i, j, k;
  75.  
  76. for (i=; i<g; ++i) {
  77. // count s
  78. memset(cs, , sizeof(cs));
  79. for (j=i,tot=; j<slen; j+=g,++tot) {
  80. ++cs[s[j]-'a'];
  81. }
  82. for (j=i; j<dlen; j+=g) {
  83. ans += (tot - cs[d[j]-'a']);
  84. }
  85. }
  86.  
  87. ans *= t;
  88. printf("%I64d\n", ans);
  89.  
  90. #ifndef ONLINE_JUDGE
  91. printf("time = %d.\n", (int)clock());
  92. #endif
  93.  
  94. return ;
  95. }

【CF】207 Div.1 B.Xenia and Hamming的更多相关文章

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

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

  2. 【CF】121 Div.1 C. Fools and Roads

    题意是给定一棵树.同时,给定如下k个查询: 给出任意两点u,v,对u到v的路径所经过的边进行加计数. k个查询后,分别输出各边的计数之和. 思路利用LCA,对cnt[u]++, cnt[v]++,并对 ...

  3. 【CF】310 Div.1 C. Case of Chocolate

    线段树的简单题目,做一个离散化,O(lgn)可以找到id.RE了一晚上,额,后来找到了原因. /* 555C */ #include <iostream> #include <str ...

  4. 【CF】110 Div.1 B. Suspects

    这题目乍眼一看还以为是2-sat.其实很水的,O(n)就解了.枚举每个人,假设其作为凶手.观察是否满足条件.然后再对满足的数目分类讨论,进行求解. /* 156B */ #include <io ...

  5. 【CF】222 Div.1 B Preparing for the Contest

    这样类似的题目不少,很多都是一堆优化条件求最优解,这个题的策略就是二分+贪心.对时间二分, 对费用采用贪心. /* 377B */ #include <iostream> #include ...

  6. 【CF】142 Div.1 B. Planes

    SPFA.注意状态转移条件,ans的求解需要在bfs中间求解.因为只要到了地点n,则无需等待其他tourist.还是蛮简单的,注意细节. /* 229B */ #include <iostrea ...

  7. 【CF】196 Div.2 D. Book of Evil

    显然这个图是一课树,看着题目首先联想到LCA(肯定是可以解的).但是看了一下数据大小,应该会TLE.然后,忽然想到一个前面做过的题目,大概是在一定条件下树中某结点旋转成为根后查询最长路径.结果灵感就来 ...

  8. 【CF】223 Div.1 C Sereja and Brackets

    水线段树. /* 380C */ #include <iostream> #include <string> #include <map> #include < ...

  9. 【CF】259 Div.1 B Little Pony and Harmony Chest

    还蛮有趣的一道状态DP的题目. /* 435B */ #include <iostream> #include <string> #include <map> #i ...

随机推荐

  1. HTML表单元素中disabled的元素的值不会提交到服务器

    一.在HTMl页面的form表单中对disabled的元素的属性和值不会提交到服务器 实例1: <form action="#"> <input type=&qu ...

  2. C10K问题和Libevent库介绍

    http://blog.chinaunix.net/uid-20761674-id-75056.html 一.C10K的问题 C10K的问题在上个世纪90年代就被提出来了.大概的意思是当用户数超过1万 ...

  3. Java线程间通信-回调的实现方式

    Java线程间通信-回调的实现方式   Java线程间通信是非常复杂的问题的.线程间通信问题本质上是如何将与线程相关的变量或者对象传递给别的线程,从而实现交互.   比如举一个简单例子,有一个多线程的 ...

  4. java- 枚举的常见用法

    用法一:常量 public enum MyColor{Red,Black,Blue} public enum Color { RED, GREEN, BLANK, YELLOW } enum为枚举类的 ...

  5. css滚动条样式

    1.横向滚动条:(abeamScroll) <div style="width:400px;height:200px;overflow-x:auto;overflow-y:hidden ...

  6. scrapy, 自带命令行调用工具.

    #-*- coding:utf-8 -*- from scrapy import cmdline cmdline.execute("scrapy crawl dmoz".split ...

  7. 用source code编译安装Xdebug

    1. Unpack the tarball: tar -xzf xdebug-2.2.x.tgz.  Note that you do not need to unpack the tarball i ...

  8. web前端工程师必须掌握的localStorage(二)

    最近工作太忙了,回来后就很晚了,因为红牛喝太多都不想睡觉了(公司免费给的,好多箱o(╯□╰)o),睡不着就想着逛逛博客园,本人最近忙着做一个仿原生app的singlePage应用,话说最近后台那帮兄弟 ...

  9. JDK重要包和Java学习方法论

    以下内容摘自:万能的林萧说:一篇文章教会你,如何做到简历中要求的“要有扎实的Java基础”    第一级别:精读源码 该级别包含的包如下: java.io java.lang java.util 第二 ...

  10. echo、print、print_r、printf、sprintf、var_dump的区别比较

    一.echoecho() 实际上不是一个函数,是php语句,因此您无需对其使用括号.不过,如果您希望向 echo() 传递一个以上的参数,那么使用括号会发生解析错误.而且echo是返回void的,并不 ...