【数论,水题】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 #include<cmath>
 6 using namespace std;
 7 const int maxn = 1010;
 8 int vis[maxn], prime[maxn], cnt;
 9 void pre()
10 {
11     int m = sqrt(maxn+0.5);
12     vis[0] = 1;
13     vis[1] = 1;
14     for(int i = 2; i <= m; i++)
15         if(!vis[i])
16             for(int j = i*i; j < maxn; j+=i)
17                 vis[j] = 1;
18     cnt = 0;
19     for(int i = 2; i < maxn; i++)
20         if(!vis[i])
21             prime[cnt++] = i;
22 }
23
24 int main()
25 {
26     pre();
27     int S, kase = 0;
28     while(~scanf("%d", &S) && S)
29     {
30         if(S == 1) {printf("Case %d: 1\n", ++kase); continue;}
31         bool exist = false;
32         int i;
33         for(i = S-1; i >= 1; i--)
34         {
35 //            if(!vis[i])
36 //            {
37                 //cout << i << endl;
38                 int sum = 0; bool ok = true;
39                 for(int j = 1; j <= sqrt(i); j++)
40                 {
41                     int tmp = i/j;
42                     //cout << tmp << endl;
43                     if(sum > S) {ok = false; break;}
44                     if(tmp*j != i) continue;
45                     if(tmp != j) sum += j;
46                     sum += tmp;
47                 }
48                 if(sum == S) {exist = true; break;}
49 //            }
50         }
51         if(exist) printf("Case %d: %d\n", ++kase, i);
52         else printf("Case %d: -1\n", ++kase);
53
54     }
55     return 0;
56 }

时间: 2024-10-08 02:54:12

【数论,水题】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

方法:数论 因为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 数学

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 (唯一分解定理)

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

BZOJ1968: [Ahoi2005]COMMON 约数研究(数论 水题)

Description Input 只有一行一个整数 N(0 < N < 1000000). Output 只有一行输出,为整数M,即f(1)到f(N)的累加和. Sample Input 3 Sample Output 5 Solve: 数论水题,求因数又不是质因数,所以,只要求出对于[1 , n]有多少个倍数,就表示[1 , n]中以i为因数的是哪些,加起来就可以了 Code: 1 #include <bits/stdc++.h> 2 using namespace std;

HDU 2674 N!Again (数论-水题)

N!Again Problem Description WhereIsHeroFrom:             Zty, what are you doing ? Zty:                                     I want to calculate N!...... WhereIsHeroFrom:             So easy! How big N is ? Zty:                                    1 <=

HDU 4143 A Simple Problem(数论-水题)

A Simple Problem Problem Description For a given positive integer n, please find the smallest positive integer x that we can find an integer y such that y^2 = n +x^2. Input The first line is an integer T, which is the the number of cases. Then T line

HDU 3215 The first place of 2^n (数论-水题)

The first place of 2^n Problem Description LMY and YY are mathematics and number theory lovers. They like to find and solve interesting mathematic problems together. One day LMY calculates 2n one by one, n=0, 1, 2,- and writes the results on a sheet