UVA 10726-coco Monkey (number theory)

Source: Internet
Author: User

Coco Monkey

The story of a time long ago. There were these sailors stranded on a beautiful island full off
Coconut trees. And what does our sailors does with the coconut trees? They collect all the coconuts and
Divide those among themselves and the monkeys that they brought with them!
History tells us this there were S sailors on the island, and they collected C coconuts in total. At
Nightfall they decided to leave the coconuts in a secret place and divide it equally in the morning. But
As the night grew darker, the sailors began to get impatient. At midnight, one of the sailors got up. He
Dug up the coconuts and saw that after dividing the coconuts equally into S baskets, there is exactly
M coconuts left. These, as you must has guessed, goes to the M monkeys that they has brought
with them. Now, we greedy sailor took one of those baskets for himself and left the remaining s−1
Ones for his Commodores. History repeats. So we had another sailor succumbing to the temptation of
Juicy coconuts. He again, divided the remaining coconuts equally in S baskets only to find M coconuts
Extra. One basket for himself, M coconuts for the M monkeys and the remaining s−1 baskets
Fellows–that ' s how it goes.
Before the night is over each and every sailor played he part exactly once. At dawn when they
Started to divide the remaining coconuts, they found this there ' s no extra coconut left for the monkeys.
But the monkeys would isn't complain, nor would any of the sailors. They had their share alright.
Historians, as one would expect, can be trusted for facts not for the figures. So whatever they would
Say about the values of S, C and M cannot is trusted entirely. But given the values of S, M. and an
Interval for C one can identify possible values for C. As a programmer need to count the
Number of valid C values on a given range [low, high] inclusive.
Input
The first line of the input gives you the number of test cases, T (1≤t≤15). Then T test cases follow.
Each of the test cases consists of four integers and are presented one in each line. The first integer gives
You the number of sailors S (1 < s≤100) and then the next integer gives you the number of monkeys M
(0≤m < S). The next integers would give you the value of low and high (0≤low≤high≤108
).
Output
For each of the test cases, you need to print one line of output. The output for each test case starts
With the test case number, followed by the number of valid C values in the range [low, high]. For the
Exact format of output, please consult the sample Input/output section.
Illustration:the First test case gives 3 sailors, 2 monkeys and a range of [$] to choose from.
If you're a try to do the math in hand, you'll see this only would satisfy the scenario. So the answer
The for the first test case had to be 1. Here ' s The brief illustration:


Sample Input
3
3 2 49 50
4 2 5 10000
6 4 1 100000000


Sample Output
Case 1:1
Case 2:10
Case 3:357

Test instructions: Greedy people have s, monkeys have M, everyone will first put all the coconut (-m) again, and ask how many data in the range L to R is set up.

Tip: Start enumerating from the last person, judging whether the condition is out of bounds or not divisible (s-1)

1#include <cstdio>2#include <cstring>3#include <cmath>4#include <iostream>5 #defineLL Long Long6 7 using namespacestd;8 9 LL s,m,l,r,t;Ten intcases=0; One voidSolve () A { -     BOOLflag=true; -     intans=0, J; the      for(intI=1;; i++)///the rest of the list -     { -         if(!flag) -              Break; +LL remain=s*i; -         if(remain% (S-1))///must be able to divide evenly +             Continue; A          for(j=0; j<s;j++) at         { -remain=s*remain/(S-1)+m; -             if(Remain>r)///Out of bounds -             { -flag=false; -                  Break; in             } -             if(remain% (S-1) &&j!=s-1)///not divisible (s-1) and J is less than (s-1) to                  Break; +         } -         if(J==S&AMP;&AMP;REMAIN&GT;=L&AMP;&AMP;REMAIN&LT;=R)///J=s means the number of cycles is enough. theans++; *     } $printf"Case %d:%d\n",++Cases,ans);Panax Notoginseng } -  the intMain () + { ACin>>T; the      while(t--) +     { -Cin>>s>>m>>l>>R; $ Solve (); $     } -     return 0; -}

UVA 10726-coco Monkey (number theory)

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.