[Hdoj] 1276 soldiers queue Training Problems

Source: Internet
Author: User

At first glance, this question is very similar to utherv's problem. The difference is that each time it starts from 1. The solution is similar. Recursive Formula for mathematical solutions.
It is assumed that after the k-th report, the remaining number of people cannot exceed 3.
If the first K times is a 1-3 Report, the number of the current index n of the three numbers can be pushed to N + (n-1)/2 before the Previous Report, which is easy to understand, because every three people need to remove the third person, (n-1)/2 can know that a few people have been removed, plus the basic number n is the number of the last report;
If the K number is 1-2, this is very simple. The number of the current index n is 2n-1 in the previous report.
Therefore, the county calculates the number of reports and returns the initial values of the remaining numbers in reverse order.

 1 #include <cstdio> 2 #include <cstring> 3  4 int main() { 5     int t, n, m; 6     int i, j; 7     int a[4]; 8  9     scanf("%d", &t);10     while (t--) {11         scanf("%d", &n);12         m = 0;13         while (n > 3) {14             ++m;15             if (m & 1)16                 n -= n/2;17             else18                 n -= n/3;19         }20         for (i=1; i<=n; ++i)21             a[i] = i;22         while (m > 0) {23             if (m & 1) {24                 for (i=1; i<=n; ++i)25                     a[i] = 2*a[i]-1;26             } else {27                 for (i=1; i<=n; ++i)28                     a[i] = a[i]+(a[i]-1)/2;29             }30             --m;31         }32         printf("%d", a[1]);33         for (i=2; i<=n; ++i)34             printf(" %d", a[i]);35         printf("\n");36     }37 38     return 0;39 }

 

[Hdoj] 1276 soldiers queue Training Problems

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.