Application of the unique decomposition theorem: The second problem of NOIP2009 raising group Hankson [unique decomposition theorem | violence]

Source: Internet
Author: User
Tags gcd cmath

Input/output format

Input format:

The first behavior is a positive integer n, which indicates that there are n groups of input data. The next n rows are each

Line a set of input data, a0,a1,b0,b1 four positive integers, separated by a space between each two integers. Input

The data guarantees that the A0 can be divisible by A1, B1 divisible by B0.

Output format:

Output file Son.out total n rows. The output of each set of input data is one row, which is an integer.

For each set of data: if there is no such x, output 0;

If there is such an X, please output the number of x that satisfies the condition;

Input/Output sample

Input Sample # #:

2

Sample # # of output:

6
2

"description"

The first set of input data, X can be 9, 18, 36, 72, 144, 288, a total of 6.

The second set of input data, X can be 48, 1776, a total of 2.

"Data Range"

For 50% of data, 1≤a0,a1,b0,b1≤10000 and n≤100 are guaranteed.

For 100% of data, 1≤a0,a1,b0,b1≤2,000,000,000 and n≤2000 are guaranteed.

Resolution

1. Decompose the A0,A1,B0,B1 on-line and then judge

2. Brute Force enumeration B1 + various optimizations

Note Enumeration to sqrt (B1) instead of sqrt (B1) +1

//Unique decomposition theorem#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <    Cmath> #include <queue>using namespace std;const int N=50005;inline int read () {char c=getchar (); int x=0,f=1; while (c< ' 0 ' | |    C> ' 9 ') {if (c== '-') f=-1; C=getchar ();}    while (c>= ' 0 ' &&c<= ' 9 ') {x=x*10+c-' 0 '; C=getchar ();} return x*f;}    int n,a0,a1,b0,b1,a,b,ans;int vis[n],p[n],cnt=0;void era (int n) {int m=sqrt (n) +1;    for (int i=2;i<=m;i++) if (!vis[i]) for (int j=i+i;j<=n;j+=i) vis[j]=1; for (int i=2;i<=n;i++) if (!vis[i]) p[++cnt]=i;}    void solve (int p) {int ea0=0,ea1=0,eb0=0,eb1=0;    while (a0%p==0) ea0++,a0/=p;    while (a1%p==0) ea1++,a1/=p;    while (b0%p==0) eb0++,b0/=p;    while (b1%p==0) eb1++,b1/=p;        if (EA0==EA1&AMP;&AMP;EB0==EB1) {if (ea1<=eb1) ans*=eb1-ea1+1;    else ans=0; }else if (ea0!=ea1&&eb0!=eb1&&ea1!=eb1) ans=0;}    int main (int argc, const char * argv[]) {era (50000);    N=read ();while (n--) {a0=read (); A1=read (); B0=read (); B1=read ();        Ans=1;        for (int i=1;i<=cnt;i++) solve (p[i]);        if (b1!=1) solve (B1);    printf ("%d\n", ans); } return 0;}

//Violence #include <iostream> #include <cstdio> #include <cstring> #include <algorithm># Include <cmath> #include <queue>using namespace std;const int N=50005;inline int read () {    char C=getchar ( ); int x=0,f=1;    while (c< ' 0 ' | | C> ' 9 ') {if (c== '-') f=-1; C=getchar ();}    while (c>= ' 0 ' &&c<= ' 9 ') {x=x*10+c-' 0 '; C=getchar ();}    return x*f;} int n,a0,a1,b0,b1,a,b;inline int gcd (int a,int b) {return b==0?a:gcd (b,a%b);} inline int cal (int i) {    if (I%A1) return 0;    else return gcd (i/a1,a) ==1&&gcd (b,b1/i) ==1;} int main (int argc, const char * argv[]) {    n=read ();    while (n--) {        a0=read (); A1=read (); B0=read (); B1=read ();        int Ans=0,m=sqrt (B1);        a=a0/a1,b=b1/b0;        for (int i=1;i<=m;i++) if (b1%i==0) {            ans+=cal (i);            if (I*I!=B1) ans+=cal (b1/i);        }        printf ("%d\n", ans);    }    return 0;}

Students must master, combined with courseware textbooks to study.

Application of the unique decomposition theorem: The second problem of NOIP2009 raising group Hankson [unique decomposition theorem | violence]

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.