Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- int n,p,x[100],produs=0;
- void maxim(int x[]){
- int prod=1;
- int i;
- for(i=0;i<p;++i){
- prod*=x[i];
- }
- if(prod>produs){
- produs = prod;
- }
- }
- int verif(int x[], int k){
- int s=0;
- if(k>0){
- if(x[k]<x[k-1])
- return 0;
- }
- for(int i=0;i<=k;++i){
- s+=x[i];
- }
- return (s<=n);
- }
- int solutie(int x[], int k){
- int s=0;;
- if(k!=p)
- return 0;
- for(int i=0;i<p;++i)
- s+=x[i];
- return (s==n);
- }
- void back(){
- int i;
- int k=0;
- for(i=0;i<p;++i)
- x[i]=0;
- while(k>-1){
- if(solutie(x,k)){
- maxim(x);
- k--;
- }
- else{
- if(x[k]<n){
- x[k]++;
- if (verif(x,k))
- k++;
- }
- else {
- x[k]=0;
- k--;
- }
- }
- }
- }
- int main(){
- cin>>n>>p;
- back();
- cout<<produs;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement