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

 /* 356B */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = 1e6+;
char s[maxn], d[maxn];
int cs[];
int cd[]; int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif __int64 n, m; scanf("%I64d %I64d", &n, &m);
scanf("%s %s", s, d);
int slen = strlen(s);
int dlen = strlen(d); int g = __gcd(slen, dlen);
__int64 lcm = 1LL * slen / g * dlen;
__int64 n_ = lcm / slen;
__int64 m_ = lcm / dlen; #ifndef ONLINE_JUDGE
printf("n_ = %I64d, m_ = %I64d\n", n_, m_);
#endif __int64 t = n / n_;
__int64 tot; __int64 ans = ;
int i, j, k; for (i=; i<g; ++i) {
// count s
memset(cs, , sizeof(cs));
for (j=i,tot=; j<slen; j+=g,++tot) {
++cs[s[j]-'a'];
}
for (j=i; j<dlen; j+=g) {
ans += (tot - cs[d[j]-'a']);
}
} ans *= t;
printf("%I64d\n", ans); #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

【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. Sql遍历更新脚本

    DECLARE My_Cursor CURSOR --定义游标 declare@indexId int FOR (SELECT * FROM dbo.GalleryPhoto) --查出需要的集合放到 ...

  2. 图灵API

    namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine( ...

  3. JAVA长连接demo

    http://blog.csdn.net/caomiao2006/article/details/38830475 JAVA长连接demo 2014-08-25 23:20 4767人阅读 评论(2) ...

  4. cognos 10.2.2 搭建网关做负载均衡

    最近要设计cognos服务器灾备模式,所以想到了cognos10自带的gateway负载均衡模式,搭建起来还是挺简洁的 设计背景: cognos主服务器:231 cognos灾备服务器:238 gat ...

  5. Javascript 迭代法实现数组多条件排序

    多条件排序可能有很多种思路,效率也各不相同,我的方法可能只适合自己用,毕竟目的是为了实现功能,所以采用了最笨的方法,不过效果还是很理想的,经过多次测试,6列1000行数据,平均排序时间大约是:28ms ...

  6. addLoadEvent函数

    首先是addLoadEvent函数的代码清单: function addLoadEvent(func){    var oldonload=window.onload;    if(typeof wi ...

  7. 基于HTML5和JSP实现的图片Ajax上传和预览

    本文对如何实现使用Ajax提交"multipart/form"格式的表单数据,已经如何在图片上传之前,在浏览器上进行预览.使用的主要相关技术HTML5的FILE API,XMLHt ...

  8. docker中搭建gitlab

    1, 下载镜像 docker pull sameersbn/gitlab:7.4.3 # 下载gitlab镜像 docker pull sameersbn/mysql:latest # 下载gitla ...

  9. getopt getopt_long

    getopt_long支持长选项的命令行解析,使用man getopt_long,得到其声明如下: #include <getopt.h> int getopt_long(int argc ...

  10. PL/SQL学习(一)

    原文参考:http://plsql-tutorial.com/ 组成: 声明部分(可选) 执行部分(必选) 异常处理(可选)       声明:         DECLARE       执行:   ...