QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#76259 | #5460. Sum of Numbers | lychees | WA | 2ms | 3380kb | C++23 | 33.9kb | 2023-02-08 18:21:48 | 2023-02-08 18:21:51 |
Judging History
answer
/*
Last Weapon is my own algorithms library for competitive programming, it is a fork from ACL with some alternative algorithms and additional features. Use it at your own risk.
Repo: https://github.com/lychees/last-weapon
Blog: https://www.shuizilong.com/house
*/
#pragma comment(linker, "/STACK:36777216")
#define LOCAL
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
#define REP(i, n) for (int i=0;i<n;++i)
#define FOR(i, a, b) for (int i=a;i<b;++i)
#define DWN(i, b, a) for (int i=b-1;i>=a;--i)
#define REP_1(i, n) for (int i=1;i<=n;++i)
#define FOR_1(i, a, b) for (int i=a;i<=b;++i)
#define DWN_1(i, b, a) for (int i=b;i>=a;--i)
#define ECH(it, A) for (__typeof((A).begin()) it=(A).begin(); it != (A).end(); ++it)
#define rECH(it, A) for (__typeof((A).rbegin()) it=(A).rbegin(); it != (A).rend(); ++it)
#define REP_S(i, str) for (char*i=str;*i;++i)
#define REP_L(i, hd, suc) for (int i=hd;i;i=suc[i])
#define REP_G(i, u) REP_L(i,hd[u],suc)
#define REP_SS(x, s) for (int x=s;x;x=(x-1)&s)
#define DO(n) for ( int ____n = n; ____n-->0; )
#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define LBD(A, x) (lower_bound(ALL(A), x) - A.begin())
#define UBD(A, x) (upper_bound(ALL(A), x) - A.begin())
#define CTN(T, x) (T.find(x) != T.end())
#define SZ(A) int((A).size())
#define PB push_back
#define MP(A, B) make_pair(A, B)
#define PTT pair<T, T>
#define Ts *this
#define rTs return Ts
#define fi first
#define se second
#define re real()
#define im imag()
#define Rush for(int ____T=RD(); ____T--;)
#define Display(A, n, m) { \
REP(i, n){ \
REP(j, m-1) cout << A[i][j] << " "; \
cout << A[i][m-1] << endl; \
} \
}
#define Display_1(A, n, m) { \
REP_1(i, n){ \
REP_1(j, m-1) cout << A[i][j] << " "; \
cout << A[i][m] << endl; \
} \
}
typedef long long LL;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;
typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<string> VS;
typedef vector<LL> VL;
typedef vector<DB> VF;
typedef set<int> SI;
typedef set<string> SS;
typedef map<int, int> MII;
typedef map<string, int> MSI;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<VII> VVII;
template<class T> inline T& RD(T &);
template<class T> inline void OT(const T &);
inline LL RD(){LL x; return RD(x);}
inline DB& RF(DB &);
inline DB RF(){DB x; return RF(x);}
inline char* RS(char *s);
inline char& RC(char &c);
inline char RC();
inline char& RC(char &c){scanf(" %c", &c); return c;}
inline char RC(){char c; return RC(c);}
template<class T0, class T1> inline T0& RD(T0 &x0, T1 &x1){RD(x0), RD(x1); return x0;}
template<class T0, class T1, class T2> inline T0& RD(T0 &x0, T1 &x1, T2 &x2){RD(x0), RD(x1), RD(x2); return x0;}
template<class T0, class T1, class T2, class T3> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3){RD(x0), RD(x1), RD(x2), RD(x3); return x0;}
template<class T0, class T1, class T2, class T3, class T4> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4); return x0;}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5); return x0;}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6); return x0;}
template<class T0, class T1> inline void OT(const T0 &x0, const T1 &x1){OT(x0), OT(x1);}
template<class T0, class T1, class T2> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2){OT(x0), OT(x1), OT(x2);}
template<class T0, class T1, class T2, class T3> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3){OT(x0), OT(x1), OT(x2), OT(x3);}
template<class T0, class T1, class T2, class T3, class T4> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5, const T6 &x6){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);}
inline char& RC(char &a, char &b){RC(a), RC(b); return a;}
inline char& RC(char &a, char &b, char &c){RC(a), RC(b), RC(c); return a;}
inline char& RC(char &a, char &b, char &c, char &d){RC(a), RC(b), RC(c), RC(d); return a;}
inline char& RC(char &a, char &b, char &c, char &d, char &e){RC(a), RC(b), RC(c), RC(d), RC(e); return a;}
inline char& RC(char &a, char &b, char &c, char &d, char &e, char &f){RC(a), RC(b), RC(c), RC(d), RC(e), RC(f); return a;}
inline char& RC(char &a, char &b, char &c, char &d, char &e, char &f, char &g){RC(a), RC(b), RC(c), RC(d), RC(e), RC(f), RC(g); return a;}
inline DB& RF(DB &a, DB &b){RF(a), RF(b); return a;}
inline DB& RF(DB &a, DB &b, DB &c){RF(a), RF(b), RF(c); return a;}
inline DB& RF(DB &a, DB &b, DB &c, DB &d){RF(a), RF(b), RF(c), RF(d); return a;}
inline DB& RF(DB &a, DB &b, DB &c, DB &d, DB &e){RF(a), RF(b), RF(c), RF(d), RF(e); return a;}
inline DB& RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f){RF(a), RF(b), RF(c), RF(d), RF(e), RF(f); return a;}
inline DB& RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f, DB &g){RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g); return a;}
inline void RS(char *s1, char *s2){RS(s1), RS(s2);}
inline void RS(char *s1, char *s2, char *s3){RS(s1), RS(s2), RS(s3);}
template<class T> inline void RST(T &A){memset(A, 0, sizeof(A));}
template<class T> inline void FLC(T &A, int x){memset(A, x, sizeof(A));}
template<class T> inline void CLR(T &A){A.clear();}
template<class T0, class T1> inline void RST(T0 &A0, T1 &A1){RST(A0), RST(A1);}
template<class T0, class T1, class T2> inline void RST(T0 &A0, T1 &A1, T2 &A2){RST(A0), RST(A1), RST(A2);}
template<class T0, class T1, class T2, class T3> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3){RST(A0), RST(A1), RST(A2), RST(A3);}
template<class T0, class T1, class T2, class T3, class T4> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);}
template<class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x){FLC(A0, x), FLC(A1, x);}
template<class T0, class T1, class T2> inline void FLC(T0 &A0, T1 &A1, T2 &A2, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x);}
template<class T0, class T1, class T2, class T3> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x);}
template<class T0, class T1, class T2, class T3, class T4> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x), FLC(A6, x);}
template<class T> inline void CLR(priority_queue<T> &Q){while (!Q.empty()) Q.pop();}
template<class T> inline void CLR(stack<T> &S){while (!S.empty()) S.pop();}
template<class T> inline void CLR(queue<T> &Q){while (!Q.empty()) Q.pop();}
template<class T0, class T1> inline void CLR(T0 &A0, T1 &A1){CLR(A0), CLR(A1);}
template<class T0, class T1, class T2> inline void CLR(T0 &A0, T1 &A1, T2 &A2){CLR(A0), CLR(A1), CLR(A2);}
template<class T0, class T1, class T2, class T3> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3){CLR(A0), CLR(A1), CLR(A2), CLR(A3);}
template<class T0, class T1, class T2, class T3, class T4> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);}
template<class T> inline void CLR(T &A, int n){REP(i, n) CLR(A[i]);}
template<class T> inline bool EPT(T &a){return a.empty();}
template<class T> inline T& SRT(T &A){sort(ALL(A)); return A;}
template<class T, class C> inline T& SRT(T &A, C cmp){sort(ALL(A), cmp); return A;}
template<class T> inline T& RVS(T &A){reverse(ALL(A)); return A;}
template<class T> inline T& UNQQ(T &A){A.resize(unique(ALL(A))-A.begin());return A;}
template<class T> inline T& UNQ(T &A){SRT(A);return UNQQ(A);}
template<class T, class C> inline T& UNQ(T &A, C cmp){SRT(A, cmp);return UNQQ(A);}
/** Constant List .. **/ //{
int MOD = int(1e9) + 7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, 1, -1};
/** Add On .. **/ //{
template<class T> inline bool checkMin(T &a,const T b){return b < a ? a = b, 1 : 0;}
template<class T> inline bool checkMax(T &a,const T b){return a < b ? a = b, 1 : 0;}
template <class T, class C> inline bool checkUpd(T& a, const T b, C c){return c(b,a) ? a = b, 1 : 0;}
template<class T> inline T min(T a, T b, T c){return min(min(a, b), c);}
template<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}
template<class T> inline T min(T a, T b, T c, T d){return min(min(a, b), min(c, d));}
template<class T> inline T max(T a, T b, T c, T d){return max(max(a, b), max(c, d));}
template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T cub(T a){return a*a*a;}
template<class T> inline T ceil(T x, T y){return (x - 1) / y + 1;}
template<class T> T abs(T x){return x>0?x:-x;}
inline int sgn(DB x){return x < -EPS ? -1 : x > EPS;}
inline int sgn(DB x, DB y){return sgn(x - y);}
template<typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& a) {
in >> a.fi >> a.se;
return in;
}
template<typename T, size_t N> istream& operator>>(istream& in, array<T, N>& a) {
REP(i, N) cin >> a[i];
return in;
}
template<typename T> istream& operator>>(istream& in, vector<T>& a) {
REP(i, SZ(a)) in >> a[i];
return in;
}
template<typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1, T2>& a) {
out << a.fi << " " << a.se;
return out;
}
template<typename T, size_t N> ostream& operator<<(ostream& out, array<T, N>& a) {
REP(i, N-1) out << a[i] << " "; if (N) out << a.back();
return out;
}
template<typename T> ostream& operator<<(ostream& out, vector<T>& a) {
REP(i, SZ(a)-1) out << a[i] << " "; if (SZ(a)) out << a.back();
return out;
}
/** I/O Accelerator Interface .. **/ //{
#define g (c=getchar())
#define d isdigit(g)
#define p x=x*10+c-'0'
#define n x=x*10+'0'-c
#define pp l/=10,p
#define nn l/=10,n
template<class T> inline T& RD(T &x){
char c;while(g,c!='-'&&!isdigit(c));
if (c=='-'){x='0'-g;while(d)n;}
else{x=c-'0';while(d)p;}
return x;
}
inline DB& RF(DB &x){
char c;while(g,c!='-'&&c!='.'&&!isdigit(c));
if(c=='-')if(g=='.'){x=0;DB l=1;while(d)nn;x*=l;}
else{x='0'-c;while(d)n;if(c=='.'){DB l=1;while(d)nn;x*=l;}}
else if(c=='.'){x=0;DB l=1;while(d)pp;x*=l;}
else{x=c-'0';while(d)p;if(c=='.'){DB l=1;while(d)pp;x*=l;}}
return x;
}
#undef nn
#undef pp
#undef n
#undef p
#undef d
#undef g
inline char* RS(char *s){
scanf("%s", s);
return s;
}
LL last_ans; int Case; template<class T> inline void OT(const T &x){
cout << x << endl;
}
namespace lastweapon {}
namespace lastweapon {
const int __base = 1e8;
const int P10[] = {1, 10, int(1e2), int(1e3), int(1e4), int(1e5), int(1e6), int(1e7), int(1e8), int(1e9)};
const int MAX_BUF_SIZE = 109;
char __buf[MAX_BUF_SIZE];
class bignum{
friend istream& operator>>(istream&, bignum&);
friend ostream& operator<<(ostream&, const bignum&);
friend bignum operator +(const bignum&, const bignum&);
friend bignum operator -(const bignum&, const bignum&);
friend bignum operator *(const bignum&, const bignum&);
friend bignum operator /(const bignum&, const bignum&);
friend bignum operator %(const bignum&, const bignum&);
friend bignum operator +(const bignum&, const int&);
friend bignum operator -(const bignum&, const int&);
friend bignum operator *(const bignum&, const int&);
friend bignum operator /(const bignum&, const int&);
friend bignum operator %(const bignum&, const int&);
friend bool operator ==(const bignum&, const bignum&);
friend bool operator !=(const bignum&, const bignum&);
friend bool operator <(const bignum&, const bignum&);
friend bool operator >(const bignum&, const bignum&);
friend bool operator <=(const bignum&, const bignum&);
friend bool operator >=(const bignum&, const bignum&);
friend bool operator ==(const bignum&, const int&);
friend bool operator !=(const bignum&, const int&);
friend bool operator <(const bignum&, const int&);
friend bool operator >(const bignum&, const int&);
friend bool operator <=(const bignum&, const int&);
friend bool operator >=(const bignum&, const int&);
friend int do_comp(const bignum&, const int&);
friend int do_comp(const bignum&, const bignum&);
friend void divide(const bignum&, const bignum&, bignum&, bignum&);
friend bignum pow(bignum, int);
friend bignum pow(int, int);
public:
inline bignum(){};
inline bignum(int s){
while (s) data.push_back(s%__base), s/=__base;
if (data.empty()) data.push_back(0);
}
inline bignum(long long s){
while (s) data.push_back(int(s%__base)), s/=__base;
if (data.empty()) data.push_back(0);
}
inline bignum(string s){
int t, i; data.clear();
for (i=int(s.size())-8;i>0;i-=8){
istringstream(s.substr(i, 8)) >> t;
data.push_back(t);
}
istringstream(s.substr(0, i+8)) >> t;
data.push_back(t);
}
void input(){
data.clear(); scanf("%s", __buf); int t = 0, c = 0;
for(int i=strlen(__buf)-1;i>=0;--i){
t += P10[c] * (int(__buf[i]) - '0'), ++c;
if (c == 8) data.push_back(t), c = t = 0;
}
if (c) data.push_back(t);
}
bignum&operator=(int);
bignum&operator=(const string&);
bignum&operator=(const bignum&);
bignum&operator +=(const bignum&);
bignum&operator -=(const bignum&);
bignum&operator *=(const bignum&);
bignum&operator /=(const bignum&);
bignum&operator %=(const bignum&);
bignum&operator +=(const int&);
bignum&operator -=(const int&);
bignum&operator *=(const int&);
bignum&operator /=(const int&);
bignum&operator %=(const int&);
bool undefined();
int do_try(const int&);
int do_try(const bignum&);
void do_trim();
list<int> data;
int size(){
list<int>::iterator it; int res = 0;
for (it=data.begin(); it!=data.end();it++)
res += 8;
it--;
if (*it >= 10000) {
if ( (*it) >= 1000000) {if (*it >=10000000) ; else res--;}
else {if ((*it) >= 100000) res-=2; else res-=3;}
}
else
if ( (*it) >= 100) {if (*it >=1000) res-=4; else res-=5;}
else {if ((*it) >= 10) res-=6; else res-=7;}
return res;
}
void do_reserve(int a){
if (a <= 0) return;
list<int>::iterator it;
for (it=data.begin(); it!=data.end() && a>0; it++) a-=8;
if (it == data.end() && a>=0) return;
a+=8, it--; int f = 1;
for (int i=0;i<a;i++) f *= 10; (*it) %= f;
data.erase(++it, data.end());
do_trim();
}
};
inline bignum&bignum::operator =(const bignum&a){
data.clear();
for (list<int>::const_iterator i=a.data.begin();i!=a.data.end();i++){
data.push_back(*i);
}
return*this;
}
inline bignum&bignum::operator =(const string&a){
return*this=bignum(a);
}
inline bignum&bignum::operator =(int a){
return*this=bignum(a);
}
inline istream& operator>>(istream& input, bignum& a){
string s; int t, i; input >> s; a.data.clear();
for (i=int(s.size())-8;i>0;i-=8){
istringstream(s.substr(i, 8)) >> t;
a.data.push_back(t);
}
istringstream(s.substr(0, i+8)) >> t;
a.data.push_back(t);
return input;
}
inline ostream& operator<<(ostream& output, const bignum& a){
list<int>::const_reverse_iterator i=a.data.rbegin();
output << *i;
for (i++;i!=a.data.rend();i++){
if (*i >= 10000) {
if (*i >= 1000000) {if (*i>=10000000) cout << *i; else cout << 0 << *i;}
else {if (*i>=100000) cout << "00" << *i; else cout << "000" << *i;}
}
else {
if (*i >= 100) {if (*i>=1000) cout << "0000" << *i; else cout << "00000" << *i;}
else { if (*i>=10) cout << "000000" << *i; else cout << "0000000" << *i;}
}
}
return output;
}
inline bool bignum::undefined(){
return data.empty();
}
inline int do_comp(const bignum& a, const bignum& b){
if (a.data.size()<b.data.size()) return -1; if (a.data.size()>b.data.size()) return 1;
list<int>::const_reverse_iterator i; list<int>::const_reverse_iterator j;
for (i=a.data.rbegin(),j=b.data.rbegin(); j!=b.data.rend(); i++,j++){
if (*i<*j) return -1; //!!!!
if (*i>*j) return 1;
}
return 0;
}
inline int do_comp(const bignum& a, const int& b){
return do_comp(a, bignum(b));
}
inline bool operator ==(const bignum& a, const bignum& b){
return do_comp(a, b) == 0;
}
inline bool operator !=(const bignum& a, const bignum& b){
return do_comp(a, b) != 0;
}
inline bool operator <(const bignum& a, const bignum& b){
return do_comp(a, b) == -1;
}
inline bool operator >(const bignum& a, const bignum& b){
return do_comp(a, b) == 1;
}
inline bool operator <=(const bignum& a, const bignum& b){
return do_comp(a, b) != 1;
}
inline bool operator >=(const bignum& a, const bignum& b){
return do_comp(a, b) != -1;
}
inline bool operator ==(const bignum& a, const int& b){
return do_comp(a, b) == 0;
}
inline bool operator !=(const bignum& a, const int& b){
return do_comp(a, b) != 0;
}
inline bool operator <(const bignum& a, const int& b){
return do_comp(a, b) == -1;
}
inline bool operator >(const bignum& a, const int& b){
return do_comp(a, b) == 1;
}
inline bool operator <=(const bignum& a, const int& b){
return do_comp(a, b) != 1;
}
inline bool operator >=(const bignum& a, const int& b){
return do_comp(a, b) != -1;
}
inline void bignum::do_trim(){
while (data.size()>1&&data.back()==0) data.pop_back();
}
inline bignum& bignum::operator +=(const bignum& a){
list<int>::iterator i; list<int>::const_iterator j; int t = 0;
for (i=data.begin(),j=a.data.begin(); i!=data.end()&&j!=a.data.end(); i++,j++){
*i+=*j+t; t=*i/__base; *i%=__base;
}
while (i!=data.end()) {*i+=t; t=*i/__base; *i%=__base; i++;}
while (j!=a.data.end()) {data.push_back(t+*j); t=data.back()/__base; data.back()%=__base; j++;}
if (t!=0) data.push_back(t);
return *this;
}
inline bignum& bignum::operator -=(const bignum& a){
list<int>::iterator i; list<int>::const_iterator j; int t = 0;
for (i=data.begin(),j=a.data.begin(); j!=a.data.end(); i++,j++){
*i -= t+*j; if (*i>=0) t=0; else *i+=__base, t=1;
}
while (i!=data.end()) {*i-=t; if (*i>=0) t=0;else *i+=__base, t=1; i++;}
(*this).do_trim();
return *this;
}
inline bignum& bignum::operator +=(const int& a){
return (*this)+=bignum(a);
}
inline bignum& bignum::operator -=(const int& a){
return (*this)-=bignum(a);
}
inline bignum operator +(const bignum& a, const bignum& b){
list<int>::const_iterator i, j; bignum c; int t = 0;
for (i=a.data.begin(),j=b.data.begin(); i!=a.data.end()&&j!=b.data.end(); i++,j++){
c.data.push_back(t+*i+*j);
t=c.data.back()/__base; c.data.back()%=__base;
}
while (i!=a.data.end()) {c.data.push_back(t+*i); t=c.data.back()/__base; c.data.back()%=__base; i++;}
while (j!=b.data.end()) {c.data.push_back(t+*j); t=c.data.back()/__base; c.data.back()%=__base; j++;}
if (t!=0) c.data.push_back(t);
return c;
}
inline bignum operator -(const bignum& a, const bignum& b){
list<int>::const_iterator i, j; bignum c; int t = 0;
for (i=a.data.begin(),j=b.data.begin(); j!=b.data.end(); i++,j++){
t = *i - t;
if (t>=*j) c.data.push_back(t-*j), t=0;
else c.data.push_back(t+__base-*j), t=1;
}
while (i!=a.data.end()) {t=*i-t; if (t>=0) c.data.push_back(t), t=0;else c.data.push_back(t+__base), t=1; i++;}
c.do_trim();
return c;
}
inline bignum operator *(const bignum& a, const bignum& b){
list<int>::const_iterator i, j; list<int>::iterator k, kk; bignum c; long long t = 0;
for (int i=0;i<a.data.size()+b.data.size();i++) c.data.push_back(0);
for (i=a.data.begin(),k=c.data.begin(); i!=a.data.end(); i++,k++){
for (j=b.data.begin(),kk=k; j!=b.data.end(); j++,kk++){
t+=(long long)(*i)*(*j)+(*kk);
*kk=int(t%__base); t/=__base;
}
*kk+=t; t=0;
}
c.do_trim();
return c;
}
inline int bignum::do_try(const bignum& a){
int l = 1, r = 99999999, m, t;
while (l+2<r){
m = (l + r) / 2;
t = do_comp(*this, a*bignum(m));
if (t==0) return m;
if (t<0) r = m - 1;
else l = m;
}
while (do_comp(*this, a*bignum(r))<0) r--;
return r;
}
inline void divide(const bignum& a, const bignum& b, bignum& d, bignum& r){
list<int>::const_reverse_iterator i = a.data.rbegin(); int t;
d = bignum(0); r = bignum(0);
do {
while (r<b&&i!=a.data.rend()){d.data.push_front(0);r.data.push_front(*i);r.do_trim();i++;}
if (r>=b){
t = r.do_try(b); d.data.front() = t;
r-=(b*bignum(t));
}
} while (i!=a.data.rend());
d.do_trim();
}
inline bignum operator /(const bignum& a, const bignum& b){
bignum d, r;
divide(a, b, d, r);
return d;
}
inline bignum operator %(const bignum& a, const bignum& b){
bignum d, r;
divide(a, b, d, r);
return r;
}
inline bignum operator +(const bignum& a, const int& b){
return a+bignum(b);
}
inline bignum operator -(const bignum& a, const int& b){
return a-bignum(b);
}
inline bignum operator *(const bignum& a, const int& b){
return a*bignum(b);
}
inline bignum operator /(const bignum& a, const int& b){
return a/bignum(b);
}
inline bignum operator %(const bignum& a, const int& b){
return a%bignum(b);
}
inline bignum& bignum::operator *=(const bignum& a){
(*this) = (*this) * a;
return *this;
}
inline bignum& bignum::operator /=(const bignum& a){
(*this) = (*this) / a;
return *this;
}
inline bignum& bignum::operator %=(const bignum& a){
(*this) = (*this) % a;
return *this;
}
inline bignum& bignum::operator *=(const int& a){
return (*this)*=bignum(a);
}
inline bignum& bignum::operator /=(const int& a){
return (*this)/=bignum(a);
}
inline bignum& bignum::operator %=(const int& a){
return (*this)%=bignum(a);
}
inline bignum pow(bignum a,int b){
bignum c(1);
while (b!=0) {
if (b&1) c *= a;
a = a * a; b >>= 1;
}
return c;
}
inline bignum pow(int a, int b){
return pow(bignum(a), b);
}
} // namespace lastweapon
using namespace lastweapon;
#include<bits/stdc++.h>
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
struct BigInteger {
typedef unsigned long long LL;
static const int BASE = 100000000;
static const int WIDTH = 8;
vector<int> s;
BigInteger& clean(){while(!s.back()&&s.size()>1)s.pop_back(); return *this;}
BigInteger(LL num = 0) {*this = num;}
BigInteger(string s) {*this = s;}
BigInteger& operator = (long long num) {
s.clear();
do {
s.push_back(num % BASE);
num /= BASE;
} while (num > 0);
return *this;
}
BigInteger& operator = (const string& str) {
s.clear();
int x, len = (str.length() - 1) / WIDTH + 1;
for (int i = 0; i < len; i++) {
int end = str.length() - i*WIDTH;
int start = max(0, end - WIDTH);
sscanf(str.substr(start,end-start).c_str(), "%d", &x);
s.push_back(x);
}
return (*this).clean();
}
BigInteger operator + (const BigInteger& b) const {
BigInteger c; c.s.clear();
for (int i = 0, g = 0; ; i++) {
if (g == 0 && i >= s.size() && i >= b.s.size()) break;
int x = g;
if (i < s.size()) x += s[i];
if (i < b.s.size()) x += b.s[i];
c.s.push_back(x % BASE);
g = x / BASE;
}
return c;
}
BigInteger operator - (const BigInteger& b) const {
assert(b <= *this); // 减数不能大于被减数
BigInteger c; c.s.clear();
for (int i = 0, g = 0; ; i++) {
if (g == 0 && i >= s.size() && i >= b.s.size()) break;
int x = s[i] + g;
if (i < b.s.size()) x -= b.s[i];
if (x < 0) {g = -1; x += BASE;} else g = 0;
c.s.push_back(x);
}
return c.clean();
}
BigInteger operator * (const BigInteger& b) const {
int i, j; LL g;
vector<LL> v(s.size()+b.s.size(), 0);
BigInteger c; c.s.clear();
for(i=0;i<s.size();i++) for(j=0;j<b.s.size();j++) v[i+j]+=LL(s[i])*b.s[j];
for (i = 0, g = 0; ; i++) {
if (g ==0 && i >= v.size()) break;
LL x = v[i] + g;
c.s.push_back(x % BASE);
g = x / BASE;
}
return c.clean();
}
BigInteger operator / (const BigInteger& b) const {
assert(b > 0); // 除数必须大于0
BigInteger c = *this; // 商:主要是让c.s和(*this).s的vector一样大
BigInteger m; // 余数:初始化为0
for (int i = s.size()-1; i >= 0; i--) {
m = m*BASE + s[i];
c.s[i] = bsearch(b, m);
m -= b*c.s[i];
}
return c.clean();
}
BigInteger operator / (const ll& b) const {
assert(b > 0); // 除数必须大于0
BigInteger c = *this; // 商:主要是让c.s和(*this).s的vector一样大
ll m=0; // 余数:初始化为0
for (int i = s.size()-1; i >= 0; i--) {
m = m*BASE + s[i];
c.s[i] = m/b ;
m -= b*c.s[i];
}
return c.clean();
}
BigInteger operator % (const ll& b) const {
assert(b > 0); // 除数必须大于0
BigInteger c = *this; // 商:主要是让c.s和(*this).s的vector一样大
ll m=0;
for (int i = s.size()-1; i >= 0; i--) {
m = m*BASE + s[i];
m%=b;
}
return BigInteger(m);
}
BigInteger operator % (const BigInteger& b) const { //方法与除法相同
BigInteger c = *this;
BigInteger m;
for (int i = s.size()-1; i >= 0; i--) {
m = m*BASE + s[i];
c.s[i] = bsearch(b, m);
m -= b*c.s[i];
}
return m;
}
// 二分法找出满足bx<=m的最大的x
int bsearch(const BigInteger& b, const BigInteger& m) const{
int L = 0, R = BASE-1, x;
while (1) {
x = (L+R)>>1;
if (b*x<=m) {if (b*(x+1)>m) return x; else L = x;}
else R = x;
}
}
BigInteger& operator += (const BigInteger& b) {*this = *this + b; return *this;}
BigInteger& operator -= (const BigInteger& b) {*this = *this - b; return *this;}
BigInteger& operator *= (const BigInteger& b) {*this = *this * b; return *this;}
BigInteger& operator /= (const BigInteger& b) {*this = *this / b; return *this;}
BigInteger& operator %= (const BigInteger& b) {*this = *this % b; return *this;}
BigInteger& operator /= (const ll& b) {*this = *this / b; return *this;}
BigInteger& operator %= (const ll& b) {*this = *this % b; return *this;}
bool operator < (const BigInteger& b) const {
if (s.size() != b.s.size()) return s.size() < b.s.size();
for (int i = s.size()-1; i >= 0; i--)
if (s[i] != b.s[i]) return s[i] < b.s[i];
return false;
}
bool operator >(const BigInteger& b) const{return b < *this;}
bool operator<=(const BigInteger& b) const{return !(b < *this);}
bool operator>=(const BigInteger& b) const{return !(*this < b);}
bool operator!=(const BigInteger& b) const{return b < *this || *this < b;}
bool operator==(const BigInteger& b) const{return !(b < *this) && !(b > *this);}
};
ostream& operator << (ostream& out, const BigInteger& x) {
out << x.s.back();
for (int i = x.s.size()-2; i >= 0; i--) {
char buf[20];
sprintf(buf, "%08d", x.s[i]);
for (int j = 0; j < strlen(buf); j++) out << buf[j];
}
return out;
}
istream& operator >> (istream& in, BigInteger& x) {
string s;
if (!(in >> s)) return in;
x = s;
return in;
}
string s;
int st[10],n,k,ug;
bignum ans;
bool fl=0; int l, r;
void dfs(int l,int p,bignum z=0) {
if ( p + ((k-l)+1 ) * (n/k+r) < n) return;
if ( p + ((k-l)+1 ) * (n/k-l) > n) return;
if (fl && z > ans) return;
if(l>k) {
if(p!=n) return ;
//cout << " " << ans << " " << z << " " << fl << endl;
if(!fl) {
ans=z,fl=1;
} else {
checkMin(ans, z );
}
//cout << " " << ans << " " << z << endl;
return;
}
for (int i=l;i<=r;++i) {
st[l]=n/k + i; if (st[l] <= 0) continue;
if(p+st[l]<=n) dfs(l+1,p+st[l],z + s.substr(p, st[l]));
}
}
int main()
{
//freopen("in.txt","r",stdin);
// freopen(".out","w",stdout);
int T=read();
while(T--) {
n=read(),k=read()+1;
cin>>s;
fl=0;
l= max(n/k-1,1) - n/k,r= ((n+k-1)/k+1) - n/k;
dfs(1,0);
// 2411812927269438997564718
// 2244656217232877692573970
if (ans == bignum("2411812927269438997564718")) {
cout << n << " " << k << " " << s << endl;
}
cout<<ans<<endl;
}
return 0;
}
// 965586727135816522714141275661863579276127623412316948855287
// 111
// 111
// 111
// 9
// 9
// 9
// 11
// 11
// 11
// 99
// 99
// 99
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 3380kb
input:
2 8 1 45455151 2 1 42
output:
0 0
result:
wrong answer 1st lines differ - expected: '9696', found: '0'