ybt1200 factorization

ybt1200 factorization

Description [title]

Gives a positive integer a, requires a positive integer into several product, i.e. a = a1 × a2 × a3 × ... × an, and 1 <a1≤a2≤a3≤ ... ≤an, ask the several decomposition of the number. Noting also a decomposition of a = a.

[Enter]

Line 1 is the set of n number of test data, followed by n input lines. Each set of test data representing a line, a positive integer including a (1 <a <32768).

[Output]

n lines, each corresponding to an input line of output. The output should be a positive integer indicating the several decomposition meet the requirements.

[Sample input]

2
2
20

[Sample Output]

1
4

【answer】

This problem as long as the number of program output, regardless of the combination. To prevent duplicate, to ensure ascending order factor.

Because each number n in the decomposition, can be divided into less than a factor equal to the square root of n a, and a number n greater than or equal root factor b, and determining a long a, the other is also determined b: b = n / a. So long as the ascending enumeration of a minimal factor in this embodiment as the n, b and ensure decomposition of the decomposition scheme b b is equal to a factor greater than minimum, thus ensuring each factor by a factor n in ascending order scheme.

Further, since the sqrt () function appears in the floating-point square root, it can be transformed at a determined number n of less than or equal root statement, easy to prove that:
\ [a <= \ sqrt n is equivalent to a <= n / a \]

Recursive boundaries are / have / current b / decomposition / not / In the case of the condition (b greater than or equal to the minimum factor a) is / continue to decompose. (Forced punctuate) (tired heart)

According to the above algorithm, write recursive function:

void f(int a,int b) {//a,b分别是当前的n的较小因数和较大因数(操作将累计将b分成最小因数大于等于a的所有方案数)
    for(int i=a;i<=b;i++) {//分解b,实际上i最后不会达到b,在大约根号b时就会跳出函数
        if((!(b%i))&&(b/i>=i)) {//满足b可以被i整除,并且i小于等于根号b
            ans++;//先将b分解为只有唯一因数(它本身)的情况
            f(i,b/i);//然后再讨论把b分成i和b/i的情况
        }
        if(b/i<i)//i大于根号b,结束递归
            return;
    }
    return;//不会被执行到,详见第二行注释,但是会使强迫症舒服
}

Then it is easy to play the complete code:

#include<iostream>
using namespace std;
int ans,n,t;
void f(int a,int b);
int main() {
    cin>>t;
    for(int k=1;k<=t;k++) {
        ans=1;//这要算上n本身作为唯一因数的情况
        cin>>n;
        f(2,n);//前面已经讨论了1*n的情况,这里要跳过,从2开始递归
        cout<<ans<<endl;
    }
    return 0;
}

Happy New Year duck!

Thanks problem solution algorithms help provide!

Guess you like

Origin www.cnblogs.com/Wild-Donkey/p/12233539.html