Problem 1607 Greedy division from http://acm.fzu.edu.cn/problem.php?pid=1607

来源:互联网 发布:查士丁尼瘟疫 知乎 编辑:程序博客网 时间:2024/05/01 19:41

 Problem Description

Oaiei has inherited a large sum of wealth recently; this treasure has n pieces of golden coins. Unfortunately, oaiei can not own this wealth alone, and he must divide this wealth into m parts (m>1). He can only get one of the m parts and the m parts have equal coins. Oaiei is not happy for only getting one part of the wealth. He would like to know there are how many ways he can divide the wealth into m parts and he wants as many golden coins as possible. This is your question, can you help him?

 Input

There are multiply tests, and that will be 500000. For each test, the first line is a positive integer N(2 <= N <= 1000000), N indicates the total number of golden coins in the wealth.

 Output

For each test, you should output two integer X and Y, X is the number of ways he can divide the wealth into m parts where m is large than one, Y is the number of golden coins that he can get from the wealth.

 Sample Input

568

 Sample Output

1 13 33 4

 Hint

There are huge tests, so you should refine your algorithm.

 Source

FOJ月赛-2008年5月

[cpp] view plaincopy
  1. #include<cstdio>  
  2. #include<iostream>  
  3. #include<algorithm>  
  4. #include<cmath>  
  5. #include<memory.h>  
  6. using namespace std;  
  7. int prime[1000005];  
  8. int minp[1000005];  
  9. int n, m, ans, k, choices;  
  10. int main(){  
  11.   
  12.     //freopen("in.txt", "r", stdin);  
  13.     /*int d = sqrt(1000005); 
  14.     memset(prime, 0, sizeof(prime)); 
  15.     for(int i=2;i<=d;++i){ 
  16.         if(!prime[i]){ 
  17.             prime[i] = 1; 
  18.             minp[i] = i; 
  19.             for(int j=i*2;j<=1000000;j+=i){ 
  20.                 if(!prime[j]){ 
  21.                     prime[j] = 2; 
  22.                     minp[j] = i; 
  23.                 } 
  24.  
  25.             } 
  26.         } 
  27.     }*/  
  28.     memset(prime,0,sizeof(prime));  
  29.     int ddd=sqrt(1000000.0)+1;  
  30.     for(int i=2;i<=1000000;i++)  
  31.     {  
  32.         if(prime[i])continue;  
  33.         minp[i]=i;  
  34.         if(i>ddd)continue;  
  35.         for(int j=i*2;j<=1000000;j+=i)  
  36.             if(prime[j]==0)  
  37.                 prime[j]=1,minp[j]=i;  
  38.     }  
  39.   
  40.   
  41.     while(scanf("%d", &n)!=EOF){  
  42.         m = n;  
  43.         ans = 1;  
  44.   
  45.         while(n>1){  
  46.             k = minp[n];  
  47.             choices = 1;  
  48.             while(n%k==0){  
  49.                 choices++;  
  50.                 n /= k;  
  51.             }  
  52.             ans *= choices;  
  53.         }  
  54.         printf("%d %d\n", ans-1, m/minp[m]);  
  55.     }  
  56.   
  57.     //fclose(stdin);  
  58.   
  59. }