杂物
这里面都是一些多而杂的东西...
按需求看看吧。
exc拓展库万能头使用指南
#include<bits/extc++.h>
#include<bits/stdc++.h>
#define debug cerr
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
BM算法 找规律题一定要试试啊
#include<bits/stdc++.h>
using namespace std;
const int N=2005;
const double eps=1e-8;
int cnt,fail[N];
double val[N],delta[N];
vector<double>ans[N];
int main() {
int N;scanf("%d",&N);
for(int i=1;i<=N;i++)scanf("%lf",&val[i]);
for(int i=1;i<=N;i++) {
double tmp=val[i];
for(int j=0;j<ans[cnt].size();j++)
tmp-=ans[cnt][j]*val[i-j-1];
delta[i]=tmp;
if(fabs(tmp)<=eps)continue;
fail[cnt]=i;
if(cnt==0) {
ans[++cnt].resize(i);
continue;
}
double mul=delta[i]/delta[fail[cnt-1]];
cnt++;ans[cnt].resize(i-fail[cnt-2]-1);
ans[cnt].push_back(mul);
for(int j=0;j<ans[cnt-2].size();j++)
ans[cnt].push_back(ans[cnt-2][j]*-mul);
if(ans[cnt].size()<ans[cnt-1].size())ans[cnt].resize(ans[cnt-1].size());
for(int j=0;j<ans[cnt-1].size();j++)
ans[cnt][j]+=ans[cnt-1][j];
}
for(int i=0;i<ans[cnt].size();i++)
cout<<ans[cnt][i]<<' ';
return 0;
}
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
int main() {
return 0;
}
namespace IO {
#define BUFFER_SIZE 100000
char ibuf[BUFFER_SIZE],obuf[BUFFER_SIZE];
char *p1=ibuf,*p2=ibuf,*p3=obuf;
#ifdef ONLINE_JUDGE
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,BUFFER_SIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+BUFFER_SIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#endif
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<=32&&ch!=EOF)
#define isseen(ch) (ch>32)
const long long pow10[] = {
(long long)1e0, (long long)1e1, (long long)1e2, (long long)1e3, (long long)1e4, (long long)1e5,
(long long)1e6, (long long)1e7, (long long)1e8, (long long)1e9, (long long)1e10, (long long)1e11,
(long long)1e12, (long long)1e13, (long long)1e14, (long long)1e15, (long long)1e16, (long long)1e17, (long long)1e18,
};
struct IO_t {
~IO_t(){fwrite(obuf,p3-obuf,1,stdout);}
bool flag=false;
operator bool(){return flag;}
}io;
template<typename T>inline T read() {
T x=0;int f=1;char ch;
while(ch=getchar(),!isdigit(ch)&&(ch!=EOF))if(ch=='-')f=-1;
if(ch==EOF)return 0;
while(isdigit(ch))x=x*10+ch-48,ch=getchar();
if(ch=='.') {
long long flt=0;int cnt=0;
while(ch=getchar(),isdigit(ch))if(cnt<18)flt=flt*10+ch-48,cnt++;
x+=(double)flt/pow10[cnt];
}
return x*=f;
}
template<typename T>inline bool read(T&x) {
x=0;int f=1;char ch;
while(ch=getchar(), !isdigit(ch)&&(ch!=EOF))if(ch=='-')f=-1;
if(ch==EOF)return false;
while(isdigit(ch))x=x*10+ch-48,ch=getchar();
if(ch=='.') {
long long flt=0;int cnt=0;
while(ch=getchar(),isdigit(ch))if(cnt<18)flt=flt*10+ch-48,cnt++;
x+=(double)flt/pow10[cnt];
}
return x*=f,true;
}
inline bool read(char&x) {
while(x=getchar(),isspace(x));
return x!=EOF;
}
inline bool read(char*x) {
char ch;
while(ch=getchar(),isspace(ch));
if(ch==EOF)return false;
while(isseen(ch))*x++=ch,ch=getchar();
*x='\000';
return true;
}
template<typename T> void print(T x) {
static int t[20];int top=0;
if(x<0)putchar('-'),x=-x;
do{t[++top]=x%10;x/=10;}while(x);
while(top)putchar(t[top--]+48);
}
struct empty_type{};
int pcs=8;
empty_type setpcs(int cnt){return pcs=cnt,empty_type();}
inline void print(empty_type x){}
inline void print(double x) {
if(x<0)putchar('-'),x=-x;
x+=5.0/pow10[pcs+1];
print((long long)(x));
x-=(long long)(x);
if(pcs!=0)putchar('.');
for(int i=1;i<=pcs;i++)
x*=10,putchar((int)x+'0'),x-=(int)x;
}
inline void print(float x) {
if(x<0)putchar('-'),x=-x;
x+=5.0/pow10[pcs+1];
print((long long)(x));
x-=(long long)(x);
if(pcs!=0)putchar('.');
for(int i=1;i<=pcs;i++)
x*=10,putchar((int)x+'0'),x-=(int)x;
}
inline void print(char x){putchar(x);}
inline void print(char*x){for(int i=0;x[i];i++)putchar(x[i]);}
inline void print(const char*x){for(int i=0;x[i];i++)putchar(x[i]);}
#ifdef _GLIBCXX_STRING
inline bool read(std::string&x) {
x="";char ch;
while(ch=getchar(),isspace(ch));
if(ch==EOF)return false;
while(!isspace(ch))x+=ch,ch=getchar();
return true;
}
inline void print(std::string x){for(std::string::iterator i=x.begin();i!=x.end();i++)putchar(*i);}
inline bool getline(IO_t&io,std::string x) {
x="";
char ch=getchar();
if(ch==EOF) return false;
while(ch!='\n'&&ch!=EOF)x+=ch,ch=getchar();
return true;
}
#endif
#if __cplusplus >= 201103L
template<typename T,typename... Args>
inline int read(T& a,Args&...other){return read(a)+read(other...);}
template<typename T, typename... Args>
inline void print(T a,Args...other){print(a);print(other...);}
#endif
template<typename T>
IO_t& operator>>(IO_t&io,T&b){return io.flag=read(b),io;}
IO_t& operator >>(IO_t &io,char*b){return io.flag=read(b),io;}
template<typename T>
IO_t& operator<<(IO_t &io,T b){return print(b),io;}
#define cout io
#define cin io
#define endl '\n'
}using namespace IO;
PDQ sort
#ifndef PDQSORT_H
#define PDQSORT_H
#include <algorithm>
#include <cstddef>
#include <functional>
#include <utility>
#include <iterator>
#if __cplusplus >= 201103L
#include <cstdint>
#include <type_traits>
#define PDQSORT_PREFER_MOVE(x) std::move(x)
#else
#define PDQSORT_PREFER_MOVE(x) (x)
#endif
namespace pdqsort_detail {
enum {
// Partitions below this size are sorted using insertion sort.
insertion_sort_threshold = 24,
// Partitions above this size use Tukey's ninther to select the pivot.
ninther_threshold = 128,
// When we detect an already sorted partition, attempt an insertion sort that allows this
// amount of element moves before giving up.
partial_insertion_sort_limit = 8,
// Must be multiple of 8 due to loop unrolling, and < 256 to fit in unsigned char.
block_size = 64,
// Cacheline size, assumes power of two.
cacheline_size = 64
};
#if __cplusplus >= 201103L
template<class T> struct is_default_compare : std::false_type { };
template<class T> struct is_default_compare<std::less<T>> : std::true_type { };
template<class T> struct is_default_compare<std::greater<T>> : std::true_type { };
#endif
// Returns floor(log2(n)), assumes n > 0.
template<class T>
inline int log2(T n) {
short log = 0;
while (n >>= 1) ++log;
return log;
}
// Sorts [begin, end) using insertion sort with the given comparison function.
template<class Iter, class Compare>
inline void insertion_sort(Iter begin, Iter end, Compare comp) {
typedef typename std::iterator_traits<Iter>::value_type T;
if (begin == end) return;
for (Iter cur = begin + 1; cur != end; ++cur) {
Iter sift = cur;
Iter sift_1 = cur - 1;
// Compare first so we can avoid 2 moves for an element already positioned correctly.
if (comp(*sift, *sift_1)) {
T tmp = PDQSORT_PREFER_MOVE(*sift);
do { *sift-- = PDQSORT_PREFER_MOVE(*sift_1); } while (sift != begin && comp(tmp, *--sift_1));
*sift = PDQSORT_PREFER_MOVE(tmp);
}
}
}
// Sorts [begin, end) using insertion sort with the given comparison function. Assumes
// *(begin - 1) is an element smaller than or equal to any element in [begin, end).
template<class Iter, class Compare>
inline void unguarded_insertion_sort(Iter begin, Iter end, Compare comp) {
typedef typename std::iterator_traits<Iter>::value_type T;
if (begin == end) return;
for (Iter cur = begin + 1; cur != end; ++cur) {
Iter sift = cur;
Iter sift_1 = cur - 1;
// Compare first so we can avoid 2 moves for an element already positioned correctly.
if (comp(*sift, *sift_1)) {
T tmp = PDQSORT_PREFER_MOVE(*sift);
do { *sift-- = PDQSORT_PREFER_MOVE(*sift_1); } while (comp(tmp, *--sift_1));
*sift = PDQSORT_PREFER_MOVE(tmp);
}
}
}
// Attempts to use insertion sort on [begin, end). Will return false if more than
// partial_insertion_sort_limit elements were moved, and abort sorting. Otherwise it will
// successfully sort and return true.
template<class Iter, class Compare>
inline bool partial_insertion_sort(Iter begin, Iter end, Compare comp) {
typedef typename std::iterator_traits<Iter>::value_type T;
if (begin == end) return true;
std::size_t limit = 0;
for (Iter cur = begin + 1; cur != end; ++cur) {
Iter sift = cur;
Iter sift_1 = cur - 1;
// Compare first so we can avoid 2 moves for an element already positioned correctly.
if (comp(*sift, *sift_1)) {
T tmp = PDQSORT_PREFER_MOVE(*sift);
do { *sift-- = PDQSORT_PREFER_MOVE(*sift_1); } while (sift != begin && comp(tmp, *--sift_1));
*sift = PDQSORT_PREFER_MOVE(tmp);
limit += cur - sift;
}
if (limit > partial_insertion_sort_limit) return false;
}
return true;
}
template<class Iter, class Compare>
inline void sort2(Iter a, Iter b, Compare comp) {
if (comp(*b, *a)) std::iter_swap(a, b);
}
// Sorts the elements *a, *b and *c using comparison function comp.
template<class Iter, class Compare>
inline void sort3(Iter a, Iter b, Iter c, Compare comp) {
sort2(a, b, comp);
sort2(b, c, comp);
sort2(a, b, comp);
}
template<class T>
inline T* align_cacheline(T* p) {
#if defined(UINTPTR_MAX) && __cplusplus >= 201103L
std::uintptr_t ip = reinterpret_cast<std::uintptr_t>(p);
#else
std::size_t ip = reinterpret_cast<std::size_t>(p);
#endif
ip = (ip + cacheline_size - 1) & -cacheline_size;
return reinterpret_cast<T*>(ip);
}
template<class Iter>
inline void swap_offsets(Iter first, Iter last,
unsigned char* offsets_l, unsigned char* offsets_r,
size_t num, bool use_swaps) {
typedef typename std::iterator_traits<Iter>::value_type T;
if (use_swaps) {
// This case is needed for the descending distribution, where we need
// to have proper swapping for pdqsort to remain O(n).
for (size_t i = 0; i < num; ++i) {
std::iter_swap(first + offsets_l[i], last - offsets_r[i]);
}
}
else if (num > 0) {
Iter l = first + offsets_l[0]; Iter r = last - offsets_r[0];
T tmp(PDQSORT_PREFER_MOVE(*l)); *l = PDQSORT_PREFER_MOVE(*r);
for (size_t i = 1; i < num; ++i) {
l = first + offsets_l[i]; *r = PDQSORT_PREFER_MOVE(*l);
r = last - offsets_r[i]; *l = PDQSORT_PREFER_MOVE(*r);
}
*r = PDQSORT_PREFER_MOVE(tmp);
}
}
// Partitions [begin, end) around pivot *begin using comparison function comp. Elements equal
// to the pivot are put in the right-hand partition. Returns the position of the pivot after
// partitioning and whether the passed sequence already was correctly partitioned. Assumes the
// pivot is a median of at least 3 elements and that [begin, end) is at least
// insertion_sort_threshold long. Uses branchless partitioning.
template<class Iter, class Compare>
inline std::pair<Iter, bool> partition_right_branchless(Iter begin, Iter end, Compare comp) {
typedef typename std::iterator_traits<Iter>::value_type T;
// Move pivot into local for speed.
T pivot(PDQSORT_PREFER_MOVE(*begin));
Iter first = begin;
Iter last = end;
// Find the first element greater than or equal than the pivot (the median of 3 guarantees
// this exists).
while (comp(*++first, pivot));
// Find the first element strictly smaller than the pivot. We have to guard this search if
// there was no element before *first.
if (first - 1 == begin) while (first < last && !comp(*--last, pivot));
else while (!comp(*--last, pivot));
// If the first pair of elements that should be swapped to partition are the same element,
// the passed in sequence already was correctly partitioned.
bool already_partitioned = first >= last;
if (!already_partitioned) {
std::iter_swap(first, last);
++first;
// The following branchless partitioning is derived from "BlockQuicksort: How Branch
// Mispredictions don’t affect Quicksort" by Stefan Edelkamp and Armin Weiss, but
// heavily micro-optimized.
unsigned char offsets_l_storage[block_size + cacheline_size];
unsigned char offsets_r_storage[block_size + cacheline_size];
unsigned char* offsets_l = align_cacheline(offsets_l_storage);
unsigned char* offsets_r = align_cacheline(offsets_r_storage);
Iter offsets_l_base = first;
Iter offsets_r_base = last;
size_t num_l, num_r, start_l, start_r;
num_l = num_r = start_l = start_r = 0;
while (first < last) {
// Fill up offset blocks with elements that are on the wrong side.
// First we determine how much elements are considered for each offset block.
size_t num_unknown = last - first;
size_t left_split = num_l == 0 ? (num_r == 0 ? num_unknown / 2 : num_unknown) : 0;
size_t right_split = num_r == 0 ? (num_unknown - left_split) : 0;
// Fill the offset blocks.
if (left_split >= block_size) {
for (size_t i = 0; i < block_size;) {
offsets_l[num_l] = i++; num_l += !comp(*first, pivot); ++first;
offsets_l[num_l] = i++; num_l += !comp(*first, pivot); ++first;
offsets_l[num_l] = i++; num_l += !comp(*first, pivot); ++first;
offsets_l[num_l] = i++; num_l += !comp(*first, pivot); ++first;
offsets_l[num_l] = i++; num_l += !comp(*first, pivot); ++first;
offsets_l[num_l] = i++; num_l += !comp(*first, pivot); ++first;
offsets_l[num_l] = i++; num_l += !comp(*first, pivot); ++first;
offsets_l[num_l] = i++; num_l += !comp(*first, pivot); ++first;
}
}
else {
for (size_t i = 0; i < left_split;) {
offsets_l[num_l] = i++; num_l += !comp(*first, pivot); ++first;
}
}
if (right_split >= block_size) {
for (size_t i = 0; i < block_size;) {
offsets_r[num_r] = ++i; num_r += comp(*--last, pivot);
offsets_r[num_r] = ++i; num_r += comp(*--last, pivot);
offsets_r[num_r] = ++i; num_r += comp(*--last, pivot);
offsets_r[num_r] = ++i; num_r += comp(*--last, pivot);
offsets_r[num_r] = ++i; num_r += comp(*--last, pivot);
offsets_r[num_r] = ++i; num_r += comp(*--last, pivot);
offsets_r[num_r] = ++i; num_r += comp(*--last, pivot);
offsets_r[num_r] = ++i; num_r += comp(*--last, pivot);
}
}
else {
for (size_t i = 0; i < right_split;) {
offsets_r[num_r] = ++i; num_r += comp(*--last, pivot);
}
}
// Swap elements and update block sizes and first/last boundaries.
size_t num = std::min(num_l, num_r);
swap_offsets(offsets_l_base, offsets_r_base,
offsets_l + start_l, offsets_r + start_r,
num, num_l == num_r);
num_l -= num; num_r -= num;
start_l += num; start_r += num;
if (num_l == 0) {
start_l = 0;
offsets_l_base = first;
}
if (num_r == 0) {
start_r = 0;
offsets_r_base = last;
}
}
// We have now fully identified [first, last)'s proper position. Swap the last elements.
if (num_l) {
offsets_l += start_l;
while (num_l--) std::iter_swap(offsets_l_base + offsets_l[num_l], --last);
first = last;
}
if (num_r) {
offsets_r += start_r;
while (num_r--) std::iter_swap(offsets_r_base - offsets_r[num_r], first), ++first;
last = first;
}
}
// Put the pivot in the right place.
Iter pivot_pos = first - 1;
*begin = PDQSORT_PREFER_MOVE(*pivot_pos);
*pivot_pos = PDQSORT_PREFER_MOVE(pivot);
return std::make_pair(pivot_pos, already_partitioned);
}
// Partitions [begin, end) around pivot *begin using comparison function comp. Elements equal
// to the pivot are put in the right-hand partition. Returns the position of the pivot after
// partitioning and whether the passed sequence already was correctly partitioned. Assumes the
// pivot is a median of at least 3 elements and that [begin, end) is at least
// insertion_sort_threshold long.
template<class Iter, class Compare>
inline std::pair<Iter, bool> partition_right(Iter begin, Iter end, Compare comp) {
typedef typename std::iterator_traits<Iter>::value_type T;
// Move pivot into local for speed.
T pivot(PDQSORT_PREFER_MOVE(*begin));
Iter first = begin;
Iter last = end;
// Find the first element greater than or equal than the pivot (the median of 3 guarantees
// this exists).
while (comp(*++first, pivot));
// Find the first element strictly smaller than the pivot. We have to guard this search if
// there was no element before *first.
if (first - 1 == begin) while (first < last && !comp(*--last, pivot));
else while (!comp(*--last, pivot));
// If the first pair of elements that should be swapped to partition are the same element,
// the passed in sequence already was correctly partitioned.
bool already_partitioned = first >= last;
// Keep swapping pairs of elements that are on the wrong side of the pivot. Previously
// swapped pairs guard the searches, which is why the first iteration is special-cased
// above.
while (first < last) {
std::iter_swap(first, last);
while (comp(*++first, pivot));
while (!comp(*--last, pivot));
}
// Put the pivot in the right place.
Iter pivot_pos = first - 1;
*begin = PDQSORT_PREFER_MOVE(*pivot_pos);
*pivot_pos = PDQSORT_PREFER_MOVE(pivot);
return std::make_pair(pivot_pos, already_partitioned);
}
// Similar function to the one above, except elements equal to the pivot are put to the left of
// the pivot and it doesn't check or return if the passed sequence already was partitioned.
// Since this is rarely used (the many equal case), and in that case pdqsort already has O(n)
// performance, no block quicksort is applied here for simplicity.
template<class Iter, class Compare>
inline Iter partition_left(Iter begin, Iter end, Compare comp) {
typedef typename std::iterator_traits<Iter>::value_type T;
T pivot(PDQSORT_PREFER_MOVE(*begin));
Iter first = begin;
Iter last = end;
while (comp(pivot, *--last));
if (last + 1 == end) while (first < last && !comp(pivot, *++first));
else while (!comp(pivot, *++first));
while (first < last) {
std::iter_swap(first, last);
while (comp(pivot, *--last));
while (!comp(pivot, *++first));
}
Iter pivot_pos = last;
*begin = PDQSORT_PREFER_MOVE(*pivot_pos);
*pivot_pos = PDQSORT_PREFER_MOVE(pivot);
return pivot_pos;
}
template<class Iter, class Compare, bool Branchless>
inline void pdqsort_loop(Iter begin, Iter end, Compare comp, int bad_allowed, bool leftmost = true) {
typedef typename std::iterator_traits<Iter>::difference_type diff_t;
// Use a while loop for tail recursion elimination.
while (true) {
diff_t size = end - begin;
// Insertion sort is faster for small arrays.
if (size < insertion_sort_threshold) {
if (leftmost) insertion_sort(begin, end, comp);
else unguarded_insertion_sort(begin, end, comp);
return;
}
// Choose pivot as median of 3 or pseudomedian of 9.
diff_t s2 = size / 2;
if (size > ninther_threshold) {
sort3(begin, begin + s2, end - 1, comp);
sort3(begin + 1, begin + (s2 - 1), end - 2, comp);
sort3(begin + 2, begin + (s2 + 1), end - 3, comp);
sort3(begin + (s2 - 1), begin + s2, begin + (s2 + 1), comp);
std::iter_swap(begin, begin + s2);
}
else sort3(begin + s2, begin, end - 1, comp);
// If *(begin - 1) is the end of the right partition of a previous partition operation
// there is no element in [begin, end) that is smaller than *(begin - 1). Then if our
// pivot compares equal to *(begin - 1) we change strategy, putting equal elements in
// the left partition, greater elements in the right partition. We do not have to
// recurse on the left partition, since it's sorted (all equal).
if (!leftmost && !comp(*(begin - 1), *begin)) {
begin = partition_left(begin, end, comp) + 1;
continue;
}
// Partition and get results.
std::pair<Iter, bool> part_result =
Branchless ? partition_right_branchless(begin, end, comp)
: partition_right(begin, end, comp);
Iter pivot_pos = part_result.first;
bool already_partitioned = part_result.second;
// Check for a highly unbalanced partition.
diff_t l_size = pivot_pos - begin;
diff_t r_size = end - (pivot_pos + 1);
bool highly_unbalanced = l_size < size / 8 || r_size < size / 8;
// If we got a highly unbalanced partition we shuffle elements to break many patterns.
if (highly_unbalanced) {
// If we had too many bad partitions, switch to heapsort to guarantee O(n log n).
if (--bad_allowed == 0) {
std::make_heap(begin, end, comp);
std::sort_heap(begin, end, comp);
return;
}
if (l_size >= insertion_sort_threshold) {
std::iter_swap(begin, begin + l_size / 4);
std::iter_swap(pivot_pos - 1, pivot_pos - l_size / 4);
if (l_size > ninther_threshold) {
std::iter_swap(begin + 1, begin + (l_size / 4 + 1));
std::iter_swap(begin + 2, begin + (l_size / 4 + 2));
std::iter_swap(pivot_pos - 2, pivot_pos - (l_size / 4 + 1));
std::iter_swap(pivot_pos - 3, pivot_pos - (l_size / 4 + 2));
}
}
if (r_size >= insertion_sort_threshold) {
std::iter_swap(pivot_pos + 1, pivot_pos + (1 + r_size / 4));
std::iter_swap(end - 1, end - r_size / 4);
if (r_size > ninther_threshold) {
std::iter_swap(pivot_pos + 2, pivot_pos + (2 + r_size / 4));
std::iter_swap(pivot_pos + 3, pivot_pos + (3 + r_size / 4));
std::iter_swap(end - 2, end - (1 + r_size / 4));
std::iter_swap(end - 3, end - (2 + r_size / 4));
}
}
}
else {
// If we were decently balanced and we tried to sort an already partitioned
// sequence try to use insertion sort.
if (already_partitioned && partial_insertion_sort(begin, pivot_pos, comp)
&& partial_insertion_sort(pivot_pos + 1, end, comp)) return;
}
// Sort the left partition first using recursion and do tail recursion elimination for
// the right-hand partition.
pdqsort_loop<Iter, Compare, Branchless>(begin, pivot_pos, comp, bad_allowed, leftmost);
begin = pivot_pos + 1;
leftmost = false;
}
}
}
template<class Iter, class Compare>
inline void pdqsort(Iter begin, Iter end, Compare comp) {
if (begin == end) return;
#if __cplusplus >= 201103L
pdqsort_detail::pdqsort_loop<Iter, Compare,
pdqsort_detail::is_default_compare<typename std::decay<Compare>::type>::value &&
std::is_arithmetic<typename std::iterator_traits<Iter>::value_type>::value>(
begin, end, comp, pdqsort_detail::log2(end - begin));
#else
pdqsort_detail::pdqsort_loop<Iter, Compare, false>(
begin, end, comp, pdqsort_detail::log2(end - begin));
#endif
}
template<class Iter>
inline void pdqsort(Iter begin, Iter end) {
typedef typename std::iterator_traits<Iter>::value_type T;
pdqsort(begin, end, std::less<T>());
}
template<class Iter, class Compare>
inline void pdqsort_branchless(Iter begin, Iter end, Compare comp) {
if (begin == end) return;
pdqsort_detail::pdqsort_loop<Iter, Compare, true>(
begin, end, comp, pdqsort_detail::log2(end - begin));
}
template<class Iter>
inline void pdqsort_branchless(Iter begin, Iter end) {
typedef typename std::iterator_traits<Iter>::value_type T;
pdqsort_branchless(begin, end, std::less<T>());
}
#undef PDQSORT_PREFER_MOVE
#endif
/**
* BigN Beata v1.3.1
* By: Nathaniel
* 13th,Dec,2017
**/
//This file provides four operation for big-intgers
//You can use class 'BigN' to define a variable.
//Follow operators are available
//+ - * / % = += -= *= /= %= > < >= <= == !=
//these operators can used between BigN and BigN or BigN and int.
//You can use function print() to print a BigN to stdout.
//printn() will print a '\n' at the end.
//Function getstr() and str() will return a std::string-type string.
//IN SAFE MODE, you can access k-th digit, from 0-th high digits, by [].
//Or you can also modify x-th digit by modify(x,y).
//In this template, multipy operator is implicated by fast_Fourier_transform,
//Which can calculate convolution of to n-vectors in O(nlogn) time.
//When number is small, this code still use O(n^2) algorithm.
//and divide operator is implicated by dichotomy.
//-O2 or -O3 optimize option is recomended.
//**********************************************************************//
//If you delete the follow definition,the calc speed will improve
//but when number become huge, especially over 2000000 digits,
//it may NOT work correctly.
//So, to make sure that you answer is correct, do NOT delete this line.
//#define SAFE_CALC_MODE
//**********************************************************************//
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
#include <complex>
const double _pi=acos(-1);
class _fast_Fourier_transform
{
typedef std::complex<double> Cmplx;
typedef std::vector<Cmplx> E;
typedef std::vector<int> Vec;
private:
int _n,_m,_N;
Vec R;
//butterfly operation
inline void Expand(int temp)
{
int L=0; for(_N=1;_N<=temp;_N<<=1,L++);
R.resize(_N+1);
for(int i=0;i<_N;++i) R[i]=R[i>>1]>>1|(i&1)<<(L-1);
return ;
}
//Fast Fourier Transform
void FFT(E& A,int f)
{
for(int i=0;i<_N;++i) if(i<R[i]) swap(A[i],A[R[i]]);
for(int i=1;i<_N;i<<=1)
{
Cmplx wn(cos(_pi/i),f*sin(_pi/i));
for(int j=0;j<_N;j+=i<<1)
{
Cmplx w(1,0);
for(int k=0;k<i;++k,w*=wn)
{
Cmplx x=A[j+k],y=w*A[i+j+k];
A[j+k]=x+y; A[i+j+k]=x-y;
}
}
} return ;
}
inline Vec Mul(E& A,E& B)
{
Expand(_n+_m); A.resize(_N+1); B.resize(_N+1);
FFT(A,1); FFT(B,1);//DFT
for(int i=0;i<=_N;++i) A[i]*=B[i];//MUL
FFT(A,-1);//IDFT
std::vector<int> vec;
for(int i=0;i<_n+_m-1;++i)
vec.push_back((int)(A[i].real()/_N+0.5));
//fix deviation
return vec;
}
public:
inline Vec multi(Vec& AA,Vec& BB)
{
E A,B;
_n=AA.size(); _m=BB.size();
//turn two vectors to complex
A.resize(_n); B.resize(_m);
for(int i=0;i<_n;++i) A[i]=AA[i];
for(int i=0;i<_m;++i) B[i]=BB[i];
return Mul(A,B);
}
};
class _normal_mul
{
//O(n^2) multiply
typedef std::vector<int> Vec;
private:
inline Vec mul(Vec& A,Vec& B)
{
Vec vec; int _n,_m;
_n=(int)A.size(),_m=(int)B.size();
vec.resize(_n+_m);
for(int i=0;i<_n;++i) for(int j=0;j<_m;++j)
vec[i+j]+=A[i]*B[j];
return vec;
}
public:
inline Vec multi(Vec& AA,Vec& BB) { return mul(AA,BB); }
};
#ifndef SAFE_CALC_MODE
#define DIGITS 1000
#else
#define DIGITS 10
#endif
class Divide_By_Zero_Error { };
class _data_container_of_BigN
{
private:
bool sym;//0 positive,1 negetive
unsigned int length;//total length of the number
std::vector<int> vec;//vector of data
public:
typedef _data_container_of_BigN _this_class;
inline void _set_zero()
{ sym=0; length=1; vec.clear(); vec.push_back(0); }
_data_container_of_BigN() { _set_zero();}
_data_container_of_BigN(int x)
{
//turn int to BigN
_set_zero();
if(x) vec.pop_back();
if(x<0) sym=true,x=-x;
while(x)
{
vec.push_back(x%DIGITS);
x/=DIGITS;
}
update_length();
}
_data_container_of_BigN(const char* str)
{
int _n=strlen(str),data=0,base=1,t=0;
vec.clear(); if(str[0]=='-') sym=true,t=1;
for(int i=_n-1; i>=t; --i)
{
data=data+(str[i]-48)*base;
#ifndef SAFE_CALC_MODE
if((_n-i)%3==0)
#endif
{
vec.push_back(data),data=0,base=1;
continue;
}
base*=10;
}
vec.push_back(data);
update_length();
}
inline _this_class operator=(const char* str)
{
//turn char* to BigN
int _n=strlen(str),data=0,base=1,t=0;
vec.clear(); if(str[0]=='-') sym=true,t=1;
for(int i=_n-1; i>=t; --i)
{
data=data+(str[i]-48)*base;
#ifndef SAFE_CALC_MODE
if((_n-i)%3==0)
#endif
{
vec.push_back(data),data=0,base=1;
continue;
}
base*=10;
}
vec.push_back(data);
update_length();
return *this;
}
inline _this_class operator =(int x)
{
//turn int to BigN
_set_zero();
if(x) vec.pop_back();
if(x<0) sym=true,x=-x;
while(x)
{
vec.push_back(x%DIGITS);
x/=DIGITS;
}
update_length();
return *this;
}
inline _this_class operator -()
{
_this_class C=*this;
C.sym=!C.sym;
return C;
}
inline int& operator[](int x) { return vec[x]; }
inline bool is_positive() { return !sym; }
inline bool is_negetive() { return sym; }
//Fix -0 to 0
inline void fix_zero()
{ if(sym && length==1 && vec[0]==0) sym=!sym; }
//get k-th digit
inline int getkth(const int x)
{
if(x>=(int)length) return -1;
return vec[length-x-1];
}
//Modify
inline bool modify(const int x,const int y)
{
if(x>=(int)length) return false;
vec[length-x-1]=y; update_length(); return true;
}
inline _this_class operator+(_this_class& B)
{
if(!sym && !B.sym) return positive_add(B);
if(sym && B.sym) return -positive_add(B);
if(sym && !B.sym)
{
if(compare_abs(B)>0) return -positive_minus(B);
return B.positive_minus(*this);
}
if(compare_abs(B)>0) return positive_minus(B);
return -B.positive_minus(*this);
}
inline _this_class operator-(_this_class& B)
{
if(!sym && !B.sym)
{
if(compare_abs(B)>0) return positive_minus(B);
return -B.positive_minus(*this);
}
if(sym && B.sym)
{
if(compare_abs(B)>0) return -positive_minus(B);
return B.positive_minus(*this);
}
if(!sym && B.sym) return positive_add(B);
return -B.positive_add(*this);
}
inline _this_class operator*(_this_class& B)
{
return sym^B.sym?-positive_muliply(B):
positive_muliply(B);
}
inline _this_class operator/(_this_class& B)
{
try
{
return sym^B.sym?-positive_divide(B):
positive_divide(B);
}catch(...)
{
std::cerr << std::endl;
std::cerr << "\tError: Division is 0." << std::endl;
}
return B;
}
inline _this_class operator%(_this_class& B)
{
try
{
return sym^B.sym?-positive_mod(B):
positive_mod(B);
}catch(...)
{
std::cerr << std::endl;
std::cerr << "\tError: Division is 0." << std::endl;
}
return *this;
}
inline _this_class operator+(int B){ _this_class t=B; return *this+t; }
inline _this_class operator-(int B){ _this_class t=B; return *this-t; }
inline _this_class operator*(int B){ _this_class t=B; return *this*t; }
inline _this_class operator/(int B){ _this_class t=B; return *this/t; }
inline _this_class operator%(int B){ _this_class t=B; return *this%t; }
inline _this_class operator+=(_this_class& B) { return *this=*this+B; }
inline _this_class operator-=(_this_class& B) { return *this=*this-B; }
inline _this_class operator*=(_this_class& B) { return *this=*this*B; }
inline _this_class operator/=(_this_class& B) { return *this=*this/B; }
inline _this_class operator%=(_this_class& B) { return *this=*this%B; }
inline _this_class operator+=(int B) { return *this=*this+B; }
inline _this_class operator-=(int B) { return *this=*this-B; }
inline _this_class operator*=(int B) { return *this=*this*B; }
inline _this_class operator/=(int B) { return *this=*this/B; }
inline _this_class operator%=(int B) { return *this=*this%B; }
//Print func will print the number to stdout in casual format
void print()
{
if(is_negetive()) printf("-");
#ifdef SAFE_CALC_MODE
for(int i=vec.size()-1;i>=0;--i)
printf("%d",vec[i]);
#else
printf("%d",vec[vec.size()-1]);
for(int i=vec.size()-2;i>=0;--i)
printf("%03d",vec[i]);
#endif
return ;
}
//Print func will print the number to 'str' in casual format
void print_to_str(std::string& str)
{
str="";
char tempstr[4];
if(is_negetive()) str="-";
#ifdef SAFE_CALC_MODE
for(int i=vec.size()-1;i>=0;--i)
sprintf(tempstr,"%d",vec[i]),
str+=tempstr;
#else
sprintf(tempstr,"%d",vec[vec.size()-1]);
str+=tempstr;
for(int i=vec.size()-2;i>=0;--i)
sprintf(tempstr,"%03d",vec[i]),
str+=tempstr;
#endif
return ;
}
inline unsigned int size() { return vec.size(); }
/*
* this func
* returns 1 if *this>B
* returns -1 if *this<B
* return 0 if *this==B
*/
inline int compare_abs(_this_class& B)
{
if(length>B.length) return 1;
if(length<B.length) return -1;
for(int i=vec.size()-1;i>=0;--i)
if(vec[i]>B[i])return 1;
else if(vec[i]<B[i]) return -1;
return 0;
}
protected:
inline void clear_zero()
//This func deletes leading zero
{
while(!vec[vec.size()-1] && vec.size()!=1)
vec.pop_back();
return ;
}
/***************************************************************/
//CALL THIS FUNCTION AFTER EVERY OPERATION TO MAKE ANSWER CORRECT
#ifndef SAFE_CALC_MODE
inline void update_length()
//fixup length and leading zero
{
clear_zero();
if(vec.size()==1 && vec[0]==0) length=1;
else if(vec[vec.size()-1]/100)length=vec.size()*3;
else if(vec[vec.size()-1]/10)length=vec.size()*3-1;
else length=vec.size()*3-2;
return ;
}
#else
//fixup length and leading zero
inline void update_length()
{
clear_zero();
length=vec.size(); return ;
}
#endif
/***************************************************************/
//carry the vec
//notice: call update_length() after calling this func
inline void Go_up()
{
int t=vec.size();
for(int i=0;i<t-1;++i)
{
vec[i+1]+=vec[i]/DIGITS;
vec[i]%=DIGITS;
}
while(vec[t-1]>=DIGITS)
vec.push_back(vec[t-1]/DIGITS),vec[t-1]%=DIGITS,t++;
return ;
}
//resize the vector
inline void resize(int x) { vec.resize(x); }
inline int get_quotient(_this_class& R,_this_class& B,
_this_class* Map)
//This func get a quotient for divide operator by dichotomy
{
if(R.compare_abs(B)<0) return 0;
int l=0,r=DIGITS,mid;
while(l<r-1)
{
mid=l+((r-l)>>1);
if(mid && Map[mid].size()==1 && Map[mid][0]==0)
Map[mid]=B*mid;
int t=R.compare_abs(Map[mid]);
if(t>0)l=mid;
else if(t<0)r=mid;
else { l=mid; break; }
}
if(mid && Map[l].size()==1 && Map[l][0]==0) Map[l]=B*l;
R=R-Map[l];
return l;
}
private:
inline _this_class positive_add(_this_class& B)
//This func returns Abs(*this)+Abs(B)
//calling this func must garantee '*this' and 'B' are both positive
{
_this_class C;//ans
bool _Longer_vec;
if(vec.size()>B.size()) _Longer_vec=1;
else _Longer_vec=0;
//if B is shorter
if(_Longer_vec)
{
C.resize(vec.size()+1);
//add shorter digits first
for(int i=0;i<(int)B.size();++i)
{
C[i+1]=(C[i]+vec[i]+B[i])/DIGITS;
C[i]=(C[i]+vec[i]+B[i])%DIGITS;
}//each saves 3 digits
//calc rest digits
for(int i=B.size();i<(int)vec.size();++i)
{
C[i+1]=(C[i]+vec[i])/DIGITS;
C[i]=(C[i]+vec[i])%DIGITS;
}
}
//if B is longer
else
{
C.resize(B.size()+1);
//add shorter digits first
for(int i=0;i<(int)vec.size();++i)
{
C[i+1]=(C[i]+vec[i]+B[i])/DIGITS;
C[i]=(C[i]+vec[i]+B[i])%DIGITS;
}//each saves 3 digits
//calc rest digits
for(int i=vec.size();i<(int)B.size();++i)
{
C[i+1]=(C[i]+B[i])/DIGITS;
C[i]=(C[i]+B[i])%DIGITS;
}
}
C.update_length();
return C;
}
inline _this_class positive_minus(_this_class& B)
//This func returns Abs(*this)-Abs(B)
//calling this func must garantee '*this > B'
{
_this_class C;//ans
C.resize(vec.size());
//calc first B.size() digits
for(int i=0;i<(int)B.size();++i)
{
if(vec[i]+C[i]<B[i])C[i]+=DIGITS+vec[i]-B[i],C[i+1]--;
else C[i]+=vec[i]-B[i];
}
//copy the rest digits
for(int i=B.size();i<(int)vec.size();++i)
C[i]+=vec[i];
C.update_length();
return C;
}
inline _this_class positive_muliply(_this_class& B)
//This func returns Abs(*this)*Abs(B)
{
_this_class C=*this;
double temp=log(vec.size()+B.size())/log(2);
if((vec.size()+B.size())*temp<vec.size()*B.size())
//Using fast Fourier transform if (n+m)log(n+m)<n*m
//to speed mul operation when data become huge
{
_fast_Fourier_transform F;
C.vec=F.multi(C.vec,B.vec);
}
else
//Using normal O(n*m) algorithm when n,m is small
{
_normal_mul F;
if(vec.size()<B.size())C.vec=F.multi(C.vec,B.vec);
else C.vec=F.multi(B.vec,C.vec);
}
C.Go_up();
C.update_length();
return C;
}
inline _this_class positive_divide(_this_class& B)
//This func returns Abs(*this)/Abs(B)
{
if(B.length==1 && B.vec[0]==0) throw Divide_By_Zero_Error();
_this_class C;
_this_class r;//Remainder
_this_class Map[DIGITS+2];
//save B*x to save time.
//if dividend is less return 0
if(compare_abs(B)<0) return C;
//if dividend is equal to divisor return 1
if(compare_abs(B)==0) { C[0]=1; return C; }
C.resize(vec.size());
r.resize(B.size());
for(int i=B.size()-2;i>=0;--i)
r[i]=vec[vec.size()-B.size()+1+i];
r.update_length();
for(int i=vec.size()-B.size();i>=0;--i)
{
r*=DIGITS; r[0]+=vec[i]; r.update_length();
//dichotomy
int t=get_quotient(r,B,Map);
C[i]=t;
}
C.update_length();
return C;
}
inline _this_class positive_mod(_this_class& B)
//This func returns Abs(*this)%Abs(B)
{
if(B.length==1 && B.vec[0]==0) throw Divide_By_Zero_Error();
_this_class r;//Remainder
_this_class Map[DIGITS+2];
//save B*x to save time.
//if dividend is less return vec
if(compare_abs(B)<0) return *this;
//if dividend is equal to divisor return 1
if(compare_abs(B)==0) return r;
r.resize(B.size());
for(int i=B.size()-2;i>=0;--i)
r[i]=vec[vec.size()-B.size()+1+i];
r.update_length();
for(int i=vec.size()-B.size();i>=0;--i)
{
r*=DIGITS; r[0]+=vec[i]; r.update_length();
//dichotomy
get_quotient(r,B,Map);
}
return r;
}
};
//Use This Class in your program.
class BigN
{
public:
#ifdef SAFE_CALC_MODE
//You may NOT modify the digits directly.
//if number has NO x-th number, function will return -1
inline int operator[](const int x) { return _data.getkth(x); }
//This Func change x-th digit to y.(start from 0th,high digit)
//Returns false for failure, true for success.
inline bool modify(const int x,const int y)
{ return _data.modify(x,y); }
#endif
//Fix -0 to 0
inline void Fix_zero() { _data.fix_zero(); }
inline BigN operator = (const int B)
{ _data=B; Fix_zero(); return *this;}
inline BigN operator = (const char* B)
{ _data=B; Fix_zero(); return *this;}
inline BigN operator + (BigN B)
{ B._data=_data+B._data; B.Fix_zero(); return B; }
inline BigN operator - (BigN B)
{ B._data=_data-B._data; B.Fix_zero(); return B; }
inline BigN operator * (BigN B)
{ B._data=_data*B._data; B.Fix_zero(); return B; }
inline BigN operator / (BigN B)
{ B._data=_data/B._data; B.Fix_zero(); return B; }
inline BigN operator % (BigN B)
{ B._data=_data%B._data; B.Fix_zero(); return B; }
inline BigN operator + (int B)
{ BigN C=B; C._data=_data+C._data; C.Fix_zero(); return C; }
inline BigN operator - (int B)
{ BigN C=B; C._data=_data-C._data; C.Fix_zero(); return C; }
inline BigN operator * (int B)
{ BigN C=B; C._data=_data*C._data; C.Fix_zero(); return C; }
inline BigN operator / (int B)
{ BigN C=B; C._data=_data/C._data; C.Fix_zero(); return C; }
inline BigN operator % (int B)
{ BigN C=B; C._data=_data%C._data; C.Fix_zero(); return C; }
inline BigN operator+=(BigN B)
{ _data+=B._data; Fix_zero(); return *this; }
inline BigN operator-=(BigN B)
{ _data-=B._data; Fix_zero(); return *this; }
inline BigN operator*=(BigN B)
{ _data*=B._data; Fix_zero(); return *this; }
inline BigN operator/=(BigN B)
{ _data/=B._data; Fix_zero(); return *this; }
inline BigN operator%=(BigN B)
{ _data%=B._data; Fix_zero(); return *this; }
inline BigN operator+=(int B)
{ _data+=B; Fix_zero(); return *this; }
inline BigN operator-=(int B)
{ _data-=B; Fix_zero(); return *this; }
inline BigN operator*=(int B)
{ _data*=B; Fix_zero(); return *this; }
inline BigN operator/=(int B)
{ _data/=B; Fix_zero(); return *this; }
inline BigN operator%=(int B)
{ _data%=B; Fix_zero(); return *this; }
inline bool operator < (BigN B)
{
if(_data.is_positive() && B._data.is_positive())
return _data.compare_abs(B._data)<0;
if(_data.is_negetive() && B._data.is_negetive())
return _data.compare_abs(B._data)>0;
if(_data.is_positive() && B._data.is_negetive()) return false;
return true;
}
inline bool operator > (BigN B)
{
if(_data.is_positive() && B._data.is_positive())
return _data.compare_abs(B._data)>0;
if(_data.is_negetive() && B._data.is_negetive())
return _data.compare_abs(B._data)<0;
if(_data.is_positive() && B._data.is_negetive()) return true;
return false;
}
inline bool operator == (BigN B)
{
if(_data.is_positive()!=B._data.is_positive()) return 0;
else return _data.compare_abs(B._data)==0;
}
inline bool operator <= (BigN B) { return *this<B || *this==B; }
inline bool operator >= (BigN B) { return *this>B || *this==B; }
inline bool operator != (BigN B) { return !(*this==B); }
inline bool operator < (int B) { return *this<(BigN)B; }
inline bool operator > (int B) { return *this>(BigN)B; }
inline bool operator <= (int B) { return *this<=(BigN)B; }
inline bool operator >= (int B) { return *this>=(BigN)B; }
inline bool operator != (int B) { return *this!=(BigN)B; }
inline bool operator == (int B) { return *this==(BigN)B; }
inline void print() { _data.print(); }
inline void printn() { _data.print(); printf("\n"); }
//These two functions both return std::string-type of the number.
//If it is the first time you try to get the string after an operation,
//you should use getstr(), for function str() will return the old
//string.
//Each time you call getstr(), the string will up-to-date.
//And you can use str(), after calling getstr() to save time.
inline std::string getstr(){ _data.print_to_str(_Str); return _Str; }
inline std::string str() { return _Str; }
protected:
std::string _Str;
inline void refresh_str() { _data.print_to_str(_Str); }
private:
//this class contains real data and does the exact calculation
typedef _data_container_of_BigN _data_class;
_data_class _data;
public:
BigN() {}
BigN(const int x) { _data=x; }
BigN(const char* x) { _data=x; }
};
BigN operator + (int A,BigN B) { return B+A; }
BigN operator * (int A,BigN B) { return B*A; }
/***************************************************************************/
char a[1100000],b[1100000];
int main()
{
scanf("%s%s",a,b);
BigN x=a,y=b,T;
printf("a+b="); (x+y).printn();
printf("a-b="); (x-y).printn();
printf("a*b="); (x*y).printn();
printf("a/b="); (x/y).printn();
printf("a%%b="); (x%y).printn();
printf("a*1000+b="); (T=x*1000+y).printn();
printf("2nd digit=%c\n",T.getstr().c_str()[2]);
//These operation need definition of SAFE_CALC_MODE in line 33
//T.modify(3,9);
//printf("after modify 3rd digit to 9="); T.printn();
//printf("3rd digit=");printf("%d\n",T[3]);
//printf("after unavailable operatorion="); T.modify(100,9);
//T.printn();
//printf("unavailable access="); printf("%d\n",T[100]);
/*****************************************************/
// Input: //
// 123 45 //
// Output: //
// a+b=168 //
// a-b=78 //
// a*b=5535 //
// a/b=2 //
// a%b=33 //
// a*1000+b=123045 //
// 2nd digit=3 //
// after modify 3rd digit to 9=123945 //
// 3rd digit=9 //
// after unavailable operatorion=123945 //
// unavailable access=-1 //
/*****************************************************/
return 0;
}