Codeforces Round #117 (Div. 2)---D. Common Divisors

Vasya has recently learned at school what a number’s divisor is and decided to determine a string’s divisor. Here is what he came up with.

String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string “abab” has two divisors — “ab” and “abab”.

Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.

Input

The first input line contains a non-empty string s1.

The second input line contains a non-empty string s2.

Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.

Output

Print the number of common divisors of strings s1 and s2.

Sample test(s)

Input

abcdabcd

abcdabcdabcdabcd

Output

2

Input

aaa

aa

Output

1

Note

In first sample the common divisors are strings “abcd” and “abcdabcd”.

In the second sample the common divisor is a single string “a”. String “aa” isn’t included in the answer as it isn’t a divisor of string “aaa”.

求2个串的”公约串”,分别kmp求出最小循环节

当然如果最小的循环节都不一样,答案显然就是0

设S1 = A^X

设s2 = B^Y

接下来只要求A和B的公约数的个数就行了

/*************************************************************************
    > File Name: CF-117-D.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年03月30日 星期一 15时42分33秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int N = 200100;
int nxt[N];
char str1[N], str2[N];
bool has[N];
char p1[N], p2[N];

void get_next(char str[], int nxt[])
{
    nxt[0] = -1;
    int j = 0;
    int k = -1;
    int len = strlen(str);
    while (j < len)
    {
        if (k == -1 || str[j] == str[k])
        {
            nxt[++j] = ++k;
        }
        else
        {
            k = nxt[k];
        }
    }
}

int main()
{
    while (~scanf("%s%s", str1, str2))
    {
        int len1 = strlen(str1);
        get_next(str1, nxt);
        int cnt1, cnt2;
        if (len1 % (len1 - nxt[len1]))
        {
            cnt1 = len1;
            strcpy(p1, str1);
        }
        else
        {
            cnt1 = len1 - nxt[len1];
            for (int i = 0; i < cnt1; ++i)
            {
                p1[i] = str1[i];
            }
            p1[cnt1] = ‘\0‘;
        }
        get_next(str2, nxt);
        int len2 = strlen(str2);
        if (len2 % (len2 - nxt[len2]))
        {
            cnt2 = len2;
            strcpy(p2, str2);
        }
        else
        {
            cnt2 = len2 - nxt[len2];
            for (int i = 0; i < cnt2; ++i)
            {
                p2[i] = str2[i];
            }
            p2[cnt2] = ‘\0‘;
        }
        if (strcmp(p1, p2))
        {
            printf("0\n");
            continue;
        }
        int x1 = len1 / cnt1;
        int x2 = len2 / cnt2;
        memset(has, 0, sizeof(has));
        for (int i = 1; i * i <= x1; ++i)
        {
            if (x1 % i == 0)
            {
                has[i] = has[x1 / i] = 1;
            }
        }
        int ans = 0;
        for (int i = 1; i * i <= x2; ++i)
        {
            if (x2 % i == 0)
            {
                if (has[i])
                {
                    ++ans;
                }
                if (has[x2 / i] && i * i != x2)
                {
                    ++ans;
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-08-27 05:45:32

Codeforces Round #117 (Div. 2)---D. Common Divisors的相关文章

Codeforces Round #256 (Div. 2) E Divisors

E. Divisors Bizon the Champion isn't just friendly, he also is a rigorous coder. Let's define function f(a), where a is a sequence of integers. Function f(a) returns the following sequence: first all divisors of a1 go in the increasing order, then al

Codeforces Round #259 (Div. 2) 解题报告

终于重上DIV1了.... A:在正方形中输出一个菱形 解题代码: 1 // File Name: a.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月01日 星期五 23时27分55秒 4 5 #include<vector> 6 #include<set> 7 #include<deque> 8 #include<stack> 9 #include<bitset> 10 #inclu

Codeforces Round #257 (Div. 2) E题:Jzzhu and Apples 模拟

E. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to

Codeforces Round #245 (Div. 1)——Tricky Function

l and dished out an assist in the Blackhawks' 5-3 win over the Nashville Predators.Shaw said just playing with the Blackhawks was enough motivation for him."Positive, I'm playing in the NHL," Shaw said after Sunday's win. "What can't you be

Codeforces Round #354 (Div. 2) ABCD

Codeforces Round #354 (Div. 2) Problems # Name     A Nicholas and Permutation standard input/output 1 s, 256 MB    x3384 B Pyramid of Glasses standard input/output 1 s, 256 MB    x1462 C Vasya and String standard input/output 1 s, 256 MB    x1393 D T

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #422 (Div. 2) A. I&#39;m bored with life 暴力

A. I'm bored with life Holidays have finished. Thanks to the help of the hacker Leha, Noora managed to enter the university of her dreams which is located in a town Pavlopolis. It's well known that universities provide students with dormitory for the

Codeforces Round #356 (Div. 2) [Codeforces680]

此处有目录↑ Codeforces Round #356(Div. 2):http://codeforces.com/contest/680 A. Bear and Five Cards (贪心) time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A little bear Limak plays a game. He has

Codeforces Round #424 (Div. 2) E. Cards Sorting(线段树)

题目链接:Codeforces Round #424 (Div. 2) E. Cards Sorting 题意: 将n个数放进一个队列,每次检查队首,看看是不是队列中最小的数,如果是就扔掉,如果不是就放到队尾. 这样直到队列为空,为需要操作多少次. 题解: 考虑用两个指针模拟,最开始now指针指向第一个数,然后nxt指针指向下一个将要被删除的数. 然后我们要算出这里需要移动多少步,然后删掉这个数,一直重复操作,直到将全部的数删完. nxt指针可以用set来维护,now指针可以用并查集来维护. 计