HDU4497 GCD and LCM 數論 素數分解

來源:互聯網
上載者:User

標籤:blog   資料   2014   os   for   io   

題意很簡單首先以前做最簡單的LCM跟CGD的時候都知道先求出兩個數A,B的最大公約數GCD,那麼LCM可以利用  A*B/GCD來求得,這點一開始腦殘了沒想到,結果沒有進行特盤所以錯了,意思就是 題目給的L%G不為0的話就是無解,結果我給判其它的去了,肯定漏了些什麼沒有發現


然後對於 L/G進行素因子分解,同時任意的數都能夠通過素因子分解來表示,所以三個解x,y,z也能分解

L/G = p1^q1*p2^q2....

x = p1^i1*...

y = p1^j1*...

z = p1^k1*...

注意分解過後 咱們先忽略指數,保證x,y,z要互質同時這時候 LCM(x,y,z)的值為  L/G,那麼我們只看第一個,對於素數p1來說,x,y,z中的p1的指數中肯定有一個為0,並且有另一個為q1,外加題目說打亂順序的三個數算不同的 所以最後 排列一下就是六種


#include<iostream>#include<cstdio>#include<list>#include<algorithm>#include<cstring>#include<string>#include<queue>#include<stack>#include<map>#include<vector>#include<cmath>#include<memory.h>#include<set>#include<cctype>#define ll long long#define LL __int64#define eps 1e-8#define inf 0xfffffff//const LL INF = 1LL<<61;using namespace std;//vector<pair<int,int> > G;//typedef pair<int,int > P;//vector<pair<int,int> > ::iterator iter;////map<ll,int >mp;//map<ll,int >::iterator p;#define N 100009int prime[N];bool isprime[N];int k = 0;void init()//依據題目資料範圍處理一定範圍內的素數{memset(isprime,false,sizeof(isprime));for(int i=2;i<100009;i++)if(!isprime[i])for(int j=i*2;j<100009;j+=i)isprime[j]=true;k = 0;for(int i=2;i<100009;i++)if(!isprime[i])prime[k++]=i;}int main() {init();int G,L;int t;scanf("%d",&t);while(t--) {scanf("%d %d",&G,&L);if(L%G) {puts("0");//L肯定得是G的倍數,否則不存在答案,continue;}int tmp = L/G;int now = tmp;int ans = 1;for(int i=0;i<k;i++) {if(prime[i] * prime[i] > tmp)break;if(now%prime[i] == 0) {int cnt = 0;while(now%prime[i] == 0) {now /= prime[i];cnt++;}ans *= cnt * 6;}}if(now != 1) ans *= 6;printf("%d\n",ans);}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.