Add solution for 2016 Day 15
This commit is contained in:
1 parent
988c5552d0
commit
5111d633b7
3 files changed
+64
-16
No files matched your search
+26
-8
@@ -600,12 +600,15 @@ struct Helper
|
||||
return Index2D{-1, -1};
|
||||
}
|
||||
|
||||
static int64_t ChineseRemainderTheoremTwo(int64_t mod1, int64_t mod2, int64_t res1, int64_t res2)
|
||||
// Solve for N:
|
||||
// remainder1 = N % mod1
|
||||
// remainder2 = N % mod2
|
||||
static int64_t ChineseRemainderTheoremTwo(int64_t mod1, int64_t mod2, int64_t remainder1, int64_t remainder2)
|
||||
{
|
||||
int64_t i = res1;
|
||||
int64_t i = remainder1;
|
||||
while (true)
|
||||
{
|
||||
if (i % mod2 == res2)
|
||||
if (i % mod2 == remainder2)
|
||||
return i;
|
||||
|
||||
i += mod1;
|
||||
@@ -613,17 +616,32 @@ struct Helper
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int64_t ChineseRemainderTheorem(const std::vector<int64_t>& mods, const std::vector<int64_t>& results)
|
||||
// Solve for N:
|
||||
// remainders = N % mods
|
||||
static int64_t ChineseRemainderTheorem(const std::vector<int64_t>& mods, const std::vector<int64_t>& remainders)
|
||||
{
|
||||
int64_t currentMod = mods.front();
|
||||
int64_t currentRes = results.front();
|
||||
int64_t currentRem = remainders.front();
|
||||
for (int i = 1; i < mods.size(); i++)
|
||||
{
|
||||
int64_t result = ChineseRemainderTheoremTwo(currentMod, mods[i], currentRes, results[i]);
|
||||
currentRes = result;
|
||||
int64_t result = ChineseRemainderTheoremTwo(currentMod, mods[i], currentRem, remainders[i]);
|
||||
currentRem = result;
|
||||
currentMod = currentMod * mods[i];
|
||||
}
|
||||
return currentRes;
|
||||
return currentRem;
|
||||
}
|
||||
|
||||
// Solve for N:
|
||||
// remainders = (start + N) % mods
|
||||
static int64_t ChineseRemainderTheorem(const std::vector<int64_t>& mods,
|
||||
std::vector<int64_t> remainders,
|
||||
const std::vector<int64_t>& starts)
|
||||
{
|
||||
for (int i = 0; i < mods.size(); i++)
|
||||
{
|
||||
remainders[i] = ((remainders[i] - starts[i]) % mods[i] + mods[i]) % mods[i];
|
||||
}
|
||||
return ChineseRemainderTheorem(mods, remainders);
|
||||
}
|
||||
|
||||
static uint64_t FastExponentiation(uint64_t start, uint64_t multiplication, uint64_t power, uint64_t mod)
|
||||
|
||||
Reference in new issue
Block a user