-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathFCTRL2.cs
180 lines (145 loc) · 4.77 KB
/
FCTRL2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Collections.Generic;
using System.Linq;
// https://www.spoj.com/problems/FCTRL2/ #big-numbers #factorial
// Returns n! for 1 <= n <= 100 (you can just use System.Numerics for this now).
public static class FCTRL2
{
private const int _limit = 100;
private static readonly IReadOnlyList<BigInteger> _factorials;
static FCTRL2()
{
var factorials = new BigInteger[_limit + 1];
factorials[0] = BigInteger.One;
for (int i = 1; i <= _limit; ++i)
{
factorials[i] = factorials[i - 1] * (new BigInteger(i));
}
_factorials = factorials;
}
public static BigInteger Solve(int n)
=> _factorials[n];
}
public struct BigInteger : IEquatable<BigInteger>
{
private static readonly BigInteger _zero = new BigInteger(0);
private static readonly BigInteger _one = new BigInteger(1);
public static BigInteger Zero => _zero;
public static BigInteger One => _one;
private readonly IReadOnlyList<byte> _digits;
private BigInteger(IReadOnlyList<byte> digits)
{
_digits = digits;
}
public BigInteger(int n)
: this(n.ToString())
{ }
// For a number string, the first character is the most significant digit. For our _digits
// array, it's most convenient if the first element is the least significant digit, so
// reverse the order here.
public BigInteger(string digits)
{
byte[] digitsArray = new byte[digits.Length];
for (int i = 0; i < digits.Length; ++i)
{
digitsArray[i] = byte.Parse(digits[digits.Length - i - 1].ToString());
}
_digits = Array.AsReadOnly(digitsArray);
}
public bool IsZero => this == Zero;
public bool IsOne => this == One;
public static BigInteger operator +(BigInteger a, BigInteger b)
{
// No more than multiplying the larger by 2, so can't require more than one extra digit.
int maxDigitCount = Math.Max(a._digits.Count, b._digits.Count);
var result = new List<byte>(maxDigitCount + 1);
byte carry = 0;
for (int i = 0; i < maxDigitCount; ++i)
{
byte value = carry;
if (i < a._digits.Count)
{
value += a._digits[i];
}
if (i < b._digits.Count)
{
value += b._digits[i];
}
result.Add((byte)(value % 10));
carry = (byte)(value / 10);
}
if (carry != 0)
{
result.Add(carry);
}
return new BigInteger(result.AsReadOnly());
}
public static BigInteger operator *(BigInteger a, BigInteger b)
{
var result = BigInteger.Zero;
for (int i = 0; i < b._digits.Count; ++i)
{
result += a.MultiplyByDigit(b._digits[i]).MultiplyByPowerOfTen(i);
}
return result;
}
private BigInteger MultiplyByDigit(byte digit)
{
if (IsZero || digit == 1) return this;
if (digit == 0) return BigInteger.Zero;
// Digit is less than 10 so the result can't require more than one extra digit.
var result = new List<byte>(_digits.Count + 1);
byte carry = 0;
for (int i = 0; i < _digits.Count; i++)
{
byte value = (byte)(_digits[i] * digit + carry);
result.Add((byte)(value % 10));
carry = (byte)(value / 10);
}
if (carry != 0)
{
result.Add(carry);
}
return new BigInteger(result.AsReadOnly());
}
private BigInteger MultiplyByPowerOfTen(int power)
{
if (IsZero || power == 0) return this;
byte[] result = new byte[_digits.Count + power];
for (int i = 0; i < power; ++i)
{
result[i] = 0;
}
for (int i = 0; i < _digits.Count; ++i)
{
result[power + i] = _digits[i];
}
return new BigInteger(Array.AsReadOnly(result));
}
public override string ToString()
=> string.Concat(_digits.Reverse());
public bool Equals(BigInteger other)
=> _digits.SequenceEqual(other._digits);
public override bool Equals(object obj)
=> obj is BigInteger ? Equals((BigInteger)obj) : false;
public override int GetHashCode()
{
throw new NotImplementedException();
}
public static bool operator ==(BigInteger a, BigInteger b)
=> a.Equals(b);
public static bool operator !=(BigInteger a, BigInteger b)
=> !(a == b);
}
public static class Program
{
private static void Main()
{
int remainingTestCases = int.Parse(Console.ReadLine());
while (remainingTestCases-- > 0)
{
Console.WriteLine(
FCTRL2.Solve(int.Parse(Console.ReadLine())));
}
}
}