Blue Bridge cup algorithm training ALGO-121 monkeys to divide apples

Source: Internet
Author: User

Algorithm training monkey to divide apple time limit: 1.0s memory Limit: 256.0MB problem description Autumn arrived, n monkeys picked a lot of apples put into the cave, agreed to the second balance points. These monkeys adore Monkey King monkey, so all want to leave him some apples. The first monkey crept into the cave, divided the apples evenly into n parts, ate the remaining m apples, hid them, and finally put the remaining apples together again. The monkeys quietly came to the cave, all doing the same operation, just about every time there are a few m apples. The next day, the monkeys came to the cave, the rest of the apples into the n points, the coincidence, or the remaining m. Ask, how many apples did these monkeys pick up at least. Input format two integers, N m output format an integer that indicates the original number of apples sample input 5 1 sample output 15621 data size and convention 0<m<n<9: using recursive thinking from the back forward, the number of apples and monkeys is equal when the apple is the least.  Apple was divided n+1 times, ate n times, and finally after the end of the remaining m. So, get the formula: Total = n^ (n+1)-n*m + M Sample code:
1 ImportJava.util.Scanner;2 3  Public classMain {4      Public Static voidMain (string[] args) {5Scanner sc =NewScanner (system.in);6         intn =sc.nextint ();7         intm =sc.nextint ();8         intApplenum = (int) Math.pow (n, n+1)-n*m + M;//The apples were divided n+1 times, ate n times, and finally left with M9 System.out.println (applenum);Ten     } One}

Blue Bridge cup algorithm training ALGO-121 monkeys to divide apples

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.