读入 并查集 gcd/exgcd 高精度 快速幂
ios_base::sync_with_stdio();
cin.tie();
int to[MAXM << ], nxt[MAXM << ], Head[MAXN], ed = ;
inline void addedge(int u, int v)
{
to[++ed] = v;
nxt[ed] = Head[u];
Head[u] = ed;
}
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
//睿智OJ头文件
CLOCK
printf("Time: %.3lfs\n", (double)clock() / CLOCKS_PER_SEC);
枚举子集
for (int sub = (j - )&j; sub; sub = (sub - )&j)
Huyyt
/*Huyyt*/
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mod = 1e9 + ;
const int gakki = + + + + 1e9;
const int MAXN = 2e5 + ;
const int MAXM = 2e5 + ;
int main()
{
ios_base::sync_with_stdio();
cin.tie();
return ;
}
读入挂
template <typename T> inline void read(T&x)
{
char cu = getchar();
x = ;
bool fla = ;
while (!isdigit(cu))
{
if (cu == '-')
{
fla = ;
}
cu = getchar();
}
while (isdigit(cu))
{
x = x * + cu - '', cu = getchar();
}
if (fla)
{
x = -x;
}
}
inline void read(int &v)
{
v = ;
char c = ;
int p = ;
while (c < '' || c > '')
{
if (c == '-')
{
p = -;
}
c = getchar();
}
while (c >= '' && c <= '')
{
v = (v << ) + (v << ) + c - '';
c = getchar();
}
v *= p;
}
输出挂
struct Quick_Out {
char buf[BUFFER_MAX_SIZE], *ps = buf, *pe = buf + BUFFER_MAX_SIZE;
char tmp[];
double dx[] = {5e-,5e-,5e-,5e-,5e-,5e-,5e-,5e-,5e-,5e-,5e-,5e-,5e-,5e-,5e-};
inline void write() {
fwrite(buf, sizeof(char), ps - buf, stdout);
ps = buf;
}
inline void oc(char c) {
*(ps++) = c;
if (ps == pe) write();
}
inline void os(char *s) {
for (int i = ; s[i]; ++i) oc(s[i]);
}
template<class T>
inline void oi(T x, char bc = '\n') {
if (x < ) oc('-'), x = -x;
int len = ;
if (!x) tmp[len++] = '';
while (x) tmp[len++] = x % + '', x /= ;
while (len) oc(tmp[--len]);
oc(bc);
}
~Quick_Out() {
write();
}
} Out;
并查集
int par[maxn], hs[maxn],int sz[maxn];
int cnt;
void init(int n)
{
for (int i = ; i <= n; i++)
{
par[i] = i, hs[i] = i, sz[i] = ;
}
}
int find(int x)
{
return par[x] == x ? x : par[x] = find(par[x]);
}
void unite(int x, int y)
{
x = find(x);
y = find(y);
if (x != y)
{
par[x] = y, sz[y] += sz[x];
}
}
void del(int x)
{
sz[find(hs[x])]--;
hs[x] = ++cnt;
}
高精度
#include <bits/stdc++.h>
#define PI acos(-1.0)
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define inf 1e9
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que; get min
const double eps = 1.0e-8;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 3e7 + ;
const int maxm = ;
const int turn[][] = {{, }, { -, }, {, }, {, -}};
//priority_queue<int, vector<int>, less<int>> que;
//next_permutation
ll mod = 1e6;
using namespace std;
const int base = ;
const int base_digits = ;
struct bigint
{
vector<int> a;
int sign;
int size()
{
if (a.empty())
{
return ;
}
int ans = (a.size() - ) * base_digits;
int ca = a.back();
while (ca)
{
ans++, ca /= ;
}
return ans;
}
bigint operator ^(const bigint &v)
{
bigint ans = , a = *this, b = v;
while (!b.isZero())
{
if (b % )
{
ans *= a;
}
a *= a, b /= ;
}
return ans;
}
bigint() :
sign()
{
} bigint(long long v)
{
*this = v;
} bigint(const string &s)
{
read(s);
} void operator=(const bigint &v)
{
sign = v.sign;
a = v.a;
} void operator=(long long v)
{
sign = ;
if (v < )
{
sign = -, v = -v;
}
for (; v > ; v = v / base)
{
a.push_back(v % base);
}
} bigint operator+(const bigint &v) const
{
if (sign == v.sign)
{
bigint res = v; for (int i = , carry = ; i < (int) max(a.size(), v.a.size()) || carry; ++i)
{
if (i == (int) res.a.size())
{
res.a.push_back();
}
res.a[i] += carry + (i < (int) a.size() ? a[i] : );
carry = res.a[i] >= base;
if (carry)
{
res.a[i] -= base;
}
}
return res;
}
return *this - (-v);
} bigint operator-(const bigint &v) const
{
if (sign == v.sign)
{
if (abs() >= v.abs())
{
bigint res = *this;
for (int i = , carry = ; i < (int) v.a.size() || carry; ++i)
{
res.a[i] -= carry + (i < (int) v.a.size() ? v.a[i] : );
carry = res.a[i] < ;
if (carry)
{
res.a[i] += base;
}
}
res.trim();
return res;
}
return -(v - *this);
}
return *this + (-v);
} void operator*=(int v)
{
if (v < )
{
sign = -sign, v = -v;
}
for (int i = , carry = ; i < (int) a.size() || carry; ++i)
{
if (i == (int) a.size())
{
a.push_back();
}
long long cur = a[i] * (long long) v + carry;
carry = (int) (cur / base);
a[i] = (int) (cur % base);
}
trim();
} bigint operator*(int v) const
{
bigint res = *this;
res *= v;
return res;
} friend pair<bigint, bigint> divmod(const bigint &a1, const bigint &b1)
{
int norm = base / (b1.a.back() + );
bigint a = a1.abs() * norm;
bigint b = b1.abs() * norm;
bigint q, r;
q.a.resize(a.a.size()); for (int i = a.a.size() - ; i >= ; i--)
{
r *= base;
r += a.a[i];
int s1 = r.a.size() <= b.a.size() ? : r.a[b.a.size()];
int s2 = r.a.size() <= b.a.size() - ? : r.a[b.a.size() - ];
int d = ((long long) base * s1 + s2) / b.a.back();
r -= b * d;
while (r < )
{
r += b, --d;
}
q.a[i] = d;
} q.sign = a1.sign * b1.sign;
r.sign = a1.sign;
q.trim();
r.trim();
return make_pair(q, r / norm);
} bigint operator/(const bigint &v) const
{
return divmod(*this, v).first;
} bigint operator%(const bigint &v) const
{
return divmod(*this, v).second;
} void operator/=(int v)
{
if (v < )
{
sign = -sign, v = -v;
}
for (int i = (int) a.size() - , rem = ; i >= ; --i)
{
long long cur = a[i] + rem * (long long) base;
a[i] = (int) (cur / v);
rem = (int) (cur % v);
}
trim();
} bigint operator/(int v) const
{
bigint res = *this;
res /= v;
return res;
} int operator%(int v) const
{
if (v < )
{
v = -v;
}
int m = ;
for (int i = a.size() - ; i >= ; --i)
{
m = (a[i] + m * (long long) base) % v;
}
return m * sign;
} void operator+=(const bigint &v)
{
*this = *this + v;
}
void operator-=(const bigint &v)
{
*this = *this - v;
}
void operator*=(const bigint &v)
{
*this = *this * v;
}
void operator/=(const bigint &v)
{
*this = *this / v;
} bool operator<(const bigint &v) const
{
if (sign != v.sign)
{
return sign < v.sign;
}
if (a.size() != v.a.size())
{
return a.size() * sign < v.a.size() * v.sign;
}
for (int i = a.size() - ; i >= ; i--)
if (a[i] != v.a[i])
{
return a[i] * sign < v.a[i] * sign;
}
return false;
} bool operator>(const bigint &v) const
{
return v < *this;
}
bool operator<=(const bigint &v) const
{
return !(v < *this);
}
bool operator>=(const bigint &v) const
{
return !(*this < v);
}
bool operator==(const bigint &v) const
{
return !(*this < v) && !(v < *this);
}
bool operator!=(const bigint &v) const
{
return *this < v || v < *this;
} void trim()
{
while (!a.empty() && !a.back())
{
a.pop_back();
}
if (a.empty())
{
sign = ;
}
} bool isZero() const
{
return a.empty() || (a.size() == && !a[]);
} bigint operator-() const
{
bigint res = *this;
res.sign = -sign;
return res;
} bigint abs() const
{
bigint res = *this;
res.sign *= res.sign;
return res;
} long long longValue() const
{
long long res = ;
for (int i = a.size() - ; i >= ; i--)
{
res = res * base + a[i];
}
return res * sign;
} friend bigint gcd(const bigint &a, const bigint &b)
{
return b.isZero() ? a : gcd(b, a % b);
}
friend bigint lcm(const bigint &a, const bigint &b)
{
return a / gcd(a, b) * b;
} void read(const string &s)
{
sign = ;
a.clear();
int pos = ;
while (pos < (int) s.size() && (s[pos] == '-' || s[pos] == '+'))
{
if (s[pos] == '-')
{
sign = -sign;
}
++pos;
}
for (int i = s.size() - ; i >= pos; i -= base_digits)
{
int x = ;
for (int j = max(pos, i - base_digits + ); j <= i; j++)
{
x = x * + s[j] - '';
}
a.push_back(x);
}
trim();
} friend istream& operator>>(istream &stream, bigint &v)
{
string s;
stream >> s;
v.read(s);
return stream;
} friend ostream& operator<<(ostream &stream, const bigint &v)
{
if (v.sign == -)
{
stream << '-';
}
stream << (v.a.empty() ? : v.a.back());
for (int i = (int) v.a.size() - ; i >= ; --i)
{
stream << setw(base_digits) << setfill('') << v.a[i];
}
return stream;
} static vector<int> convert_base(const vector<int> &a, int old_digits, int new_digits)
{
vector<long long> p(max(old_digits, new_digits) + );
p[] = ;
for (int i = ; i < (int) p.size(); i++)
{
p[i] = p[i - ] * ;
}
vector<int> res;
long long cur = ;
int cur_digits = ;
for (int i = ; i < (int) a.size(); i++)
{
cur += a[i] * p[cur_digits];
cur_digits += old_digits;
while (cur_digits >= new_digits)
{
res.push_back(int(cur % p[new_digits]));
cur /= p[new_digits];
cur_digits -= new_digits;
}
}
res.push_back((int) cur);
while (!res.empty() && !res.back())
{
res.pop_back();
}
return res;
} typedef vector<long long> vll; static vll karatsubaMultiply(const vll &a, const vll &b)
{
int n = a.size();
vll res(n + n);
if (n <= )
{
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
{
res[i + j] += a[i] * b[j];
}
return res;
} int k = n >> ;
vll a1(a.begin(), a.begin() + k);
vll a2(a.begin() + k, a.end());
vll b1(b.begin(), b.begin() + k);
vll b2(b.begin() + k, b.end()); vll a1b1 = karatsubaMultiply(a1, b1);
vll a2b2 = karatsubaMultiply(a2, b2); for (int i = ; i < k; i++)
{
a2[i] += a1[i];
}
for (int i = ; i < k; i++)
{
b2[i] += b1[i];
} vll r = karatsubaMultiply(a2, b2);
for (int i = ; i < (int) a1b1.size(); i++)
{
r[i] -= a1b1[i];
}
for (int i = ; i < (int) a2b2.size(); i++)
{
r[i] -= a2b2[i];
} for (int i = ; i < (int) r.size(); i++)
{
res[i + k] += r[i];
}
for (int i = ; i < (int) a1b1.size(); i++)
{
res[i] += a1b1[i];
}
for (int i = ; i < (int) a2b2.size(); i++)
{
res[i + n] += a2b2[i];
}
return res;
} bigint operator*(const bigint &v) const
{
vector<int> a6 = convert_base(this->a, base_digits, );
vector<int> b6 = convert_base(v.a, base_digits, );
vll a(a6.begin(), a6.end());
vll b(b6.begin(), b6.end());
while (a.size() < b.size())
{
a.push_back();
}
while (b.size() < a.size())
{
b.push_back();
}
while (a.size() & (a.size() - ))
{
a.push_back(), b.push_back();
}
vll c = karatsubaMultiply(a, b);
bigint res;
res.sign = sign * v.sign;
for (int i = , carry = ; i < (int) c.size(); i++)
{
long long cur = c[i] + carry;
res.a.push_back((int) (cur % ));
carry = (int) (cur / );
}
res.a = convert_base(res.a, , base_digits);
res.trim();
return res;
}
}; int main()
{
bigint anser;
int n;
cin >> n;
bigint N(n);
if (n & )
{
anser = (N - bigint()) * (N - bigint()) * (bigint() * N * N * N * N - bigint() * N * N * N + bigint() * N * N - bigint() * N + bigint()) / bigint();
}
else
{
anser = N * (N - bigint()) * (N - bigint()) * (bigint() * N * N * N - bigint() * N * N + bigint() * N - bigint()) / bigint();
}
cout << anser << endl;
return ;
}
GCD
ll gcd(ll a, ll b){
ll t;
while(b){
t = b;
b = a % b;
a = t;
}
return a;
}
EXGCD
ll exgcd(ll a,ll b,ll &x,ll &y){
if (!b){
x=,y=;
return a;
}
ll res=exgcd(b,a%b,y,x);
y-=(a/b)*x;
return res;
}
快速幂
ll Qpow(ll a, ll b)
{
ll ans = , base = a;
while (b != )
{
if (b & != )
{
ans *= base;
ans %= mod;
}
base *= base;
base %= mod;
b >>= 1LL;
}
return ans;
}
组合数
ll ncr(ll n, ll r)
{
if (r < || n < )
{
return ;
}
if (n < r)
{
return ;
}
ll a = fact[n];
a = (a * Invfact[r]) % mod;
a = (a * Invfact[n - r]) % mod;
return a;
}
线性逆元
inv[i] = (p - p / i) * inv[p % i] % p;
方向
int dir[][] = { , -, -, , , , , };
__Int128
inline __int128 read(){
__int128 x=,f=;
char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')
f=-;
ch=getchar();
}
while(ch>=''&&ch<=''){
x=x*+ch-'';
ch=getchar();
}
return x*f;
} inline void print(__int128 x){
if(x<){
putchar('-');
x=-x;
}
if(x>)
print(x/);
putchar(x%+'');
} int main(void){
__int128 a = read();
__int128 b = read();
print(a + b);
cout<<endl;
return ;
}
Hash
int hs = ;
for (int i = ; i <= ; i++)
{
hs = (233LL * hs % mod + s[i]) % mod;
}
读入 并查集 gcd/exgcd 高精度 快速幂的更多相关文章
- hdu 2462(欧拉定理+高精度快速幂模)
The Luckiest number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdu 5187(高精度快速幂)
zhx's contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- 洛谷试炼场-简单数学问题-P1045 麦森数-高精度快速幂
洛谷试炼场-简单数学问题 B--P1045 麦森数 Description 形如2^P−1的素数称为麦森数,这时P一定也是个素数.但反过来不一定,即如果PP是个素数,2^P-1 不一定也是素数.到19 ...
- Qbxt 模拟赛 Day4 T2 gcd(矩阵乘法快速幂)
/* 矩阵乘法+快速幂. 一开始迷之题意.. 这个gcd有个规律. a b b c=a*x+b(x为常数). 然后要使b+c最小的话. 那x就等于1咯. 那么问题转化为求 a b b a+b 就是斐波 ...
- GCD&&素筛&&快速幂 --A - Pseudoprime numbers
Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a (mod p). Th ...
- hzau 1202 GCD(矩阵快速幂)
1202: GCD Time Limit: 1 Sec Memory Limit: 1280 MBSubmit: 201 Solved: 31[Submit][Status][Web Board] ...
- HDU 4704 Sum (高精度+快速幂+费马小定理+二项式定理)
Sum Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u Submit Status ...
- a^b-b^a - SGU 112(高精度快速幂)
分析:直接上吧,建议不要使用模板,否则没啥意义了. 代码如下: ==================================================================== ...
- 高精度快速幂(Java版)
import java.io.*; import java.math.*; import java.util.*; import java.text.*; public class Main { pu ...
随机推荐
- selenium IDE下载安装(For Chrome and firefox)
安装好Firefox/cheome之后,接下来就到了正式安装Selenuim IDE的时候了. 步骤一:下载Selenuim IDE 方法一:之前从网上查到很多安装教程,都是从http:/ ...
- 配置MySQL,使其与PyCharm相关联
在配置MySQL和PyCharm时,经常出现这样的错误提示: Connection to base@localhost failed. [08001] Could not create connect ...
- Mac下安装VirtualBox并在VirtualBox中安装CentOS7
VirtualBox (百科)VirtualBox 是一款开源虚拟机软件.VirtualBox 是由德国 Innotek 公司开发,由Sun Microsystems公司Sun Microsystem ...
- Java中的IO流之输入流|乐字节
亲爱的乐字节的小伙伴们,小乐又来分享Java技术文章了.上一篇写到了IO流,这篇文章着重 谈谈输入流,再下次再说输出流. 点击回顾上一篇:乐字节Java之file.IO流基础知识和操作步骤 一. 输入 ...
- Linux(Ubuntu)下的OpenGl的环境安装, 在qt程序中使用opengl库
OpenGl的环境安装 以下参考自:https://blog.csdn.net/wasaiheihei/article/details/52085397 1. 建立基本编译环境 首先不可或缺的,就是编 ...
- 谈谈redis的热key问题如何解决
引言 讲了几天的数据库系列的文章,大家一定看烦了,其实还没讲完...(以下省略一万字).今天我们换换口味,来写redis方面的内容,谈谈热key问题如何解决.其实热key问题说来也很简单,就是瞬间有几 ...
- [转帖]MySQL语句大全
MySQL语句大全 https://www.cnblogs.com/jicki/p/5548676.html 一.连接mysql. 格式: mysql -h主机地址 -u用户名 -p用户密码 二.修改 ...
- (一)Shiro,久闻其名,而今初相识
文章目录 shiro简介 功能介绍 从外部看 Shiro 架构 从内部看 Shiro 架构 多说一句,在学习shiro之前,我觉得应该先用 filter ,自己动手写过粗粒度的权限系统,而不要一上来就 ...
- C++ 与 MATLAB 混合编程总结(14)
1. 前言 因为毕业设计的需求,研究了一下,C++如何与MATLAB一起混合编程,中间走了一些弯路,这里总结一下. 我用的主要是C++如何调用MATLAB,而没有涉及MATLAB如何调用C++. 注意 ...
- a+b的问题
题目描述: 给定两个整数 a, b (a, b 均不超过 int 类型的表示范围),求出 a + b 的和. 输入描述: 每行输入两个整数 a 和 b,用空格隔开. 输出描述: a + b 的值. 样 ...