列表

详情


NC24762. [USACO 2010 Ope B]Cow Pals

描述

Bessie and all the other cows have an RFID serial number tag in their ear so FJ can mechanically tally them. Many cows have a 'cowpal' whose serial number is equal to the sum of the divisors (that are not the number itself) of their own serial number. Some cows don't have a cowpal because no cow's serial number matches their divisor sum.
Some cows have a superpal. Cows are superpals when their serial numbers make each of them a pal of the other. Cows that are superpals of themselves are shunned; do not consider them!
Given a serial number S (6 <= S <= 18,000), find the first cow with serial number at least S who has a super pal.
By way of example, consider serial number 220 whose divisors are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, and 110. Their sum is 284. Similarly, the divisors of 284 are 1, 2, 4, 71, and 142. Their sum is 220.

输入描述

* Line 1: A single integer: S

输出描述

* Line 1: A single line with two space-separated integers that are the serial number of the first superpal whose serial number is no smaller than S and the serial number of her pal.

示例1

输入:

206

输出:

220 284

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 284ms, 内存消耗: 588K, 提交时间: 2020-04-08 16:41:35

#include<iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    while(n)
    {
        int m=0,sum=0;
        for(int i=1;i<n;i++)
            if(n%i==0) m+=i;
        for(int i=1;i<m;i++)
            if(m%i==0) sum+=i;
        if(sum==n&&m!=n){
            cout<<sum<<" "<<m<<endl;
            break;}
        else n++;
    }
}

C 解法, 执行用时: 116ms, 内存消耗: 340K, 提交时间: 2023-04-11 17:32:43

#include<stdio.h>
int df(int a){
	int i=2;
	int sum=0;
	for(i=2;i<=a/2;i++){

	if(a%i==0){
		sum+=i;
		}
	}
	sum+=1;
	return sum;
}
int main()
{	
	int s;
	scanf("%d",&s);
	int flag=1;
	int a,b;
	while(flag==1){
		
		a=df(s);
		b=df(a);
		if(s!=a) {

			if(s==b){
				printf("%d %d",s,df(s)); 
				break;
				}		
			
		}
		s++;
	}
	return 0;
}

C++11(clang++ 3.9) 解法, 执行用时: 238ms, 内存消耗: 608K, 提交时间: 2019-10-08 17:45:36

#include<iostream>
using namespace std;
int main(){
	int n,m;
	cin>>n;
	while(n){
		int sum=0,ans=0;
		for(int i=1;i<n;i++){
			if(n%i==0)
			sum=sum+i;
		}
		for(int i=1;i<sum;i++){
			if(sum%i==0)
			ans=ans+i;
		}
		if(ans==n&&sum!=n){
			cout<<ans<<" "<<sum<<endl;
			break;
		}
		else
		n++;
	}
	return 0;
} 

Python3 解法, 执行用时: 79ms, 内存消耗: 5220K, 提交时间: 2023-04-11 20:28:34


def disolve(x: int) -> int:
    ans = 1
    for i in range(2, int(x ** 0.5) + 1):
        if x % i == 0:
            ans += i + x // i
    return ans

i = int(input())

while True:
    
    n = disolve(i)
    
    if disolve(n) == i and n != i:
        print(i, n)
        break
    i += 1

pypy3(pypy3.6.1) 解法, 执行用时: 867ms, 内存消耗: 20260K, 提交时间: 2021-05-02 19:58:15

n=int(input())
while(n):
    sum1=0
    ans=0
    for i in range(1,n):
        if(n%i==0):
            sum1+=i;
    for i in range(1,sum1):
        if(sum1%i==0):
            ans+=i      
    if(ans==n and sum1!=n):
            print(ans,sum1)
            break; 
    else:
        n+=1;

上一题