Hdu 3187 (Euler's Function + dfs)

Source: Internet
Author: User

 


/*

Given a number n, where P (k) = n is satisfied, where k has a number of prime factors <= 3; n <2 ^ 31;
Euler's function:
Phi (k) = k * (1-1/p1) (1-1/p2) (1-1/p3)
= (P1-1) * p1 ^ x * (p2-1) * p2 ^ y * (p3-1) * p3 ^ z;
Phi (1) = 1;
Because n = phi (k), enumeration pi-1, if (n % (pi-1) = 0 & is_prime (pi) is added to pi;
Then, use DFS to make phi (k) = n at most;
Note that phi (1) = 1;
When n = 1, k = 1, 2;


Note that the enumeration (pi-1) here is not the prime factor of n;

 

 

 

2013/04/22-

*/

 

 

[Cpp]
# Include "stdio. h"
# Include "string. h"
# Include "algorithm"
Using namespace std;
Int n, ans;
Int A [4010];
Int cnt;
Int ret;
Int t [3];
Int is_prime (int x)
{
Int I;
For (I = 2; I * I <= x; I ++)
If (x % I = 0) return 0;
Return 1;
}
// Starting from the number x, y has been found, and z is required in total...
Void dfs (int x, int y, int z)
{
Int I;
If (y = z)
{
Int tem = n;
Int f = 0;
For (I = 0; I <z; I ++)
{
If (tem % (t [I]-1 )! = 0)
{
F = 1; break;
}
Tem/= (t [I]-1 );
}
If (f = 0)
{
For (I = 0; I <z; I ++)
{
While (tem % t [I] = 0)
Tem/= t [I];
}
If (tem = 1) ret ++;
}
}
Else
{
For (I = x; I <cnt; I ++)
{
T [y] = A [I];
Dfs (I + 1, y + 1, z );
}
}
}
 
Int main ()
{
While (scanf ("% d", & n )! =-1)
{
If (n = 1)
{
Printf ("2 \ n ");
Continue;
}
Else
{
Int I, j;
Cnt = 0;
// Store the prime factor n into.
For (I = 1; I * I <= n; I ++)
{
If (n % I = 0 & is_prime (I + 1) = 1)
A [cnt ++] = I + 1;
If (n % I = 0 & is_prime (n/I + 1) = 1)
A [cnt ++] = n/I + 1;
}
Sort (A, A + cnt );
// There may be duplicates...
For (I = 1; I <cnt; I ++)
{
If (A [I] = A [I-1])
{
For (j = I; j <cnt-1; j ++)
A [j] = A [j + 1];
Cnt --;
}
}
Ans = 0;
For (I = 1; I <= 3; I ++)
{
Ret = 0;
Dfs (0, 0, I );
Ans + = ret;
}
Printf ("% d \ n", ans );
}
}
Return 0;
}

# Include "stdio. h"
# Include "string. h"
# Include "algorithm"
Using namespace std;
Int n, ans;
Int A [4010];
Int cnt;
Int ret;
Int t [3];
Int is_prime (int x)
{
Int I;
For (I = 2; I * I <= x; I ++)
If (x % I = 0) return 0;
Return 1;
}
// Starting from the number x, y has been found, and z is required in total...
Void dfs (int x, int y, int z)
{
Int I;
If (y = z)
{
Int tem = n;
Int f = 0;
For (I = 0; I <z; I ++)
{
If (tem % (t [I]-1 )! = 0)
{
F = 1; break;
}
Tem/= (t [I]-1 );
}
If (f = 0)
{
For (I = 0; I <z; I ++)
{
While (tem % t [I] = 0)
Tem/= t [I];
}
If (tem = 1) ret ++;
}
}
Else
{
For (I = x; I <cnt; I ++)
{
T [y] = A [I];
Dfs (I + 1, y + 1, z );
}
}
}

Int main ()
{
While (scanf ("% d", & n )! =-1)
{
If (n = 1)
{
Printf ("2 \ n ");
Continue;
}
Else
{
Int I, j;
Cnt = 0;
// Store the prime factor n into.
For (I = 1; I * I <= n; I ++)
{
If (n % I = 0 & is_prime (I + 1) = 1)
A [cnt ++] = I + 1;
If (n % I = 0 & is_prime (n/I + 1) = 1)
A [cnt ++] = n/I + 1;
}
Sort (A, A + cnt );
// There may be duplicates...
For (I = 1; I <cnt; I ++)
{
If (A [I] = A [I-1])
{
For (j = I; j <cnt-1; j ++)
A [j] = A [j + 1];
Cnt --;
}
}
Ans = 0;
For (I = 1; I <= 3; I ++)
{
Ret = 0;
Dfs (0, 0, I );
Ans + = ret;
}
Printf ("% d \ n", ans );
}
}
Return 0;
}

 

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.