2-10. Pirate sharing (25) (zjupat mathematics)

Source: Internet
Author: User

Math Pat zju

Link: http://pat.zju.edu.cn/contests/ds/2-10


P pirates stole D diamond and then came to the high seas to share the stolen goods. They agreed to the following policy:

First, the p pirates determine the serial number of 1-P by drawing lots. Then, a allocation scheme is proposed by the 1st pirate (the specific quantity of each pirate should be given in the scheme). If an absolute majority (that is, more than half) including the 1, the allocation scheme will be followed; otherwise, the first part will be put into the sea to feed sharks; and then, similarly, the solution will be proposed by the 2nd and 3rd pirates, until an absolute majority of consent can be obtained, or only the last pirate is left, exclusive to all diamonds. Please write a program to show the quantity of diamond allocated by yourself in the diamond allocation plan of pirate 1st.

The following three assumptions are attached:

1) "smart" and "greedy" assumptions: every pirate can always take the maximization of his own interests as the code of conduct;
2) "humanized" assumption: if as many diamonds as possible can be obtained, the pirates will not intentionally commit together to death;
3) "no prejudice" assumes that there is no personal grievance between pirates, and the order of diamond given to other pirates is prioritized by a small serial number.

Input format description:

Enter two positive integers D and P (3 <= P <= d <= 100 ).

Output format description:

Output the quantity of diamond allocated in the diamond allocation plan of pirate 1st.

Sample input and output:

Serial number Input Output
1
10 7
6
2
3 3
2
3
100 3
99
4
100 100
49

PS:

When there are only three pirates, we need to make a special decision, because when there are only three pirates, we only need to give one diamond to one of the other two! For the rest of the time, one of the half pirates should be given two diamonds, and the other half should be given one diamond for each!


The Code is as follows:

#include <cstdio>int main(){    int D, P;    while(~scanf("%d%d",&D,&P))    {        int ans;        if(P == 3)        {            ans = D-P/2;        }        else            ans = D-(P)/2-1;        printf("%d\n",ans);    }    return 0;}


2-10. Pirate sharing (25) (zjupat mathematics)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.