Advertisement
shawon_majid

Untitled

May 9th, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. //Bismillahir Rahman-ir Rahim
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define MAX 32000
  5. #define PI acos(-1)
  6. #define debug(x) cout << '>' << #x << " : " << x << endl;
  7. #define all(c) c.begin(), c.end()
  8. //#define m (pow(10, 9)+7)
  9. typedef unsigned long long ull;
  10. typedef long long ll;
  11.  
  12.  
  13. string strsum(string a, string b){
  14.     int len = max(a.size(), b.size());
  15.  
  16.     reverse(a.begin(), a.end());
  17.     reverse(b.begin(), b.end());
  18.     int ans, carry = 0;
  19.     string sum;
  20.     for(int i = 0; i < len; i++){
  21.         ans = carry;
  22.         if(a.size() > i){
  23.             ans += a[i] - '0';
  24.         }
  25.         if(b.size() > i){
  26.             ans += b[i] - '0';
  27.         }
  28.         if(ans > 9){
  29.             carry = ans / 10;
  30.             ans = ans % 10;
  31.         }
  32.         else{
  33.             carry = 0;
  34.         }
  35.  
  36.         sum += (char)(ans + (int)'0');
  37.     }
  38.     if(carry){
  39.         sum += (char)(carry + (int)'0');
  40.     }
  41.     reverse(sum.begin(), sum.end());
  42.     return sum;
  43. }
  44.  
  45. string str_multiplic(string a, string b)
  46. {
  47.     int maxlen = max(a.size(), b.size()), minlen = min(a.size(), b.size()), temp;
  48.     string ans ="0", str, mainans = "0";
  49.     stringstream ss;
  50.     if(a.size() < b.size()){
  51.         swap(a, b);
  52.     }
  53.     reverse(a.begin(), a.end());
  54.     reverse(b.begin(), b.end());
  55.  
  56.     for(int i = 0; i < minlen; i++){
  57.         ans = "0";
  58.         for(int j = 0; j < maxlen; j++){
  59.  
  60.             temp = (b[i] - '0')*(a[j] - '0');
  61.             //cout <<"temp: " <<temp << endl;
  62.             stringstream ss;
  63.             ss << temp;
  64.  
  65.             ss >> str;
  66.  
  67.             for(int k = 0; k < j; k++){
  68.                 str+="0";
  69.             }
  70.             //cout <<"str: " << str << endl;
  71.  
  72.             ans = strsum(ans, str);
  73.             //cout << "ans: " << ans << endl;
  74.  
  75.         }
  76.         for(int l = 0; l < i; l++){
  77.             ans += "0";
  78.         }
  79.  
  80.         mainans = strsum(mainans, ans);
  81.         //cout <<"mainans: " <<mainans << endl;
  82.  
  83.     }
  84.     return mainans;
  85.  
  86. }
  87.  
  88. vector < string > vec;
  89. void dp_mult()
  90. {
  91.     //str = "0";
  92.     vec.push_back("1");
  93.     string ans = "1";
  94.     int i  = 1, limit = 400;
  95.  
  96.     while(1) {
  97.         string str;
  98.         stringstream ss;
  99.         ss << i;
  100.         ss >> str;
  101.         ans = str_multiplic(ans, str);
  102.         vec.push_back(ans);
  103.         if(i == limit) break;
  104.         i++;
  105.     }
  106. }
  107.  
  108. int main()
  109. {
  110.     freopen("input.txt", "r", stdin);
  111.     freopen("output.txt", "w", stdout);
  112.     dp_mult();
  113.     int n;
  114.    
  115.     while(cin >> n) {
  116.         cout << n << "!" << endl;
  117.         cout << vec[n] << endl;
  118.     }
  119.  
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement