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