Blue Bridge Cup analog multi-factorial

Problem Description
  We know factorial n represents n * (n-1) * (n-2) * ...... * 2 * 1, a similar, multi-factorial can be defined, for example:! 5! ! = 1 * 3 * 5, in turn may have n! ...! (K a '!', Can be simply expressed as n (k)!) = N * (nk) * (n-2k) * .... (until the last number <= 0).
  Now the value of a given set of data n, k, m, when m = 1, and calculates and outputs n (1)! + N ( 2)! + ...... + n (k)! Is, m = and the sum of the numbers 2 calculates and outputs n (1)! + n ( 2)! + ...... + n (k)! respective bits.
Input Format
  Two rows, the first row and the n k, the second line m.
Output Format
  Row of n (1)! + N (2)! + ...... + n (k)! Or the value n (1)! + N (2)! + ...... + n figures and on (k)! of individual bits.
Sample input
5 1
2
Sample Output
3
Scale data and conventions
  0 < k < n <= 20
Problem-solving ideas: bluff subject, that template to a high precision, look data range, the worst case is n = 20, k = 1, a maximum number of 20 + 19 + 18 + ...!!! +1 able to save enough !, with long long, do questions look at the data range later. By including various types of variable values, making reference https://blog.csdn.net/sinat_31275315/article/details/90477866.
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main() {
 4     cout << "int的最大最小值" << endl; 
 5     cout << INT_MAX << endl; 
 6     cout << INT_MIN << endl; 
 7     cout << "long的最大最小值" << endl; 
 8     cout << LONG_MAX << endl; 
 9     cout << LONG_MIN << endl; 
10     cout << "<<"The maximum and minimum long long endl; 
11     cout << LONG_LONG_MAX << endl; 
12     cout << LONG_LONG_MIN << endl; 
13     cout << "unsiged long long的最大最小值" << endl; 
14     cout << ULLONG_MAX << endl; 
15     //unsigned long long 最小值为0 
16     cout << "float的最大最小值" << endl; 
17     cout << FLT_MAX << endl; 
18     cout << FLT_MIN << endl; 
19     cout << "<<. "double the maximum and minimum endl; 
20     cout << DBL_MAX << endl; 
21     cout << DBL_MIN << endl; 
22     return 0;
23 }

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     int n, k, m;
 5     cin >> n >> k >> m;
 6     long long t;
 7     long long ans = 0; //ans存储n(1)!+n(2)!+......+n(k)!的值 
 8     for (int i = 1; i <= k; i++) {  
 9         t = 1; //t存储n(i)!的值 
10         for (int j = n; j > 0; j -= i) {
11             t *= j;
12         }
13         ans += t;
14     }
15     if (m == 1) {
16         cout << ans << endl;
17     } else {
18         int res = 0; //res存储ans这个数各个位上的数字之和 
19         while (ans) {
20             res += ans % 10; //拆出每一位 
21             ans /= 10;
22         }
23         cout << res << endl;
24     }
25     return 0;
26 }

 

 

 

Guess you like

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