#include<stdio.h>
#include<stdlib.h>
typedef enum{false,true} bool;
typedef struct farray* pfa;
struct farray {
int data;
bool flag;};
int Pirate(pfa A, int D, int P);
pfa rePirate(pfa A, int D, int left, int right);voidClearflag(pfa A, int left, int right);
int main(){
int D;//n of dimonds
int P;//n of personsscanf("%d %d",&D,&P);
pfa A=(pfa)malloc(P*sizeof(struct farray));
int ret =Pirate(A,D,P);//n of dimonds the first one will be distributedprintf("%d", ret);return0;}
int Pirate(pfa A, int D, int P){returnrePirate(A,D,0,P-1)[0].data;}
pfa rePirate(pfa A, int D, int left, int right){//initialization:D=10,P=7if(right - left +1==3){A[left].data =D-1;A[left].flag =true;A[left +1].data =1;A[left +1].flag =true;A[right].data =0;A[right].flag =true;returnA;}A=rePirate(A,D, left +1, right);Clearflag(A,left,right);
int cnt =(right - left +1)/2;
int i, j=-1;while(cnt){
j++;for(i = left +1; i <= right; i++){if(A[i].data == j&&A[i].flag ==false){A[i].data++;D-=A[i].data;A[i].flag =true;
cnt--;//printf("A[%d].data=%d D=%d cnt=%d\n", i, A[i].data, D, cnt);if(!cnt)break;}}}A[left].data =D;A[left].flag =true;for(i = left +1; i <= right; i++){if(A[i].flag ==false)A[i].data =0;}//for (i = left; i <=right ; i++) printf("%d\t", A[i].data);//printf("\n");returnA;}voidClearflag(pfa A, int left, int right){
int i;for(i = left; i <= right; i++)A[i].flag =false;}