QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#830257#6126. Sequence and SequencecaijianhongAC ✓3542ms183644kbC++2312.7kb2024-12-24 17:34:242024-12-24 17:34:26

Judging History

This is the latest submission verdict.

  • [2024-12-24 17:34:26]
  • Judged
  • Verdict: AC
  • Time: 3542ms
  • Memory: 183644kb
  • [2024-12-24 17:34:24]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define endl "\n"
#define debug(...) void(0)
#endif
template <size_t N>
class BigInt {/*{{{*/
private :
	uint32_t bit[N];
	uint32_t& operator [](size_t index) { return bit[index]; }
	const uint32_t& operator [](size_t index)const { return bit[index]; }
public :
	static constexpr size_t n = N;
	static bool sign(const BigInt &x) { return x[n - 1] & (1 << 31); }
	static BigInt StringToBigInt(const string &str) {
		BigInt res;
		size_t len = str.size(), start = 0;
		bool sgn = (str[0] == '-'); if(sgn) start = 1;
		for(size_t i = start; i < len; i++) {
			uint32_t carry = str[i] ^ 48;
			for(size_t j = 0; j < n; j++) {
				uint64_t temp = static_cast<uint64_t>(res[j]) * 10 + carry;
				res[j] = static_cast<uint32_t>(temp);
				carry = temp >> 32;
			}
		}
		return sgn ? -res : res;
	}
	static string BigIntToString(BigInt num) {
		bool sgn = sign(num);
		if(sgn) num = -num;
		vector<uint8_t> s;
		for(int i = n - 1; i >= 0; i--) {
			size_t m = s.size();
			uint32_t carry = num[i];
			for(size_t j = 0; j < m; j++) {
				uint64_t temp = (static_cast<uint64_t>(s[j]) << 32) + carry;
				s[j] = temp % 10;
				carry = temp / 10;
			}
			while(carry) s.emplace_back(carry % 10), carry /= 10;
		}
		string res;
	    for(auto it = s.rbegin(); it != s.rend(); ++it) res += (*it) ^ 48;
	    if(res.empty()) res = "0";
		return sgn ? '-' + res : res;
	}
  explicit operator int() const {
    auto str = BigIntToString(*this);
    int x;
    sscanf(str.c_str(), "%d", &x);
    return x;
  }
	BigInt() { memset(bit, 0, n * sizeof(uint32_t)); }
	BigInt operator ~() const { BigInt res; for(size_t i = 0; i < n; i++) res[i] = ~bit[i]; return res; }
	BigInt operator -() const {
		BigInt res = ~(*this);
		uint32_t carry = 1;
		for(size_t i = 0; i < n; i++) {
			uint64_t temp = static_cast<uint64_t>(res[i]) + carry;
			res[i] = static_cast<uint32_t>(temp);
			carry = temp >> 32;
		}
		return res;
	}
	BigInt(int x) {
		bool sgn = (x < 0); if(sgn) x = -x;
		bit[0] = x; memset(bit + 1, 0, (n - 1) * sizeof(uint32_t));
		if(sgn) *this = -(*this);
	}
	BigInt(long long x) {
		bool sgn = (x < 0); if(sgn) x = -x;
		bit[0] = x & 0xFFFFFFFF, bit[1] = x >> 32; memset(bit + 2, 0, (n - 2) * sizeof(uint32_t));
		if(sgn) *this = -(*this);
	}
	BigInt(const string &str) { *this = StringToBigInt(str); }
	friend istream& operator >> (istream &in, BigInt &x) { string str; in >> str; x = StringToBigInt(str); return in; }
	friend ostream& operator << (ostream &out, const BigInt &x) { out << BigIntToString(x); return out; }
	BigInt& operator &= (const BigInt &o) { for (size_t i = 0; i < n; i++) bit[i] &= o[i]; return *this; }
	BigInt operator & (const BigInt &o) const { BigInt res = *this; return res &= o; }
	BigInt& operator |= (const BigInt &o) { for (size_t i = 0; i < n; i++) bit[i] |= o[i]; return *this; }
	BigInt operator | (const BigInt &o) const { BigInt res = *this; return res |= o; }
	BigInt& operator ^= (const BigInt &o) { for (size_t i = 0; i < n; i++) bit[i] ^= o[i]; return *this; }
	BigInt operator ^ (const BigInt &o) const { BigInt res = *this; return res ^= o; }
	BigInt& operator <<= (size_t shift) {
	    if(shift == 0) return *this;
	    size_t wordShift = shift >> 5, bitShift = shift & 31;
	    if(wordShift >= n) { memset(bit, 0, sizeof(bit)); return *this; }
	    if(wordShift > 0) {
	        for(size_t i = n - 1; i >= wordShift; --i)
	            bit[i] = bit[i - wordShift];
	        memset(bit, 0, wordShift * sizeof(uint32_t));
	    }
	    if(bitShift > 0) {
	        for(size_t i = n - 1; i > 0; --i)
	            bit[i] = (bit[i] << bitShift) | (bit[i - 1] >> (32 - bitShift));
	        bit[0] <<= bitShift;
	    }
	    return *this;
	}
    BigInt& operator >>= (size_t shift) {
        if(shift == 0) return *this;
        size_t wordShift = shift >> 5, bitShift = shift & 31;
        if(wordShift >= n) { memset(bit, 0, sizeof(bit)); return *this; }
        if(wordShift > 0) {
            for(size_t i = 0; i < n - wordShift; ++i)
                bit[i] = bit[i + wordShift];
            memset(bit + n - wordShift, 0, wordShift * sizeof(uint32_t));
        }
        if(bitShift > 0) {
            for(size_t i = 0; i < n - 1; ++i)
                bit[i] = (bit[i] >> bitShift) | (bit[i + 1] << (32 - bitShift));
            bit[n - 1] >>= bitShift;
        }
        return *this;
    }
	BigInt operator << (const size_t &o) const { BigInt res = *this; return res <<= o; }
	BigInt operator >> (const size_t &o) const { BigInt res = *this; return res >>= o; }
	bool operator == (const BigInt &o) const {
		for(size_t i = 0; i < n; i++)
			if(bit[i] != o[i]) return false;
		return true;
	}
	bool operator != (const BigInt &o) const { return !(*this == o); }
    bool operator < (const BigInt &o) const {
		bool signT = sign(*this), signO = sign(o);
		if(signT != signO) return signT;
		for(int i = n - 1; i >= 0; i--)
			if(bit[i] != o[i]) return signT ? bit[i] > o[i] : bit[i] < o[i];
		return false;
	}
	bool operator > (const BigInt &o) const { return o < *this; }
	bool operator <= (const BigInt &o) const { return !(o < *this); }
	bool operator >= (const BigInt &o) const { return !(*this < o); }

