HDU 1405
题意:
输入一个数n,输出它的素因子与这个素因子出现的次数.
分析:
用欧拉函数,变下形就好了,不再过多解释.
代码如下:
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <fstream>
5 #include <ctime>
6 #include <cmath>
7 #include <cstdlib>
8 #include <algorithm>
9 #include <set>
10 #include <map>
11 #include <list>
12 #include <stack>
13 #include <queue>
14 #include <iterator>
15 #include <vector>
16
17 using namespace std;
18
19 #define LL long long
20 #define INF 0x3f3f3f3f
21 #define MOD 1000000007
22 #define MAXN 10000010
23 #define MAXM 1000010
24
25 const int maxn = 65537;
26 int a[maxn];
27
28 void euler_phi(int n)
29 {
30 int m = (int)sqrt(n+0.5);
31 //int ans = n;
32 for(int i = 2; i <= m; i++ )
33 if(n%i==0)
34 {
35 //ans = ans/i*(i-1);
36 while(n%i==0)
37 {
38 a[i]++;
39 n /= i;
40 }
41 }
42 if(n > 1)
43 a[n]++;
44 // ans = ans/n*(n-1);
45 // return ans;
46 }
47
48 int main()
49 {
50 int n;
51 int kase = 0;
52 while(scanf("%d", &n)==1&&n>=0)
53 {
54 if(kase)
55 printf("\n");
56 memset(a, 0, sizeof(a));
57 euler_phi(n);
58 printf("Case %d.\n", ++kase);
59 for(int i = 2; i <= n; i++ )
60 if(a[i])
61 printf("%d %d ", i, a[i]);
62 printf("\n");
63 }
64
65 return 0;
66 }