Euclidean algorithm and the unique decomposition theorem

Source: Internet
Author: User
Tags gcd greatest common divisor

Rujia "Getting Started classic" offers a classic title:

Division expression, the original question can be found on nyoj, question number 1013

  describes one such division expression: X1 /x2/x3/ /XK, where Xi is a positive integer. The Division expression should be summed from left to right, such as the expression 1/2 /1 /2 value is 1/< Span style= "color: #800080;" >4 . However, you can embed parentheses in an expression to change the order of calculations, such as an expression (1 /2 )/( Span style= "color: #800080;" >1 /2  ) has a value of 1. Enter first an n, indicating that there are n sets of test data, Each set of data input takes up one row, which is a division expression and the input is guaranteed to be legal. Makes the value of an expression an integer. K  <=10000 , Xi<=100000000  Span style= "color: #000000;"  >. Output yes or no sample input  1  1 /2 /1 / 2   

First the calculation shows that X1 constant is the numerator, X2 constant as the denominator, and the rest of the numbers can be moved through the brackets to the molecule up
Then you can write x1*x3*x4........*xk/x2.
As long as the product above can be divisible x2 can be rounded.
First, you can use the Euclidean algorithm for each xi,x2 divided by the XI and X2 greatest common divisor gcd
#include <iostream>#include<cstring>#include<cstdio>using namespacestd;Charch[50010];//Note that the number is up to 9 bits long and that there are/charactersintgcdintXinty) {    if(x<y) Swap (x, y);//This judgment can save a little time .    returny==0? X:GCD (y,x%y);}intMain () {intT;  for(SCANF ("%d", &t); T t--) {scanf ("%s", CH); inttop=0, flag=0; inttmp1,tmp2,tmp; TMP=0; tmp2=0, tmp1=0;;  for(intI=0, Len=strlen (CH); i<len;i++)        {            if(ch[i]>='0'&&ch[i]<='9') {tmp=tmp*Ten+ch[i]-'0'; if(i==len-1)                {                    if(top==1) {TMP2=tmp; TMP2/=gcd (TMP2,TMP1); }                    if(top==0) tmp1=tmp; if(top>1) tmp2/=gcd (tmp2,tmp); }            }            Else            {                if(top==0) tmp1=tmp; if(top==1) {TMP2=tmp; TMP2/=gcd (TMP2,TMP1); }                if(top>1) tmp2/=gcd (tmp2,tmp); Top++; TMP=0; }            if(tmp2==1) {flag=1; Break;} }        if(flag) puts ("YES"); ElsePuts"NO"); }    return 0;} 
View Code

However, the problem can also be solved by using the unique decomposition theorem, but unfortunately 10 of the 8 will have 5,761,455 Prime, considering the worst case, time is unbearable
The only decomposition theorem: Each natural number can be decomposed into one or more prime numbers multiplied by the form (N=P1^A1*P2^A2*.....*PM^AM)

As long as the PI indices of X2 and all molecules are calculated separately, if the pi exponent is x2 greater than all molecules, it cannot be rounded.

#include <iostream>#include<cstdio>#include<cstring>#include<vector>#include<map>#defineSET (a) memset (A,0,sizeof (a))using namespacestd;Const intmax=50010;CharCh[max];intcnt1[100000000],cnt2[100000000];voidTakeintx) {    intflag=0;if(x==2) flag=1;  for(intI=2; i<=x;i++)    {        if(x%i==0)        {             while(x%i==0) {x/=i; Cnt1[i]++; }        }        if(x==1) Break; }}intMain () {intT;  for(SCANF ("%d", &t); T t--) {scanf ("%s", CH); inttmp=0, top=0, tmp2=0; SET (CNT1);        SET (Cnt2);  for(intI=0, Len=strlen (CH); i<len;i++)        {            if(ch[i]>='0'&&ch[i]<='9') {tmp=tmp*Ten+ch[i]-'0'; if(i==len-1) {take (TMP); }            }            Else            {                if(top==1) tmp2=tmp; if(top!=1) take (TMP); TMP=0; Top++; }        }        intflag=0;  for(intI=2; i<=tmp2;i++)        {            if(tmp2%i==0)            {                 while(tmp2%i==0) {Cnt2[i]++; TMP2/=i; }            }            if(Cnt2[i]>cnt1[i]) {flag=1; Break;} if(tmp2==1) Break; }        if(flag) puts ("NO"); ElsePuts"YES"); }    return 0;}
View Code

Euclidean algorithm and the unique decomposition theorem

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.