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

Source: Internet
Author: User

Vasya have recently learned at school what a number ' s divisor are and decided to determine a string ' s divisor. Here's what he came.

String A is the divisor of string B if and only if there exists a positive integer x such so if we write out string a co Nsecutively x times, we get string B. For example, the string "Abab" has both divisors-"AB" and "abab".

Now Vasya wants to write a program that calculates the number of common divisors of the 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 does 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 is 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".

The "convention string" of 2 strings is KMP, and the minimum cycle section is calculated separately.
Of course, if the smallest circular section is different, the answer is obviously 0.
Set S1 = A^x
Set s2 = B^y
Next, the number of conventions for A and B is only required.

/************************************************************************* > File name:cf-117-d.cpp > Aut Hor:alex > Mail: [email protected] > Created time:2015 March 30 Monday 15:42 33 seconds ******************************* *****************************************/#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 DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;Const intN =200100;intNxt[n];CharStr1[n], str2[n];BOOLHas[n];CharP1[n], p2[n];voidGet_next (CharStr[],intNxt[]) {nxt[0] = -1;intj =0;intK =-1;intLen =strlen(str); while(J < Len) {if(k = =-1||        STR[J] = = Str[k]) {nxt[++j] = ++k; }Else{k = nxt[k]; }    }}intMain () { while(~scanf("%s%s", STR1, str2)) {intLen1 =strlen(STR1); Get_next (str1, NXT);intCnt1, Cnt2;if(len1% (Len1-nxt[len1])) {cnt1 = len1;strcpy(P1, str1); }Else{cnt1 = Len1-nxt[len1]; for(inti =0; i < cnt1;            ++i) {P1[i] = Str1[i]; } P1[cnt1] =' + '; } get_next (str2, NXT);intLen2 =strlen(STR2);if(Len2% (Len2-nxt[len2])) {cnt2 = Len2;strcpy(P2, str2); }Else{cnt2 = Len2-nxt[len2]; for(inti =0; i < Cnt2;            ++i) {P2[i] = Str2[i]; } P2[cnt2] =' + '; }if(strcmp(P1, p2)) {printf("0\n");Continue; }intx1 = len1/cnt1;intx2 = Len2/cnt2;memset(Has,0,sizeof(has)); for(inti =1; I * I <= x1; ++i) {if(x1% i = =0) {Has[i] = has[x1/i] =1; }        }intAns =0; for(inti =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;}

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.