Blue Bridge Cup at the end of the simulation factorial

Problem Description
  Given n and len, output n! Len end position.
Input Format
  Line two positive integers n and len.
Output Format
  Line a string that represents the answer. Lack of completion with a leading zero length.
Sample input
6 5
Sample Output
00720
Scale data and conventions
  n<=30, len<=10。
Or foundation is not solid, debugging for a long time, some details not considered good, with the test sample was changing for the better step by step.
Problem-solving ideas: Before you did a similar topic simplified version, forget what to do, and added later.
Since this question is to be output n! After len bit, then in seeking n! Each step in the process of modulo 10 ^ (len + 2). Finally, the factorial result into a string, convenient with leading zero.
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 ll f(int n) { //返回10的n次方 
 5     ll t = 1;
 6     for (int i = 1; i <= n; i++) {
 7         t *= 10;
 8     }
 9     return t;
10 }
11 int main() {
12     int n, len;
13     cin >> n >> len;
14     ll ans = 1;
15     ll mod = f(len + 2);
16     for (int i = 1; i <= n; i++) {
17         ans = (ans % mod * i % mod) % mod;
18     }
19     string s = "";
20     while (ans) {
21         int t = ans % 10;
22         s += t + '0';
23         ans /= 10;
24     }
25     s = s.substr(0, len);
26     reverse(s.begin(), s.end());
27     while (s.length() < len) {
28         s = "0" + s;
29     }
30     cout << s << endl;
31     return 0;
32 }

 

Guess you like

Origin www.cnblogs.com/fx1998/p/12641295.html