	BigInt& operator += (const BigInt &o) {
		uint32_t carry = 0;
		for(size_t i = 0; i < n; i++) {
			uint64_t temp = static_cast<uint64_t>(bit[i]) + o[i] + carry;
			bit[i] = static_cast<uint32_t>(temp);
			carry = temp >> 32;
		}
		return *this;
	}
	BigInt operator + (const BigInt &o) const { BigInt res = *this; return res += o; }
	BigInt& operator -= (const BigInt &o) { *this += -o; return *this; }
	BigInt operator - (const BigInt &o) const { BigInt res = *this; return res += -o; }
    BigInt& operator ++ () { return *this += 1; }
	BigInt operator ++ (int) { BigInt res = *this; *this += 1; return res; }
	BigInt& operator -- () { return *this += -1; }
	BigInt operator -- (int) { BigInt res = *this; *this += -1; return res; }
	BigInt& operator *= (const BigInt &o) {
	    BigInt res;
	    for(size_t i = 0; i < n; i++) {
	        uint32_t carry = 0;
	        for(size_t j = 0; j + i < n; j++) {
	            uint64_t temp = static_cast<uint64_t>(bit[i]) * o[j] + res[i + j] + carry;
	            res[i + j] = static_cast<uint32_t>(temp);
	            carry = temp >> 32;
	        }
	    }
	    return *this = res;
	}
	BigInt operator * (const BigInt &o) const { BigInt res = *this; return res *= o; }
    BigInt& operator *= (const uint32_t &o) {
	    uint32_t carry = 0;
	    for(size_t i = 0; i < n; i++) {
	        uint64_t temp = static_cast<uint64_t>(bit[i]) * o + carry;
	        bit[i] = static_cast<uint32_t>(temp);
	        carry = temp >> 32;
	    }
	    return *this;
	}
	BigInt operator * (const uint32_t &o) const { BigInt res = *this; return res *= o; }
	BigInt& operator *= (int o) {
	    bool sgn = (o < 0); if(sgn) o = -o;
	    *this *= static_cast<uint32_t>(o);
	    if(sgn) *this = -(*this);
	    return *this;
	}
	BigInt operator * (const int &o) const { BigInt res = *this; return res *= o; }
    BigInt& operator /= (const BigInt &o) {
//		if(o == BigInt(0)) assert(false);
        bool sgn = sign(*this) ^ sign(o);
        if(sign(*this)) *this = -(*this);
        BigInt divisor = sign(o) ? -o : o;
        BigInt quotient, remainder;
        for(int i = (n << 5) - 1; i >= 0; --i) {
            remainder <<= 1;
            remainder[0] |= (bit[i >> 5] >> (i & 31)) & 1;
            if (remainder >= divisor) {
                remainder -= divisor;
                quotient[i >> 5] |= (1u << (i & 31));
            }
        }
        return *this = sgn ? -quotient : quotient;
    }
    BigInt operator / (const BigInt &o) const { BigInt res = *this; return res /= o; }
	BigInt& operator /= (const uint32_t &o) {
		bool sgn = sign(*this); if(sgn) *this = -(*this);
	    uint64_t carry = 0;
	    for(int i = n - 1; i >= 0; i--) {
	        uint64_t current = (carry << 32) | bit[i];
	        bit[i] = static_cast<uint32_t>(current / o);
	        carry = current % o;
	    }
	    if(sgn) *this = -(*this);
	    return *this;
	}
	BigInt operator / (const uint32_t &o) const { BigInt res = *this; return res /= o; }
	BigInt& operator /= (int o) {
	    bool sgn = (o < 0); if(sgn) o = -o;
	    *this /= static_cast<uint32_t>(o);
	    if(sgn) *this = -(*this);
	    return *this;
	}
	BigInt operator / (const int &o) const { BigInt res = *this; return res /= o; }
    BigInt& operator %= (const BigInt &o) {
//		if(o == BigInt(0)) assert(false);
        bool sgn = sign(*this);
        if(sign(*this)) *this = -(*this);
        BigInt divisor = sign(o) ? -o : o;
        BigInt quotient, remainder;
        for(int i = (n << 5) - 1; i >= 0; --i) {
            remainder <<= 1;
            remainder[0] |= (bit[i >> 5] >> (i & 31)) & 1;
            if (remainder >= divisor) remainder -= divisor;
        }
        return *this = sgn ? -remainder : remainder;
    }
    BigInt operator % (const BigInt &o) const { BigInt res = *this; return res %= o; }
	BigInt& operator %= (const uint32_t &o) {
		bool sgn = sign(*this); if(sgn) *this = -(*this);
	    uint32_t carry = 0;
	    for(int i = n - 1; i >= 0; i--) {
	        uint64_t current = (static_cast<uint64_t>(carry) << 32) | bit[i];
	        bit[i] = 0;
	        carry = current % o;
	    }
	    bit[0] = carry;
	    if(sgn) *this = -(*this);
	    return *this;
	}
	BigInt operator % (const uint32_t &o) const { BigInt res = *this; return res %= o; }
	BigInt& operator %= (int o) {
	    if (o < 0) o = -o;
	    *this %= static_cast<uint32_t>(o);
	    return *this;
	}
	BigInt operator % (const int &o) const { BigInt res = *this; return res %= o; }
};/*}}}*/
using mint = BigInt<9>;
mint operator*(int x, mint y) { return y * x; }
using LL = long long;
using FuncPtr = mint (*)(mint);
const FuncPtr F[] = {/*{{{*/
    +[](mint x) -> mint { return x; },
    +[](mint x) -> mint { return x * (x * x + 3 * x - 4) / 6; },
    +[](mint x) -> mint {
      return x *
             (5 * x * x * x * x * x * x + 35 * x * x * x * x * x +
              91 * x * x * x * x + 105 * x * x * x - 280 * x * x - 980 * x +
              1024) /
             1680;
    },
    +[](mint x) -> mint {
      return x *
             (429 * x * x * x * x * x * x * x * x * x * x * x * x * x * x +
              6435 * x * x * x * x * x * x * x * x * x * x * x * x * x +
              40425 * x * x * x * x * x * x * x * x * x * x * x * x +
              135135 * x * x * x * x * x * x * x * x * x * x * x +
              242151 * x * x * x * x * x * x * x * x * x * x +
              153153 * x * x * x * x * x * x * x * x * x -
              256685 * x * x * x * x * x * x * x * x -
              675675 * x * x * x * x * x * x * x -
              1606176 * x * x * x * x * x * x - 6846840 * x * x * x * x * x -
              16960944 * x * x * x * x - 19459440 * x * x * x +
              46382336 * x * x + 165065472 * x - 166219776) /
             276756480;
    },
}; /*}}}*/
const FuncPtr f[] = {/*{{{*/
    +[](mint x) -> mint { return 1; },
    +[](mint x) -> mint { return (x - 1) * (x + 2) / 2; },
    +[](mint x) -> mint {
      return (x - 1) * (x + 2) * (x * x + x - 4) * (x * x + x + 6) / 48;
    },
    +[](mint x) -> mint {
      return (x - 1) * (x + 2) * (x * x + x - 4) *
             (5 * x * x * x * x * x * x * x * x * x * x +
              25 * x * x * x * x * x * x * x * x * x +
              80 * x * x * x * x * x * x * x * x +
              170 * x * x * x * x * x * x * x + 289 * x * x * x * x * x * x +
              377 * x * x * x * x * x + 546 * x * x * x * x + 612 * x * x * x -
              3864 * x * x - 4128 * x - 26880) /
             215040;
    },
};/*}}}*/
constexpr int L = 1e6, N = L + 10;
mint q[N], preq[4][N];
int p[N];
mint P(mint n) {
  mint l = 0, r = mint(1) << (mint::n * 16 - 2), ans = 0;
  while (l <= r) {
    mint mid = (l + r) >> 1;
    if ((mid * (mid + 1) >> 1) <= n)
      ans = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  return ans;
}
mint ns[110];
mint g(int c, int d) {
  if (ns[c] <= L) return preq[d][(int)ns[c]];
  return F[d](ns[c]) * g(c + 1, 0) - g(c + 1, d + 1);
}
int main() {
#ifndef LOCAL
  cin.tie(nullptr)->sync_with_stdio(false);
#endif
  q[1] = 1, p[1] = 1;
  int lst = 1;
  for (int i = 2; i <= L; i++) {
    if ((lst + 1) * (lst + 2) / 2 == i) ++lst;
    p[i] = lst;
    q[i] = q[i - 1] + q[lst];
  }
  for (int j = 0; j < 4; j++) {
    for (int i = 1; i <= L; i++) {
      preq[j][i] = preq[j][i - 1] + f[j](i) * q[p[i]];
    }
  }
  int t;
  cin >> t;
  while (t--) {
    cin >> ns[0];
    for (int i = 1; i <= 4; i++) ns[i] = P(ns[i - 1]);
    cout << g(0, 0) << endl;
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2970ms
memory: 183352kb

input:

4
10
100
1000
987654321123456789

output:

30
2522
244274
235139898689017607381017686096176798

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 3486ms
memory: 183420kb

input:

10000
613939207402503646
408978283345333197
976677512231308716
629053280913850466
148712339
236220313279945487
590396556995977994
9226
215693877607285701
649702683896705
343173887453826567
847003949499596615
867133040287550291
159928123569892354
864534948175618654
209739383170746721
4295456752378791...

output:

91030728117067063595428375419346402
40469246710473908695676971059074376
229951682342450470520349294486964970
95558039501640054006660579352994194
5418340556433412
13536357243714243974772906966693552
84197207203086091733385317631619504
20934656
11291075621624104092841708040651034
104019777231815308683...

result:

ok 10000 lines

Test #3:

score: 0
Accepted
time: 3017ms
memory: 183392kb

input:

1000
6403632579734162001008235137760132245297
1307698664787972023762442022139627469666
668870338048562416595095770565441759482
5092270
8806864498496723812760785099973409980711
2178464116010775202899984038946879469187
204381824371
8638495456004772442511643693521120926431
45954412333082528168092594892...

output:

9822905445826021159291522774438593145331066315784567505849706373529921001845336
409552844852728078841401625660602682494169687360338453221088647649526339235330
107160056181509722327918304399871120167096186888511567354513496578559803370274
6354453295964
185817323129718525790559571287806776421589155460...

result:

ok 1000 lines

Test #4:

score: 0
Accepted
time: 3063ms
memory: 183644kb

input:

2000
2587627816607030340103003175959756184662
7728753705064569253253827797978613582938
6507847628603052973674714721
6989857636824717968431061258300652290539
4734281027640913533293237760425416005062
9411123735455625690768098631226366597446
8309753930995253536640660321476246470149
63565157427098067709...

output:

1603610451790269237852635641930301658193367441164307312552842461667027137613454
14309943493171496191506053530749878345271155973702143153225815632926701434642842
10414697803791950572309383355053080338229465674803757518
11704102206894264239190418551798088411458157423624785417336589284698838535371266
5...

result:

ok 2000 lines

Test #5:

score: 0
Accepted
time: 3224ms
memory: 183416kb

input:

5000
6701025283447923273597553918313900029215
1618190467906965189844764312914748628527
2135261797018456059451326428589353332126
8027429917075086154217136768450383650445
8263301530632040969183919589286944799002
376886964626613356031779878
1191561726595055348898524031899625958102
453561433135467095374...

output:

10756647971303093856509780939435968749671842310025383400207261624750784751725876
627115945498078452730193858129037650151913122482133071938783012929533045529940
1091921493223233296724616654299408127388059493196845968361252389122720100379070
154375707400033063668088306493199269136326791073281723548116...

result:

ok 5000 lines

Test #6:

score: 0
Accepted
time: 3542ms
memory: 183420kb

input:

10000
260865660295317278841
5232352637620496342310336202478387251106
7108789244285764135987032973912918380
12766535319519586095540974948550152061
5138306300154916887884637635208203681949
7603163140266839847784708214115317398585
149590294591155205497765668766786424787
63283557694500045989299147454323...

output:

16323111740957704392106241109891718054228
6557703685144914472554701877112177422100848067214049191882271294010013621817762
12143115079716078114619105501427985631361994195400195527663921137836417758
39139456824156526604158618001888125076786817219954316014947704612553450312
6324051018379978443719363340...

result:

ok 10000 lines