Cod sursa(job #2671192)

Utilizator victorzarzuZarzu Victor victorzarzu Data 11 noiembrie 2020 17:33:21
Problema Prod Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.08 kb
	
#include <bits/stdc++.h>
 
using namespace std;
ifstream f("prod.in");
ofstream g("prod.out");
int cif[10], s;
int n1[2005], n2[2005], rez[2005];
 
void Read()
{
  for(int i = 1;i <= 9;++i)
    f>>cif[i];
}
 
bool compare()
{
  for(int i = 0;i <= n1[0];++i)
    if(n1[i] != n2[i])
      return n1[i] < n2[i];
  return true;
}
 
void numbers()
{
  int c = 9;
  while(true)
  {
    if(!cif[c])
    {
      --c;
      if(!c)
      {
        reverse(n1 + 1, n1 + n1[0] + 1);
        reverse(n2 + 1, n2 + n2[0] + 1);      
        return;
      }
      continue;
    }
    if(compare())
      n1[++n1[0]] = c;
    else
      n2[++n2[0]] = c;
    --cif[c];
  }
  
}
 
void multiply()
{
  int t = 0;
  rez[0] = n1[0] + n2[0] - 1;
  for(int i = 1;i <= n1[0];++i)
    for(int j = 1;j <= n2[0] || t;++j)
      {
        rez[i + j - 1] += n1[i] * n2[j] + t;
        t = rez[i + j - 1] / 10;
        rez[i + j - 1] %= 10;
        if(j > n2[0])
          ++rez[0];
      }
}
 
void Solve()
{
  numbers(); 
  multiply();
  for(int i = rez[0];i >= 1;--i)
    g<<rez[i];
}
 
int main()
{
  Read();
  Solve();
  return 0;
}