HDU 1492 The number of divisors(約數) about Humble Numbers

來源:互聯網
上載者:User

做該題大概要注意以下幾方面:

①每個自然數都可分解成質因數相乘的形式

②數分為三類:質數是不為1,並且因數只為1和其本身的數。和數是不為1且不為質數的數。1既不是質數也非和數。

③題目雖說輸入的是64-bits signed integer,單輸入一定沒有負數。比如輸入-2,其因數為-1,-2,1,2。-1、-2都不能由2、3、5、7的任意組合得到。

 

大概思路:

把輸入資料分解,也就是找到2、3、5、7的個數,分別用a2、a3、a5、a7記錄,在AC代碼中是用divide()函數實現的。

其次,就是找出由2、3、5、7可以組合出多少個不同的因數,在count()中四重for迴圈實現。

其中在找組合出的因數時可以肯定的是:該次找的因數一定不同於以前所找到的任何因數。因為該次得到的因數跟以前任何一次得到的因數所用的2、3、5、7

的組合一定不同(2、3、5、7各自個數不同)。

 

本人在做該題時把count()函數寫成了void count(int n),結果WA了兩次!!!

 

AC代碼:250MS

#include<iostream>using namespace std;int a2,a3,a5,a7;  //記錄質因數個數 void divide(__int64 n)  //搜出每個質因數的個數 {     a2=a3=a5=a7=0;              while(n!=1)     {         if(n%2==0)         {             a2++;             n/=2;         }         if(n%3==0)         {             a3++;             n/=3;         }         if(n%5==0)         {             a5++;             n/=5;         }         if(n%7==0)         {             a7++;             n/=7;         }     }  }   void count()    //暴力枚舉每種組合 {     int cou=0;     for(int i=0;i<=a2;i++)     {          for(int j=0;j<=a3;j++)          {                  for(int k=0;k<=a5;k++)                  {                          for(int p=0;p<=a7;p++)                          {                                  cou++;                          }                  }          }     }          cout<<cou<<endl;}                                     int main(){    __int64 n;        while(cin>>n,n)    {        divide(n);                count();    }        return 0;}

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.