UVa 11064 - Number Theory

來源:互聯網
上載者:User

標籤:blog   io   os   ar   for   sp   資料   on   2014   

題目:求給頂一個數n,的所有的1 ≤ m ≤ n的m,使得gcd(m,n)≠ 1 且 gcd(m,n)≠ m。

分析:數論,素數篩法,歐拉函數。 

            設pi為n的第i個素數因,k1為第i個素數因子的個數,則有:

            1 ≤ m ≤ n,gcd(m,n)= 1 的m的個數為歐拉函數;

            歐拉函數:φ(n)= n *(1 - 1/p1)*(1 - 1/p2)*(1 - 1/p3)*…*(1 - 1/pt);

            1 ≤ m ≤ n,gcd(m,n)= m 的m的個數為n的所有因數的個數;

            因數個數:f(n)= (k1+1)*(k2+1)*...*(kt+1);

            這裡利用篩法打表計算出150000內的素數,因為資料範圍是20000000000內的,

            所以,不能被前150000內的素數整除的數,也一定是素數,並且每個數n中最多有一個;

            計算輸出 n -  φ(n)- f(n)+ 1 即可{ gcd(1,n)計算了兩次 }。

說明:UVa10299。

#include <iostream>  #include <cstdlib>  #include <cmath>  using namespace std;    int fac[32],num[32];  int prim[150000];  int used[150000];    int main()  {      for (int i = 0 ; i < 150000 ; ++ i)          used[i] = 0;      int save = 0;      for (int i = 2 ; i < 150000 ; ++ i)          if (!used[i]) {              prim[save ++] = i;              for (int j = 2*i ; j < 150000 ; j += i)                  used[j] = 1;          }            int n;      while (cin >> n && n) {          int count = 0,base = 0,m = n;          while (n > 1 && base < save) {              if (n%prim[base] == 0) {                  fac[count] = prim[base];                num[count] = 0;                while (n%prim[base] == 0) {                    n /= prim[base];                      num[count] ++;}count ++;            }              base ++;        }        if (n > 1) {fac[count] = n;num[count] = 1;count ++;}                  long long ans = m;        for (int i = 0 ; i < count ; ++ i)              ans = ans/fac[i]*(fac[i]-1);                //gcd(m,n) = m        int s = 1;        for (int i = 0 ; i < count ; ++ i)        s = s*(num[i]+1);                cout << m-ans-s+1 << endl;      }      return 0;  }

UVa 11064 - Number Theory

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.