1. 程式人生 > >親和數(因子和)

親和數(因子和)

tput opened splay onclick names des 技術分享 自然 快速

Problem Description
古希臘數學家畢達哥拉斯在自然數研究中發現,220的所有真約數(即不是自身的約數)之和為:

1+2+4+5+10+11+20+22+44+55+110=284。

而284的所有真約數為1、2、4、71、 142,加起來恰好為220。人們對這樣的數感到很驚奇,並稱之為親和數。一般地講,如果兩個數中任何一個數都是另一個數的真約數之和,則這兩個數就是親和數。

你的任務就編寫一個程序,判斷給定的兩個數是否是親和數

Input 輸入數據第一行包含一個數M,接下有M行,每行一個實例,包含兩個整數A,B; 其中 0 <= A,B <= 600000 ;

Output 對於每個測試實例,如果A和B是親和數的話輸出YES,否則輸出NO。

Sample Input 2 220 284 100 200 比較快速的計算因子和,到根下。 技術分享圖片
 1 #include<iostream>
 2 #include<iomanip>
 3 //#include<bits/stdc++.h>
 4 #include<cstdio>
 5 #include<algorithm>
 6 #include<cmath>
 7
#define PI 3.14159265358979 8 #define LL long long 9 #define INF 10000001000 10 #define eps 0.00000001 11 using namespace std; 12 int main() 13 { 14 int T; 15 cin>>T; 16 while(T--) 17 { 18 int a,b; 19 cin>>a>>b; 20 int sum1=0,sum2=0; 21 for
(int i=2;i<=sqrt(a);++i) 22 { 23 if(a%i==0) {if(a/i==i) sum1+=i; 24 else {sum1+=i,sum1+=a/i;}} 25 } 26 for(int i=2;i<=sqrt(b);++i) 27 { 28 if(b%i==0) {if(b/i==i) sum2+=i; 29 else {sum2+=i,sum2+=b/i;}} 30 } 31 sum1++;sum2++; 32 // cout<<sum1<<" "<<sum2;return 0; 33 if(sum1==b&&sum2==a) cout<<"YES"<<endl; 34 else cout<<"NO"<<endl; 35 36 } 37 38 39 }
View Code

親和數(因子和)