约数的个数

算法:

N = (1+p1)*(1+p2) *(1+p3)...*(1+pn)

View Code
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<string>
#include<math.h>
#include<map>
#include<set>
#include<algorithm>
using namespace std;

int n, a, cnt;
int prime[41000];
int hash[41100]={0};
int visit[41100];

void Get_Prime( )
{
  cnt = 0;
  int x = (int)sqrt(1000000000.0);
  int y = (int)sqrt( x * 1.0 );
  for( int i = 2; i <= y; i++)
  {
    if( !hash[i] )
    {
      for( int j = i + i; j <= 31630; j += i )
           hash[j] = 1;    
        
    }     
  }
  for( int i = 2; i <= 31630; i++)
       if( !hash[i] )
           prime[cnt++] = i;     
}

void Get_Fac( int x)
{
  int id = 0;
  int ans = 1;
  int t = x;
  while( x )
  {
      while( x % prime[id] == 0 && x!= 1)
             x /= prime[id], hash[prime[id]]++;    
      id++;   
      if( x == 1)
      {
         for( int i = 1; i <= 31630 && i <= t; i++)
         {
           if( hash[i] )
             ans *= (1 + hash[i] );     
           hash[i] = 0;      
         }
         break;
      }
      if( id >= cnt )
      {   
         for( int i = 1; i <= 31630 && i <= t; i++)
         {
           if( hash[i] )
             ans *= (1 + hash[i] );      
           hash[i] = 0;
         }
         ans *= 2;
         break;    
      }   
  }
  printf("%d\n",ans);
}
 


int main( )
{
  int t;
  Get_Prime( );
  memset(hash,0,sizeof(hash));
  while( scanf("%d",&n)!= EOF )
  {  
    for( int i = 0; i < n; i++)
    {
      scanf("%d",&t);
      Get_Fac(t);
    }       
  }
  return 0;  
}

posted on 2012-08-21 20:52  more think, more gains  阅读(177)  评论(0编辑  收藏  举报

导航