Codeforces Round #207 (Div. 1) B. Xenia and Hamming(gcd的运用)
2014-04-30 02:03
609 查看
题目链接: B. Xenia and Hamming
题意: 要求找到复制后的两个字符串中不相同的字符
思路: 子问题: 在两串长度是最大公倍数的情况下, 求出一个串在另一个串中重复字符的个数
CODE:
#include <iostream>
#include<stdio.h>
#include<string>
#include<string.h>
using namespace std;
#define M 1000006
int com[M][26];
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
int main()
{
long long n,m;
string x,y;
while(~scanf("%I64d%I64d",&n,&m))
{
cin>>x>>y;
memset(com,0,sizeof(com));
int lenx=x.size(),leny=y.size();
long long g=gcd(lenx,leny);
long long ans=lenx/g*leny;
long long a=ans;
for(int i=0;i<lenx;i++) //巧妙的处理~将两串中重复的字符储存起来
com[i%g][x[i]-'a']++;
for(int i=0;i<leny;i++)
ans-=com[i%g][y[i]-'a'];
printf("%I64d\n",ans*(n*lenx/a));
}
return 0;
}
题意: 要求找到复制后的两个字符串中不相同的字符
思路: 子问题: 在两串长度是最大公倍数的情况下, 求出一个串在另一个串中重复字符的个数
CODE:
#include <iostream>
#include<stdio.h>
#include<string>
#include<string.h>
using namespace std;
#define M 1000006
int com[M][26];
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
int main()
{
long long n,m;
string x,y;
while(~scanf("%I64d%I64d",&n,&m))
{
cin>>x>>y;
memset(com,0,sizeof(com));
int lenx=x.size(),leny=y.size();
long long g=gcd(lenx,leny);
long long ans=lenx/g*leny;
long long a=ans;
for(int i=0;i<lenx;i++) //巧妙的处理~将两串中重复的字符储存起来
com[i%g][x[i]-'a']++;
for(int i=0;i<leny;i++)
ans-=com[i%g][y[i]-'a'];
printf("%I64d\n",ans*(n*lenx/a));
}
return 0;
}
相关文章推荐
- Codeforces Round #207 (Div. 1) B. Xenia and Hamming(gcd的运用)
- 【CF】207 Div.1 B.Xenia and Hamming
- B. Xenia and Hamming----数论入门LCA与GCD的应用
- Codeforces Round #207 (Div. 2) Xenia and Hamming(字符串匹配)
- Codeforces Round #207 (Div. 2) D. Xenia and Hamming
- (公约数问题) Xenia and Hamming (356B)
- Codeforces Round #199(Div. 2) B. Xenia and Spies
- Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence 构造
- Codeforces Round #199 (Div. 2) -- B. Xenia and Spies (简单模拟)
- Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations
- Codeforces Round #199 (Div. 2) B. Xenia and Spies
- Codeforces Round #280 (Div. 2) E. Vanya and Field (数学GCD)
- Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence 构造
- Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence(构造,思路)
- Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence 构造
- Codeforces Round #197 (Div. 2) -- C. Xenia and Weights (DFS回溯)
- Codeforces Round #199 (Div. 2) B. Xenia and Spies(贪心)
- Codeforces Round #207 (Div. 1) B (gcd的巧妙运用)
- Codeforces 432 Div. 2-D-Arpa and a list of numbers(枚举倍数求GCD)
- Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence 构造