UVA - 11728 Alternate Task (唯一分解定理)

Little Hasan loves to play number games with his friends.One day they were playing a game where one of them will speak out a positive numberand the others have to tell the sum of its factors. The first one to say itcorrectly wins. After a while they got
bored and wanted to try out a differentgame. Hassan then suggested about trying the reverse. That is, given a positivenumber
S, they have to find a numberwhose factors add up to S. Realizingthat this task is tougher than the original task, Hasan came to you forhelp.  Luckily Hasan owns a portableprogrammable device and you have decided to burn a
program to this device.Given the value of S as input to theprogram, it will output a number whose sum of factors equal to
S.

Input

 

Each case of input will consist of a positive integer
S<=1000.
The last case is followedby a value of 0.

 

Output

 

Foreach case of input, there will be one line of output. It will be a positiveinteger whose sum of factors is equal to
S.If there is more than one such integer, output the largest. If no such numberexists, output
-1. Adhere to theformat shown in sample output.

Sample Input                            Output for Sample Input


1

102

1000

0

 


Case 1: 1

Case 2: 101

Case 3: -1

 


ProblemSetter: Shamim Hafiz, Special Thanks: Sohel Hafiz

题意:输入一个正整数S,求一个最大的正整数N,使得N的所有因子的和是S

思路:统计1000内的数的因子和,求解

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1005;

int s, sum[maxn];

void init() {
	memset(sum, 0, sizeof(sum));
	for (int i = 1; i < maxn; i++)
		for (int j = 1; j <= i; j++)
			if (i % j == 0)
				sum[i] += j;
}

int main() {
	int cas = 1;
	init();
	while (scanf("%d", &s) != EOF && s) {
		int flag = 0;
		printf("Case %d: ", cas++);
		for (int i = 1000; i >= 1; i--) {
			if (sum[i] == s) {
				printf("%d\n", i);
				flag = 1;
				break;
			}
		}
		if (!flag)
			printf("-1\n");
	}
	return 0;
}
时间: 2024-11-06 22:22:15

UVA - 11728 Alternate Task (唯一分解定理)的相关文章

uva 11728 - Alternate Task(数论)

题目链接:uva 11728 - Alternate Task 题目大意:给出S,求N,要求N所有的因子和为S. 解题思路:枚举因子i,所有整除i的数和加上i. #include <cstdio> #include <cstring> const int N = 1005; int n, c[N], v[N]; void init () { memset(c, 0, sizeof(c)); memset(v, -1, sizeof(v)); for (int i = 1; i &l

UVA 11728 - Alternate Task (数论)

Uva 11728 - Alternate Task 题目链接 题意:给定一个因子和,求出对应是哪个数字 思路:数字不可能大于因子和,对于每个数字去算出因子和,然后记录下来即可 代码: #include <stdio.h> #include <string.h> const int N = 1005; int n, ans[N]; void init() { memset(ans, -1, sizeof(ans)); for (int i = 1; i <= 1000; i+

UVA 11728 - Alternate Task 数学

Little Hasan loves to play number games with his friends. One day they were playing a game whereone of them will speak out a positive number and the others have to tell the sum of its factors. The?rst one to say it correctly wins. After a while they

UVa 11728 Alternate Task

方法:数论 因为n <= factorsum(n) <= 1000, 可以暴力打表,求出所有n <= 1 所对应的factorsum(n), 求的时候甚至不需要O(n^0.5) 来判断因子,O(n) 判断也可以通过. 然而如果factorsum的范围很大该如何求呢.可以推导得出 .那么可以素数筛选预处理出素数,然后利用以上公式求解. code: #include <cstdio> #include <cstring> #include <algorithm&

【数论,水题】UVa 11728 - Alternate Task

题目链接 题意:给出一个数S,求一个最大的数,使这个数所有的因子之和为S; 这个所谓“因子之和”不知道有没有误导性,因为一开始以为得是素数才行.后来复习了下小学数学,比如12的因子分别是1,2,3,4,6,12... 我竟无言以对T^T... 感觉复杂度应该能继续优化的..没想到好的.. 代码: 1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5

UVa 1635 Irrelevant Elements (唯一分解定理 || 组合数学)

题目 题目大意 对于给定的\(n\)个数\(a_1\), \(a_2\), ···, \(a_n\), 依次求出相邻两数之和, 将得到一个新数列.重复上述操作, 最后结果将变成一个数.问这个数除以\(m\)的余数将与哪些数无关? 例如\(n = 3\), \(m = 2\)时, 第一次求和得到\(a_1 + a_2\), \(a_2 + a_3\), 再求和得到\(a_1 + 2a_2 + a_3\), 它除以\(2\)的余数和\(a_2\)无关.\(1 ≤ n ≤ 10^5\), \(2 ≤

uva 10375 唯一分解定理 筛法求素数【数论】

唯一分解理论的基本内容: 任意一个大于1的正整数都能表示成若干个质数的乘积,且表示的方法是唯一的.换句话说,一个数能被唯一地分解成质因数的乘积.因此这个定理又叫做唯一分解定理. 举个栗子:50=(2^1)*(5^2) 题目一般的思路就是要把素数表打出来,eg上面的例子 e={1,0,2,0,0......} 下面是两个题目,仅说说大致的思想: 题目一: E=(X1*X3*X4* ...*Xk)/X2   判断E是不是整数 如果把(X1*X3*X4* ...*Xk)分解成素数相乘,将X2也分解成素

uva 10791 Minimum Sum LCM ( 唯一分解定理 )

使用唯一分解定理的时候不一定要打出素数表,这句话是相对上一篇来讲的.做这道题目之前我对唯一分解定理方法的理解不完全. 现在多想到了一些 唯一分解,将当前需要分解的n用因子将其分解表达.需要试因子. 因子的枚举应该是从2开始(从1开始没有意义),当当前数字n可以整除当前因子i时,就使其不断除以i,直到不能整除. 这个步骤实际上已经在根本上避免了出现像4.6这种因子在唯一分解式中的出现--之前的因子2和3已经将其代替了.所以可证明唯一分解时并不一定需要构造素数表 针对本题来说,最小公倍数的最小和,有

UVA 10375 Choose and divide(唯一分解定理)

这么大数的乘法.除法运算,肯定不能先全部乘起来,我的思路是计算出分子.分母上的每个数的个数(因为最大的数为10000,可以开一个数组记录个数). 利用了随机数方法终于知道错在哪了,中间如果出现连乘还是会溢出,这点没想到,以下是我的溢出代码: #include<stdio.h> #include<math.h> #include<iostream> #include<string.h> #include<stdlib.h> #include<