PAT (Advanced Level) 1103. Integer Factorization (30)

暴力搜索。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;

int n,k,p,top;
int path[1000];
int flag=0;
int ans_path[1000],ans_num=0,ans_len;

void dfs(int x,int pre,int sum,int num)
{
    if(sum>n) return;
    if(x>k) return;
    if(sum==n)
    {
        if(x!=k) return;

        flag=1;
        if(num>ans_num)
        {
            ans_num=num;
            ans_len=x;
            for(int i=0;i<x;i++) ans_path[i]=path[i];
        }

        return;
    }
    for(int i=pre;i>=1;i--)
    {
        path[x]=i;
        dfs(x+1,i,sum+int(pow(i,p)+0.5),num+i);
    }
}
int main()
{
    scanf("%d%d%d",&n,&k,&p);
    top=(int) (pow(1.0*n,1.0/p)+0.5);
    top=min(top,n/k); //加这个剪枝速度还可以提高一些

    dfs(0,top,0,0);
    if(flag==0) printf("Impossible\n");
    else
    {
        printf("%d = ",n);

        for(int i=0;i<ans_len;i++)
        {
            printf("%d^%d",ans_path[i],p);
            if(i!=ans_len-1) printf(" + ");
        }
    }
    return 0;
}
时间: 2024-08-05 15:22:17

PAT (Advanced Level) 1103. Integer Factorization (30)的相关文章

【PAT甲级】1103 Integer Factorization (30分)

1103 Integer Factorization (30分) 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 are supposed to write a program to find the K?P factorization of N for any positive integers N, K an

1103. Integer Factorization (30)【搜索+剪枝】——PAT (Advanced Level) Practise

题目信息 1103. Integer Factorization (30) 时间限制1200 ms 内存限制65536 kB 代码长度限制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 are supposed to write a program to find the K-P factoriz

1103&#160;Integer Factorization (30)

1103 Integer Factorization (30 分) 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 are supposed to write a program to find the K−P factorization of N for any positive integers N, K a

PAT甲题题解-1103. Integer Factorization (30)-(dfs)

该题还不错~. 题意:给定N.K.P,使得可以分解成N = n1^P + - nk^P的形式,如果可以,输出sum(ni)最大的划分,如果sum一样,输出序列较大的那个.否则输出Impossible. dfs枚举,为了防止超时,这里要预先将从1开始的i^p的值存储在factor数组中,直到i^p>n.然后dfs深度优先搜索,相当于把问题一步步分解,即若第一个因子是n1,则接下来我们要判断N-n1^p.k-1是否可行.同时存储当前因子的总和sum,要取sum最大的:还有上一次相加的因子的索引las

浙大 PAT Advanced level 1026. Table Tennis (30)

A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the available table with the smallest number. If all the

PAT (Advanced Level) 1026. Table Tennis (30)

情况比较多的模拟题. 交了50发的样子才AC......AC之后我的天空星星都亮了. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<vector> using namespace std; struct

PAT (Advanced Level) 1080. Graduate Admission (30)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace std;

PAT (Advanced Level) 1004. Counting Leaves (30)

简单DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<vector> using namespace std; const int maxn=100+10; vector<int>g[maxn]; int n,m; int ans[maxn]; int root; i

PAT (Advanced Level) 1113. Integer Set Partition (25)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; long long a[100000+10]; long long sum=0,sum2; i