Topic links
Test instructions: Give a number S, ask for a maximum number, so that the sum of all the factors of this number is s;
The so-called "factor sum" does not know that there is no misleading, because at first thought is a prime number. Later reviewed the next primary mathematics, such as 12 of the factors are 1,2,3,4,6,12 ...
I was speechless t^t ...
The sense of complexity should continue to be optimized. I didn't think so.
Code:
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <cmath>6 using namespacestd;7 Const intMAXN =1010;8 intVIS[MAXN], PRIME[MAXN], CNT;9 voidPre ()Ten { One intm = sqrt (maxn+0.5); Avis[0] =1; -vis[1] =1; - for(inti =2; I <= m; i++) the if(!Vis[i]) - for(intj = i*i; J < Maxn; j+=i) -VIS[J] =1; -CNT =0; + for(inti =2; i < MAXN; i++) - if(!Vis[i]) +prime[cnt++] =i; A } at - intMain () - { - pre (); - intS, Kase =0; - while(~SCANF ("%d", &s) &&S) in { - if(S = =1) {printf ("Case %d:1\n", ++kase);Continue;} to BOOLexist =false; + inti; - for(i = s1; I >=1; i--) the { * //if (!vis[i]) $ // {Panax Notoginseng //cout << i << Endl; - intsum =0;BOOLOK =true; the for(intj =1; J <= sqrt (i); J + +) + { A intTMP = i/J; the //cout << tmp << Endl; + if(Sum > S) {OK =false; Break;} - if(Tmp*j! = i)Continue; $ if(tmp! = j) Sum + =J; $Sum + =tmp; - } - if(sum = = S) {exist =true; Break;} the // } - }Wuyi if(exist) printf ("Case %d:%d\n", ++Kase, i); the Elseprintf"Case %d: -1\n", ++Kase); - Wu } - return 0; About}
View Code
"Number theory, water problem" UVa 11728-alternate Task