版权声明:本文为博主原创文章,未经博主同意不得转载。
https://blog.csdn.net/u011328934/article/details/36409469
题目链接: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 < N; i++) {
for (int j = i; j < N; j += i)
c[j] += i;
if (c[i] < N)
v[c[i]] = i;
}
}
int main () {
init();
int cas = 1;
while (scanf("%d", &n) == 1 && n) {
printf("Case %d: %d
", cas++, v[n]);
}
return 0;
}