[俺们学校的题]你是天才

你是天才

不我不是

  告诉你一个数的所有约数(包括1和该数本身)的和,以及约数的倒数之和,你能推出这个数是多少吗?

  如果你能知道,那你就是天才了。

  包含多组数据。  每组数据有三个正整数,A,B1和B2(1<=A,B1,B2<=10^9),其中A为C的约数和,而对于C的所有约数的倒数之和B,为避免精度误差,以分数B1/B2的形式给出。  输入文件以一行“0 0 0”结束。

解题思路

  首先,约数倒数和为1/a1+1/a2...1+/an此时我们将它通分为an/n+an-1/n+...a1/n,这是由于乘法的对称性以及他们都是n的约数,所以说n为他们的最小公倍数。

  然后提一个1/n出来,原式为1/n(a1+a2..+an),后面一段题目给了。所以用和÷倒数和=n

  注意一下还要返回去算是否合法

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long read(){
    long long res=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        res=res*10+(ch-'0');
        ch=getchar();
    }
    return res*f;
}
#define ll long long
ll k,a,b1,b2,ans;
bool check(int x,int y){
    int tot=0;
    for(int i=1;i*i<=x;++i){
        if(x%i==0){
            tot+=i;
            if(i!=x/i){
                tot+=(x/i);
            }
        }
    }
    if(tot==y)return 0;
    else return 1;
}
int main(){
while(1){
    a=read();b1=read();b2=read();
    if(a==0&&b1==0&&b2==0)break;
    ans=a*b2/b1;
    if(ans*b1!=a*b2)cout<<0<<endl;
    else if(1.0*b1/b2<1.0)cout<<0<<endl;
    else if(check(ans,a))cout<<0<<endl;
    else{
        cout<<1<<" "<<ans<<endl;
    }
}
return 0;
}
View Code
原文地址:https://www.cnblogs.com/clockwhite/p/12039993.html