Skip to content

Commit

Permalink
Merge pull request #1292 from NachiaVivias/bigint-carrychain
Browse files Browse the repository at this point in the history
[テストケース追加] big_integer/addition - carry_chain
  • Loading branch information
maspypy authored Dec 10, 2024
2 parents be0862b + 3b18867 commit 6c21fd7
Show file tree
Hide file tree
Showing 6 changed files with 377 additions and 0 deletions.
176 changes: 176 additions & 0 deletions big_integer/addition_of_big_integers/gen/carry_chain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

#include "random.h"
#include "../params.h"

int main(int, char* argv[]) {
// seed % 2 : randomize last some digits or not
// seed / 2 : switch patterns
long long seed = atoll(argv[1]);
auto gen = Random(seed);

// decimal
std::string DIGIT_CHARS = "0123456789";
int BASE = 10;

// short case
long long max_dig1 = 1;
while(true){
long long x = max_dig1 + 1;
if(x > LOG_10_A_AND_B_MAX) break;
// split sum(size) into 14 unit,
// around 12 unit required, 2 unit left for randomization
if((x * (x+1) / 2 + x * 10) > SUM_OF_CHARACTER_LENGTH / 14) break;
max_dig1 = x;
}

std::vector<std::string> A, B;

// type 1, 3
if(seed / 2 == 0){

// Case Type 1
// 1 + 999...999
// 4 unit
for(int t=0; t<4; t++){
for(int d=1; d<=max_dig1; d++){

std::string a = std::string(1, DIGIT_CHARS[1]);
std::string b = std::string(d, DIGIT_CHARS[BASE-1]);

if(t & 1) std::swap(a, b);
if(t & 2){
a.insert(a.begin(), '-');
b.insert(b.begin(), '-');
}

A.push_back(std::move(a));
B.push_back(std::move(b));
}
}

// Case Type 3
// 1000...000 - 999...999
// 8 unit
for(int t=0; t<4; t++){
for(int d=1; d<=max_dig1; d++){
std::string a = std::string(d, DIGIT_CHARS[BASE-1]);
std::string b = std::string(1, DIGIT_CHARS[1]) + std::string(d, DIGIT_CHARS[0]);

if(t & 1) std::swap(a, b);
if(t & 2){
a.insert(a.begin(), '-');
} else {
b.insert(b.begin(), '-');
}

A.push_back(std::move(a));
B.push_back(std::move(b));
}
}
}
// type 2, 4, 5
else if(seed / 2 == 1){

// Case Type 2
// 1000...000 - 1
// 4 unit
for(int t=0; t<4; t++){
for(int d=1; d<=max_dig1; d++){
std::string a = std::string(1, DIGIT_CHARS[1]);
std::string b = std::string(1, DIGIT_CHARS[1]) + std::string(d, DIGIT_CHARS[0]);

if(t & 1) std::swap(a, b);
if(t & 2){
a.insert(a.begin(), '-');
} else {
b.insert(b.begin(), '-');
}

A.push_back(std::move(a));
B.push_back(std::move(b));
}
}

// Case Type 4
// A + B = 0
// 4 unit
for(int t=0; t<2; t++){
for(int d=1; d<=max_dig1; d++){
std::string a;
for(int i=0; i<d; i++){
int dig = gen.uniform((i == 0 ? 1 : 0), BASE-1);
a.push_back(DIGIT_CHARS[dig]);
}

std::string b = a;

if(t & 1){
a.insert(a.begin(), '-');
} else {
b.insert(b.begin(), '-');
}

A.push_back(std::move(a));
B.push_back(std::move(b));
}
}

// Case Type 5
// A + B = 100...000*
// 4 unit
for(int t=0; t<2; t++){
for(int d=1; d<=max_dig1; d++){
std::string a;
std::string b;

for(int i=0; i<d; i++){
// if (BASE-1) at the leading digit , b will have a leading zero
int dig = (i == 0) ? gen.uniform(1, BASE-2) : gen.uniform(0, BASE-1);
a.push_back(DIGIT_CHARS[dig]);
b.push_back(DIGIT_CHARS[BASE-1 - dig]);
}

auto u = gen.uniform_pair(1, BASE);
a.push_back(DIGIT_CHARS[BASE - u.first]);
b.push_back(DIGIT_CHARS[u.second - 1]);

if(t & 1){
a.insert(a.begin(), '-');
b.insert(b.begin(), '-');
}

A.push_back(std::move(a));
B.push_back(std::move(b));
}
}

}

int T = (int)A.size();

int available_char_count = SUM_OF_CHARACTER_LENGTH;
for(auto& a : A) available_char_count -= (int)a.size();
for(auto& b : B) available_char_count -= (int)b.size();

// randomize lower digit
if(seed % 2 == 1){
while(available_char_count >= 2){
int c0 = gen.uniform(0, BASE-1);
int c1 = gen.uniform(0, BASE-1);
int t = gen.uniform(0, T-1);
A[t].push_back(DIGIT_CHARS[c0]);
B[t].push_back(DIGIT_CHARS[c1]);
available_char_count -= 2;
}
}

printf("%d\n", T);
for (int i = 0; i < T; i++) {
printf("%s %s\n", A[i].c_str(), B[i].c_str());
}
}
8 changes: 8 additions & 0 deletions big_integer/addition_of_big_integers/hash.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{
"carry_chain_00.in": "db8296be9a0e9bfb430d6fcec92090c931f0a344e155052f3bbd0cab8e2df851",
"carry_chain_00.out": "58bb65d907cc9f02971faabca1638ced57368b55f6db13c50c2094b82b2129ec",
"carry_chain_01.in": "93c7381698df7ab9f2afd2d0fd578572816865498069113815859c3944c13554",
"carry_chain_01.out": "4392cf7263509d85165b0ec8e942db1a935d8928a4d64f299247a0a60c7ab691",
"carry_chain_02.in": "d151314b17a0e8d7b39f5fb07af0f44df9dd6e248c09c318ee1bed0cf110cf24",
"carry_chain_02.out": "0babf9416da1e16485506fd894d9cc97ae9fe6abdd7eac6c7c3f73a1249afab0",
"carry_chain_03.in": "5df70a3e0aa2dcbb5de1c47194633f5c3d62da811e7dc336bdfb8596f6447d50",
"carry_chain_03.out": "2da8453f83f26a83930c4af5beab2d65ff391b614d9f73d282eb29fb249edf5f",
"example_00.in": "d54f7bffdace286d4f41c2b64ed96d207040da5a9a0bba3dc6dac4d56eb8c96b",
"example_00.out": "38fb50d53f338351feb6ecec4849f891afef2ed017631190ac56ff7630765054",
"large_00.in": "e7e19506d116ca65a19bd67455060e011b841ddd488a89c930f0a43e40691354",
Expand Down
3 changes: 3 additions & 0 deletions big_integer/addition_of_big_integers/info.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ forum = "https://github.com/yosupo06/library-checker-problems/issues/941"
[[tests]]
name = "large_small.cpp"
number = 1
[[tests]]
name = "carry_chain.cpp"
number = 4

[params]
T_MAX = 200000
Expand Down
179 changes: 179 additions & 0 deletions big_integer/addition_of_hex_big_integers/gen/carry_chain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

#include "random.h"
#include "../params.h"

int main(int, char* argv[]) {
// NOTICE : The test case descriptions (comments)
// are written in decimal system.

// seed % 2 : randomize last some digits or not
// seed / 2 : switch patterns
long long seed = atoll(argv[1]);
auto gen = Random(seed);

// hexadecimal
std::string DIGIT_CHARS = "0123456789ABCDEF";
int BASE = 16;

// short case
long long max_dig1 = 1;
while(true){
long long x = max_dig1 + 1;
if(x > LOG_16_A_AND_B_MAX) break;
// split sum(size) into 14 unit,
// around 12 unit required, 2 unit left for randomization
if((x * (x+1) / 2 + x * 10) > SUM_OF_CHARACTER_LENGTH / 14) break;
max_dig1 = x;
}

std::vector<std::string> A, B;

// type 1, 3
if(seed / 2 == 0){

// Case Type 1
// 1 + 999...999
// 4 unit
for(int t=0; t<4; t++){
for(int d=1; d<=max_dig1; d++){

std::string a = std::string(1, DIGIT_CHARS[1]);
std::string b = std::string(d, DIGIT_CHARS[BASE-1]);

if(t & 1) std::swap(a, b);
if(t & 2){
a.insert(a.begin(), '-');
b.insert(b.begin(), '-');
}

A.push_back(std::move(a));
B.push_back(std::move(b));
}
}

// Case Type 3
// 1000...000 - 999...999
// 8 unit
for(int t=0; t<4; t++){
for(int d=1; d<=max_dig1; d++){
std::string a = std::string(d, DIGIT_CHARS[BASE-1]);
std::string b = std::string(1, DIGIT_CHARS[1]) + std::string(d, DIGIT_CHARS[0]);

if(t & 1) std::swap(a, b);
if(t & 2){
a.insert(a.begin(), '-');
} else {
b.insert(b.begin(), '-');
}

A.push_back(std::move(a));
B.push_back(std::move(b));
}
}
}
// type 2, 4, 5
else if(seed / 2 == 1){

// Case Type 2
// 1000...000 - 1
// 4 unit
for(int t=0; t<4; t++){
for(int d=1; d<=max_dig1; d++){
std::string a = std::string(1, DIGIT_CHARS[1]);
std::string b = std::string(1, DIGIT_CHARS[1]) + std::string(d, DIGIT_CHARS[0]);

if(t & 1) std::swap(a, b);
if(t & 2){
a.insert(a.begin(), '-');
} else {
b.insert(b.begin(), '-');
}

A.push_back(std::move(a));
B.push_back(std::move(b));
}
}

// Case Type 4
// A + B = 0
// 4 unit
for(int t=0; t<2; t++){
for(int d=1; d<=max_dig1; d++){
std::string a;
for(int i=0; i<d; i++){
int dig = gen.uniform((i == 0 ? 1 : 0), BASE-1);
a.push_back(DIGIT_CHARS[dig]);
}

std::string b = a;

if(t & 1){
a.insert(a.begin(), '-');
} else {
b.insert(b.begin(), '-');
}

A.push_back(std::move(a));
B.push_back(std::move(b));
}
}

// Case Type 5
// A + B = 100...000*
// 4 unit
for(int t=0; t<2; t++){
for(int d=1; d<=max_dig1; d++){
std::string a;
std::string b;

for(int i=0; i<d; i++){
// if (BASE-1) at the leading digit , b will have a leading zero
int dig = (i == 0) ? gen.uniform(1, BASE-2) : gen.uniform(0, BASE-1);
a.push_back(DIGIT_CHARS[dig]);
b.push_back(DIGIT_CHARS[BASE-1 - dig]);
}

auto u = gen.uniform_pair(1, BASE);
a.push_back(DIGIT_CHARS[BASE - u.first]);
b.push_back(DIGIT_CHARS[u.second - 1]);

if(t & 1){
a.insert(a.begin(), '-');
b.insert(b.begin(), '-');
}

A.push_back(std::move(a));
B.push_back(std::move(b));
}
}

}

int T = (int)A.size();

int available_char_count = SUM_OF_CHARACTER_LENGTH;
for(auto& a : A) available_char_count -= (int)a.size();
for(auto& b : B) available_char_count -= (int)b.size();

// randomize lower digit
if(seed % 2 == 1){
while(available_char_count >= 2){
int c0 = gen.uniform(0, BASE-1);
int c1 = gen.uniform(0, BASE-1);
int t = gen.uniform(0, T-1);
A[t].push_back(DIGIT_CHARS[c0]);
B[t].push_back(DIGIT_CHARS[c1]);
available_char_count -= 2;
}
}

printf("%d\n", T);
for (int i = 0; i < T; i++) {
printf("%s %s\n", A[i].c_str(), B[i].c_str());
}
}
8 changes: 8 additions & 0 deletions big_integer/addition_of_hex_big_integers/hash.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{
"carry_chain_00.in": "bc0e975898aa191e0d871b24a6deb2c206a45e84a3fb87af57bf26d02abe6a00",
"carry_chain_00.out": "902cc5362d4ef4f0c4f299845e26f64d0aee5801bd76fb659824dde07b8e3a17",
"carry_chain_01.in": "41b8e310334f54af87493fdff8bd6009599449a4df0b6a246cba6aef8fec178a",
"carry_chain_01.out": "94dfee512fe1d02d7e1ccc49acb03ed96ef3670e638c50254b61eac381c256cc",
"carry_chain_02.in": "490e4a38259d73ee1addc8ea2b0a64bcba65be8ed38b56c8f105ec49f8bc3cd6",
"carry_chain_02.out": "f139b324fb4a45bf41fc20e1dbfe6db97ce8e84cb9aa09ca4332a87e5cfc8c80",
"carry_chain_03.in": "6fe4c858252c51e6b5d3546698a4ee7766503481164e86f2d34885b8d2d6d7ab",
"carry_chain_03.out": "2fb84e651c7a6e0f0b93281f3099480660ba3560fed8891b3be8f02539a69c09",
"example_00.in": "c100065382d9827eef54b9b75b044448ad265805e1f18c0fb368cb8a9eab879f",
"example_00.out": "f4192e9c29013224ec885d4ad517ff3e2c9c0e56e2b59294281512bb1c1efaaf",
"large_00.in": "ca55e283862d764de8ea45838995bfda6174f2afe718ad7a6f0ecdf8c60a00c0",
Expand Down
Loading

0 comments on commit 6c21fd7

Please sign in to comment.