fork download
  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main (String[] args) {
  4. Scanner scanner = new Scanner(System.in);
  5. int n, p;
  6. while(scanner.hasNextInt()) {
  7. n = scanner.nextInt();
  8. p = scanner.nextInt();
  9. int k = 0;
  10. for (int i = 1; i <= n; i++) {
  11. int fact = i;
  12. while (fact > 0) {
  13. fact /= p;
  14. k += fact;
  15. }
  16. }
  17. System.out.println(k);
  18. }
  19. }
  20. }
Success #stdin #stdout 0.07s 2184192KB
stdin
1 5
3 2
4 2
4 3
10000 3
stdout
0
2
5
2
24962375