Topic Information
1103. Integer Factorization (30)
Time limit MS
Memory Limit 65536 KB
Code length limit 16000 B
The k-p factorization of a positive integer N is to write N as the sum of the p-th power of K positive integers. You is supposed to write a program to find the k-p factorization of n for any positive integers N, K and P.
Input Specification:
Each input file contains one test case which gives in a line the three positive integers N (<=400), K (<=n) and P (1
Thinking of solving problems
Search and pruning
AC Code
#include <cstdio>#include <cstring>#include <cmath>#include <functional>#include <algorithm>using namespace STD;intRMX =-1, rs[405], trs[405], pw[ A];intN, K, p, has =0;voidFindintTintLitintLocintSUM) {if(T = =0&& Loc > K) {if(Sum > RMX) {rmx = SUM;memcpy(RS, TRS,sizeof(RS)); } has =1; }if(Loc > K)return; for(inti = lit; I >=1; -I.) {if(Pw[i] <= T-(K-loc) && pw[i] * (K-loc +1) {>= T) {//PruningTrs[loc] = i; Find (T-pw[i], I, loc +1, sum + i); } }}intMain () {scanf("%d%d%d", &n, &k, &p); for(inti =0; I < +; ++i) {Pw[i] = (int)(POW(I, p) +0.1); } Find (N, -,1,0);if(HAS) {printf("%d =%d^%d", N, rs[1], p); for(inti =2; I <= K; ++i) {printf("+%d^%d", Rs[i], p); }printf("\ n"); }Else{printf("impossible\n"); }return 0;}
1103. Integer Factorization (30) "Search + prune"--pat (Advanced level) practise