社区讨论
在此贴下回复如下代码,即可 NOIP RP++
灌水区参与者 4已保存回复 6
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 6 条
- 当前快照
- 1 份
- 快照标识符
- @m39kesmb
- 此快照首次捕获于
- 2024/11/09 10:47 去年
- 此快照最后确认于
- 2024/11/09 11:17 去年
别举报,这个我写了好几天。
回复即可,无需运行。代码:
CPP#define _BEGIN_TOOLS namespace Tools {
#define _END_TOOLS }
#define _DEC
#include <cmath>
#include <cstdio>
#include <vector>
#include <string>
#include <iostream>
#include <exception>
#include <stdexcept>
#include <algorithm>
_BEGIN_TOOLS
class BigInt //高精度类
{
public:
std::vector<int> num = { 0 };
bool fushu = false;
BigInt() { num.push_back(0); }
BigInt(const BigInt& x) : num(x.num), fushu(x.fushu) {}
BigInt(const std::string& x) { std::string kk = x; if (x[0] == '-') { fushu = true; kk = x.substr(1, x.size() - 1); } for (auto ch : kk) { num.push_back(ch - '0'); } }
BigInt(const long long& x) : BigInt(std::to_string(x)) {}
BigInt(const std::vector<int>& x) : num(x) { reverse(); num.push_back(0); reverse(); }
size_t size() const { return num.size() - 1; }
void reverse() { std::reverse(num.begin() + 1, num.end()); }
operator std::string() { std::string res; if (fushu) res = ' '; for (int i = 1; i <= (int)size(); i++) { res += char(num[i] + '0'); } return res; }
operator std::vector<int>() { return num; }
int& operator[](int id) { return num[id]; }
int operator[](int id) const { return num[id]; }
const BigInt& operator=(const BigInt& x) { num = x.num; fushu = x.fushu; return *this; }
const BigInt& operator=(const std::string& x) { *this = BigInt(x); return *this; }
const BigInt& operator=(const long long& x) { *this = BigInt(x); return *this; }
const BigInt& operator=(const std::vector<int>& x) { *this = BigInt(x); return *this; }
friend const BigInt& operator+=(BigInt& x, const BigInt& y) { x = x + y; return x; }
friend const BigInt& operator-=(BigInt& x, const BigInt& y) { x = x - y; return x; }
friend const BigInt& operator*=(BigInt& x, const BigInt& y) { x = x * y; return x; }
friend const BigInt& operator/=(BigInt& x, const BigInt& y) { x = x / y; return x; }
friend const BigInt& operator%=(BigInt& x, const BigInt& y) { x = x % y; return x; }
friend const BigInt& operator+=(BigInt& x, int y) { x += BigInt(y); return x; }
friend const BigInt& operator-=(BigInt& x, int y) { x -= BigInt(y); return x; }
friend const BigInt& operator*=(BigInt& x, int y) { x *= BigInt(y); return x; }
friend const BigInt& operator/=(BigInt& x, int y) { x /= BigInt(y); return x; }
friend const BigInt& operator%=(BigInt& x, int y) { x %= BigInt(y); return x; }
friend std::istream& operator>>(std::istream& is, BigInt& u) { std::string s; is >> s; u = s; return is; }
friend std::ostream& operator<<(std::ostream& os, const BigInt& u) { if (u.fushu) os << '-'; for (int i = 1; i <= (int)u.size(); i++) { os << u[i]; } if (u.size() == 0) os << '0'; return os; };
friend BigInt operator+(BigInt, BigInt);
friend BigInt operator-(BigInt, BigInt);
friend BigInt operator*(BigInt, BigInt);
friend BigInt operator/(BigInt, BigInt);
friend BigInt operator%(BigInt x, BigInt y) { return x - x / y * y; }
friend BigInt operator+(BigInt x, int y) { return x + BigInt(y); }
friend BigInt operator-(BigInt x, int y) { return x - BigInt(y); }
friend BigInt operator*(BigInt x, int y) { return x * BigInt(y); }
friend BigInt operator/(BigInt x, int y) { return x / BigInt(y); }
friend BigInt operator%(BigInt x, int y) { return x % BigInt(y); }
friend BigInt operator-(BigInt x) { BigInt y = x; y.fushu ^= 1; return y; }
};
bool operator< (const BigInt& x, const BigInt& y);
bool operator==(const BigInt& x, const BigInt& y);
bool operator<=(const BigInt& x, const BigInt& y);
bool operator!=(const BigInt& x, const BigInt& y);
bool operator>=(const BigInt& x, const BigInt& y);
bool operator> (const BigInt& x, const BigInt& y);
bool operator< (const BigInt& x, const int& y);
bool operator<=(const BigInt& x, const int& y);
bool operator==(const BigInt& x, const int& y);
bool operator!=(const BigInt& x, const int& y);
bool operator>=(const BigInt& x, const int& y);
bool operator> (const BigInt& x, const int& y);
_END_TOOLS
namespace std
{
Tools::BigInt abs(const Tools::BigInt& x);
}
using namespace std;
_BEGIN_TOOLS
bool operator==(const BigInt& x, const BigInt& y) { return x.num == y.num; };
bool operator<=(const BigInt& x, const BigInt& y) { return x < y || x == y; };
bool operator!=(const BigInt& x, const BigInt& y) { return !(x == y); };
bool operator>=(const BigInt& x, const BigInt& y) { return !(x < y); };
bool operator> (const BigInt& x, const BigInt& y) { return !(x <= y); };
bool operator< (const BigInt& x, const int& y) { return x < BigInt(y); };
bool operator<=(const BigInt& x, const int& y) { return x < y || x == BigInt(y); };
bool operator==(const BigInt& x, const int& y) { return x == BigInt(y); };
bool operator!=(const BigInt& x, const int& y) { return !(x == y); };
bool operator>=(const BigInt& x, const int& y) { return !(x < y); };
bool operator> (const BigInt& x, const int& y) { return !(x <= y); };
Tools::BigInt operator+(Tools::BigInt x, Tools::BigInt y)
{
x.reverse();
y.reverse();
while (x.size() < y.size())
{
x.num.push_back(0);
}
while (y.size() < x.size())
{
y.num.push_back(0);
}
x.reverse();
y.reverse();
int jw = 0;
for (int i = (int)x.size(); i >= 1; i--)
{
x[i] = x[i] + y[i] + jw;
if (x[i] >= 10)
{
jw = 1;
x[i] -= 10;
}
else jw = 0;
}
if (jw)
{
x.reverse();
x.num.push_back(1);
x.reverse();
}
return x;
}
Tools::BigInt operator-(Tools::BigInt x, Tools::BigInt y)
{
if (x < y) return -(y - x);
if (x == y) return 0;
x.reverse();
y.reverse();
while (x.size() < y.size())
{
x.num.push_back(0);
}
while (y.size() < x.size())
{
y.num.push_back(0);
}
x.reverse();
y.reverse();
int jw = 0;
for (int i = (int)x.size(); i >= 1; i--)
{
x[i] = x[i] - y[i] + jw;
if (x[i] < 0)
{
jw = -1;
x[i] += 10;
}
else jw = 0;
}
x.reverse();
while (x.num.back() == 0 && x.size() >= 1)
x.num.pop_back();
// x.num.push_back(0);
x.reverse();
return x;
}
Tools::BigInt operator*(Tools::BigInt x, Tools::BigInt y)
{
//printf("operator* called.\n");
//cout << x << "*" << y << endl;
if (x == 0 || y == 0) return 0;
Tools::BigInt ans = string(x.size() + y.size(), '0');
for (int i = 1; i <= (int)x.size(); i++)
{
for (int j = 1; j <= (int)y.size(); j++)
{
ans[i + j - 1] += x[i] * y[j];
}
}
int jw = 0;
for (int i = (int)ans.size(); i >= 1; i--)
{
ans[i] += jw;
jw = ans[i] / 10;
ans[i] %= 10;
}
ans.reverse();
do
{
ans.num.push_back(jw % 10);
jw /= 10;
} while (jw);
ans.reverse();
ans.num.pop_back();
ans.reverse();
while (ans.num.back() == 0) ans.num.pop_back();
ans.reverse();
return ans;
}
Tools::BigInt operator/(Tools::BigInt x, Tools::BigInt y)
{
if (x < y) return 0;
if (x == y) return 1;
Tools::BigInt ans = { 0 };
int ind = 0;
Tools::BigInt u = string(y) + string(x.size() - y.size() - ind, '0');
while (u >= y)
{
int m = 0;
while (x >= u)
{
x -= u;
m++;
}
ans.num.push_back(m);
u.num.pop_back();
}
ans.reverse();
while (ans.num.back() == 0) ans.num.pop_back();
// ans.num.push_back(0);
ans.reverse();
return ans;
}
bool operator< (const Tools::BigInt& x, const Tools::BigInt& y)
{
if (x.size() != y.size()) return x.size() < y.size();
for (int i = 1; i <= (int)x.size(); i++)
{
if (x[i] != y[i]) return x[i] < y[i];
}
return false;
}
_END_TOOLS
namespace std
{
Tools::BigInt abs(const Tools::BigInt& x) { return x.num; }
}
_BEGIN_TOOLS
template<typename T = long long>
__declspec(dllexport)
inline T gcd(T x, T y)
{
return (y != 0) ? gcd(y, x % y) : std::abs(x);
}
template<typename T = long long>
class fract
{
protected:
T fz = 0;
T fm = 1;
static inline T getgcd(T x, T y) { return gcd(x, y) * (y > 0 ? 1 : -1); }
public:
fract() noexcept : fz(0), fm(1) {};
fract(const fract& x) noexcept : fz(x.fz), fm(x.fm) {};
fract(fract&& x) noexcept : fz(x.fz), fm(x.fm) {};
fract(T x) : fz(x) {};
//x/y
fract(T x, T y) { if (y == 0) throw std::range_error("The denominator of a fraction mustn't be zero!!\n");
T g = getgcd(x, y); fz = x / g; fm = y / g; };
friend fract operator+(const fract& x, const fract& y) { return { x.fz * y.fm + x.fm * y.fz, x.fm * y.fm }; }
friend fract operator-(const fract& x, const fract& y) { return { x.fz * y.fm - x.fm * y.fz, x.fm * y.fm }; }
friend fract operator*(const fract& x, const fract& y) { return { x.fz * y.fz, x.fm * y.fm }; }
friend fract operator/(const fract& x, const fract& y) { return { x.fz * y.fm, x.fm * y.fz }; }
friend fract operator*(const fract& x, const T& y) { return { x.fz * y, x.fm }; }
friend fract operator/(const fract& x, const T& y) { return { x.fz, x.fm * y }; }
fract& operator+=(const fract& x) { return *this = *this + x; }
fract& operator-=(const fract& x) { return *this = *this - x; }
fract& operator*=(const fract& x) { return *this = *this * x; }
fract& operator/=(const fract& x) { return *this = *this / x; }
friend fract operator-(const fract& x) { return { -x.fz, x.fm }; } // @一扶苏一
fract& operator=(const fract& x) noexcept { fz = x.fz; fm = x.fm; return *this; }
fract& operator=(fract&& x) noexcept { fz = x.fz; fm = x.fm; return *this; }
fract operator=(const T& x) noexcept { fz = x; fm = 1; return *this; }
T& getfz() { return fz; }
T& getfm() { return fm; }
const T getfz() const { return fz; }
const T getfm() const { return fm; }
friend ostream& operator<<(ostream& os, const fract& x) { os << x.fz; if (x.fm != 1) os << "/" << x.fm; return os; }
};
using bigfrac = fract<BigInt>;
_END_TOOLS
_BEGIN_TOOLS
template<typename _Input, typename _Output>
class mathfunc
{
public:
virtual _Output operator()(_Input x) const = 0;
};
_END_TOOLS
_BEGIN_TOOLS
template<typename T>
class expre : public mathfunc<const T&, T>
{
public:
std::vector<T> as;
size_t size() const { return as.size(); }
size_t deg() const { return size() - 1; }
T& getas(size_t id) { return as[id]; }
T& getas_s(size_t id) { return as.at(id); }
T getas_s0(size_t id) const { return id >= size() ? 0 : as[id]; }
const T& getas(size_t id) const { return as[id]; }
const T& getas_s(size_t id) const { return as.at(id); }
const T& operator[](size_t id) const
{
#if defined(_DEBUG) || defined(DEBUG)
return getas_s(id);
#else
return getas(id);
#endif
}
T& operator[](size_t id)
{
#if defined(_DEBUG) || defined(DEBUG)
return getas_s(id); // @mrsrz
#else
return getas(id);
#endif
}
template<typename Q> friend expre<Q> operator+(const expre<Q>& x, const expre<Q>& y);
template<typename Q> friend expre<Q> operator-(const expre<Q>& x, const expre<Q>& y);
template<typename Q> friend expre<Q> operator-(const expre<Q>& x);
template<typename Q> friend expre<Q> operator*(const expre<Q>& x, const expre<Q>& y);
template<typename Q> friend expre<Q> operator/(const expre<Q>& x, const expre<Q>& y) = delete; // 懒得写了qwq
T operator()(const T& x) const override;
};
template<typename T>
expre<T> der(const expre<T>& x); // 求导
template<typename T>
expre<T> pri(const expre<T>& x); // 原函数,默认最后一项的 C 为“当 C = 0 时,原函数的值的相反数”
using bigexpr = expre<BigInt>;
using bigfracexpr = expre<bigfrac>;
_END_TOOLS
using namespace std;
_BEGIN_TOOLS
template<typename T>
expre<T> der(const expre<T>& x)
{
expre<T> answer;
answer.as.resize(x.size() - 1);
for (int i = 0; i < answer.size(); i++)
{
answer[i] = x[i + 1] * (i + 1);
}
return answer;
}
template<typename T>
expre<T> pri(const expre<T>& x)
{
expre<T> answer;
answer.as.resize(x.size() + 1);
for (int i = 1; i < answer.size(); i++)
{
answer[i] = x[i - 1] / i;
}
answer[0] = //@chen_zhe -(answer());
114514;
return answer;
}
template<typename Q> expre<Q> operator+(const expre<Q>& x, const expre<Q>& y)
{
expre<Q> answer;
answer.as.resize(max(x.size(), y.size()));
for (int i = 0; i < answer.size(); i++)
{
answer[i] = x[i] + y[i];
}
return answer;
}
template<typename Q> expre<Q> operator-(const expre<Q>& x, const expre<Q>& y)
{
return x + (-y);
}
template<typename Q> expre<Q> operator-(const expre<Q>& x)
{
expre<Q> answer;
answer.as.resize(x.size());
for (int i = 0; i < x.size(); i++)
{ // @kkksc03
answer[i] = -x[i];
}
return answer;
}
template<typename Q> expre<Q> operator*(const expre<Q>& x, const expre<Q>& y)
{
expre<Q> answer;
answer.as.resize(x.deg() + y.deg() + 1);
for (int i = 0; i < x.size(); i++)
{
for (int j = 0; j < y.size(); j++)
{
answer[i + j] += x[i] * y[j];
}
}
return answer;
}
// template<typename Q> expre<Q> operator/(const expre<Q>& x, const expre<Q>& y)
// {
// 不会写 + 不想写 == 不写了,写什么写
// @LionBlaze 我佩服你的出题效率。
// }
template<typename T> T expre<T>::operator()(const T& x) const
{
T answer = T(0);
T m = T(1);
for (int i = 0; i < as.size(); i++)
{
answer = answer + as[i] * m;
m = m * x;
}
return answer;
}
_END_TOOLS
// TODO : 写这两个函数
using namespace std;
using namespace Tools;
BigInt fractions[1005];
int main()
{
fractions[0] = 1;
for (int i = 1; i <= 1000; i++)
{
fractions[i] = fractions[i - 1] * i;
}
int n, k;
//scanf("%d%d", &n, &k);
n = 10000; k = 5;
bigfracexpr a;
bigfracexpr b;
for (int i = 0; i <= n; i++)
{
long long fffff;
// scanf("%lld", &fffff);
fffff = 1000000000000000000;
a.as.push_back({ fffff, 1 });
b.as.push_back({ fffff, 1 });
}
for (int i = 1; i <= k; i++)
{
a = der(a);
putchar(" NI\nP+"[i]);
fflush(stdout);
b = pri(b);
putchar(" OPR+!"[i]);
fflush(stdout);
}
// for (const auto & v : a.as)
// {
// cout << v << " ";
// }
// printf("\n");
// for (int i = 0; i < k; i++)
// {
// printf("C%d ", i);
// }
// for (int i = k; i < b.as.size(); i++)
// {
// cout << b[i] << " ";
// }
return 0;
}
回复
共 6 条回复,欢迎继续交流。
正在加载回复...