HLG Unit Fraction【分解约数】

Description

Unit Fraction is a special fraction.For example,1/2 , 1/3 and so on are Unit Fractions, as their numerator is 1 and denominator is the positive integer greater than 1.

The ancient Greeks worship this type fraction.They think that everything in the world can be measured  by the Unit Fractions.There is one example that one rope with 2/3 meters long can be expressed as 2 ropes with 1/3 meters long.

One of well-known figures is Hipasose,who paid his life for finding out that the root of 2 cannot be measurd by the Unit Fraction.

From the history, we know that people will pay for a price to find out and stand up for the truth.

The following problem is not about the truth but something about fraction.

We want to know whether one fraction can be the sum of two Unit Fractions.

Input

There are many test cases.Each case have only two positive integers m (indicating the Molecular)and n(indicating the Denominator)(0<m,n<1000,000).

Process to the end of file.

Output
For each test case, first print a line saying "Scenario #k", where k is the number of the test case.Then,if there are not only one solutions output "Find NUMBER solutions",and NUMBER should be replaced by the solutions' number. If there is exactly one solution,output "Only one solution".If no solution is exist ,output "No solution".Each case's output in one line.Print a blank line after each test case, even after the last one.
Sample Input

2 3

3 4

4 5

Sample Output

Scenario #1

Find 2 solutions 

Scenario #2

Only one solution

 

Scenario #3

No solution

code :

View Code
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int cmp(const void*p1,const void*p2)
{
    return *(long long*)p1-*(long long*)p2;
}
long long a[1000023];
long long p[1000023];
long long gcd(long long y,long long x)
{
        return x==0?y:gcd(x,y%x);
}
long long sum=0;
void prime()
{
        long long i,j;
        memset(a,0,sizeof(a));
        for(i=2;i<=1000000;i++)
        {
                if(!a[i])
                {
                    p[sum++]=i;
                    if(i<=1000)
                        for(j=i*2;j<=1000020;j+=i)
                                a[j]=1;
                }
        }
}
long long fac[1000001];
int main()
{
        long long n,m,i,j,k,tot,n_t,tmp,tmp2;
        long long ca=1,res;
        prime();
        while(scanf("%lld%lld",&m,&n)!=EOF)
        {
                long long mod=gcd(m,n);
                m/=mod;
                n/=mod;
                n_t=n;
                n*=n;
                tot=-1;
                printf("Scenario #%lld\n",ca++);
                if(m>n_t)
                {
                    printf("No solution\n\n");
                    continue;
                }
                long long kk=n;
                for(i=0;p[i]<=n&&i<sum;i++)
                {
                        if(n%p[i]==0)
                        {
                                tmp=p[i];
                                tmp2=tot;
                                while(n%tmp==0)
                                {
                                        fac[++tot]=tmp;
                                        kk/=p[i];
                                        for(j=0;j<=tmp2;j++)
                                                fac[++tot]=fac[j]*tmp;
                                        tmp*=p[i];
                                }
                        }
                        if(kk<=1) break;
                }
                fac[++tot]=1;
                res=0;
                //qsort(fac,tot+1,sizeof(fac[0]),cmp);
                for(i=0;i<=tot;i++)
                {
                        if((fac[i]+n_t)%m==0&&fac[i]<=n/fac[i])
                            res++;
                }
                if(res==0)
                        printf("No solution\n\n");
                else if(res==1)
                        printf("Only one solution\n\n");
                else printf("Find %lld solutions\n\n",res);
        }
        return 0;
}

 

posted @ 2012-04-12 14:11  'wind  阅读(358)  评论(0编辑  收藏  举报