NOI算法的应用
Beta_Circuit · · 算法·理论
先看这一题: 【入门】素数的个数 时间限制:C/C++ 1000MS 内存限制:C/C++ 16MB 编程求正整数M与N之间的所有素数的个数.(M<=N) 输入描述 输入只有一行,包括2个整数M,N,之间用一个空格分开。 输出描述 输出只有一行(这意味着末尾有一个回车符号),包括1个整数。 显然这题暴力会超时,这题又不带修,考虑莫队。 在扩大或缩小区间时,若增加/减少的那个数为素数,则now++/--。 贴代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for (register int i = (a); i <= (b); ++i)
#define per(i, a, b) for (register int i = (a); i >= (b); --i)
const int maxn = 1e6 + 5;
struct node {
int l, r, id;
} q[maxn];
int m, s, l = 1, r = 0, now = 0;
int cnt[maxn], ans[maxn];
int ksm(int a, int b, int p) {
int res = 1;
while (b) {
if (b & 1) res = res * a % p;
a = a * a % p;
b >>= 1;
}
return res;
}
bool Miller_Rabin(int a, int p) {
if (a % p == 0) return true;
int d = p - 1;
while (!(d & 1)) d >>= 1;
int t = ksm(a, d, p);
if (t == 1 || t == p - 1) return true;
while (d < p - 1) {
d <<= 1;
t = ksm(t, 2, p);
if (t == p - 1) return true;
}
return false;
}
bool isprime(int p) {
if (p <= 1) return false;
if (p <= 3) return true;
if (p % 2 == 0) return false;
return Miller_Rabin(2, p) && Miller_Rabin(3, p) && Miller_Rabin(5, p) && Miller_Rabin(7, p) && Miller_Rabin(11, p);
}
bool cmp(node q1, node q2) {
if (q1.l / s == q2.l / s)
return q1.r < q2.r;
return q1.l < q2.l;
}
void add(int p) {
cnt[p]++;
if (cnt[p] == 1 && isprime(p)) now++;
}
void del(int p) {
cnt[p]--;
if (cnt[p] == 0 && isprime(p)) now--;
}
signed main() {
m = 1;
cin >> q[1].l >> q[1].r;
q[1].id = 1;
s = sqrt(q[1].r) + 1;
sort(q + 1, q + 1 + m, cmp);
rep(i, 1, m) {
int nowl = q[i].l, nowr = q[i].r;
while (l > nowl) add(--l);
while (r < nowr) add(++r);
while (l < nowl) del(l++);
while (r > nowr) del(r--);
ans[q[i].id] = now;
}
cout << ans[1] << endl;
return 0;
}
使用这个算法成功拿到了最劣解,实在是太迅速了! 再看一道题。 描述
根据输入的温度将摄氏度转换为华氏度
摄氏度转华氏度公式为F=C×1.8+32
输入描述
摄氏温度(整数,温度在-100摄氏度~100摄氏度之间)
输出描述
华氏度(保留3位小数) 这一道题是一道主席树+牛顿迭代+矩阵快速幂+Splay+树状数组+Trie的题目。 我们接下来阐述思路。 注意到
我们可以将
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = a; i <= b; ++i)
const int maxseg = 4000005;
struct Seg {
int ls[maxseg], rs[maxseg], val[maxseg];
int tot = 0;
int build(int l, int r) {
int node = ++tot;
if(l == r) {
val[node] = 0;
return node;
}
int mid = (l + r) >> 1;
ls[node] = build(l, mid);
rs[node] = build(mid + 1, r);
return node;
}
int upd(int pre, int l, int r, int pos, int v) {
int node = ++tot;
ls[node] = ls[pre];
rs[node] = rs[pre];
val[node] = val[pre];
if(l == r) {
val[node] = v;
return node;
}
int mid = (l + r) >> 1;
if(pos <= mid) ls[node] = upd(ls[pre], l, mid, pos, v);
else rs[node] = upd(rs[pre], mid + 1, r, pos, v);
val[node] = val[ls[node]] + val[rs[node]];
return node;
}
int qry(int node, int l, int r, int pos) {
if(l == r) return val[node];
int mid = (l + r) >> 1;
if(pos <= mid) return qry(ls[node], l, mid, pos);
return qry(rs[node], mid + 1, r, pos);
}
} seg;
struct Mat {
long long m[2][2];
Mat() { memset(m, 0, sizeof(m)); }
Mat operator*(const Mat& o) const {
Mat res;
rep(i, 0, 1) rep(j, 0, 1) rep(k, 0, 1)
res.m[i][j] += m[i][k] * o.m[k][j];
return res;
}
};
Mat matpow(Mat base, int exp) {
Mat res;
res.m[0][0] = res.m[1][1] = 1;
while(exp) {
if(exp & 1) res = res * base;
base = base * base;
exp >>= 1;
}
return res;
}
long long mul9(long long C) {
Mat A;
A.m[0][0] = 9; A.m[0][1] = 0;
A.m[1][0] = 0; A.m[1][1] = 1;
Mat R = matpow(A, 1);
return R.m[0][0] * C + R.m[0][1];
}
const int maxspl = 100005;
struct Spl {
int ch[2], fa, val;
} spl[maxspl];
int spl_rt = 0, spl_tot = 0;
void spl_rot(int x) {
int y = spl[x].fa, z = spl[y].fa;
int k = (spl[y].ch[1] == x);
spl[z].ch[spl[z].ch[1] == y] = x; spl[x].fa = z;
spl[y].ch[k] = spl[x].ch[k ^ 1]; spl[spl[x].ch[k ^ 1]].fa = y;
spl[x].ch[k ^ 1] = y; spl[y].fa = x;
}
void spl_splay(int x) {
while(spl[x].fa) {
int y = spl[x].fa, z = spl[y].fa;
if(z) spl_rot((spl[y].ch[0] == x) == (spl[z].ch[0] == y) ? y : x);
spl_rot(x);
}
spl_rt = x;
}
void spl_ins(int v) {
int x = ++spl_tot;
spl[x].val = v; spl[x].ch[0] = spl[x].ch[1] = spl[x].fa = 0;
if(!spl_rt) { spl_rt = x; return; }
int cur = spl_rt, fa = 0;
while(cur) { fa = cur; cur = spl[cur].ch[v > spl[cur].val]; }
spl[fa].ch[v > spl[fa].val] = x; spl[x].fa = fa;
spl_splay(x);
}
double newt_div5(double x) {
double t = x * 0.2;
rep(i, 1, 100) {
t = t - (5.0 * t - x) / 5.0;
}
spl_ins((int)(t * 1000));
return t;
}
struct Bit {
double bit[1005];
int n;
void init(int _n) { n = _n; memset(bit, 0, sizeof(bit)); }
void add(int i, double v) {
for(; i <= n; i += i & (-i)) bit[i] += v;
}
double qry(int i) {
double s = 0;
for(; i > 0; i -= i & (-i)) s += bit[i];
return s;
}
} bit;
double add32(double x) {
bit.init(10);
bit.add(1, x);
bit.add(2, 32.0);
return bit.qry(2);
}
const int maxtri = 1005;
struct Tri {
int ch[10];
bool ed;
} tri[maxtri];
int tri_tot = 0;
void tri_ins(double val) {
char buf[50];
sprintf(buf, "%.3f", val);
int node = 0;
for(int i = 0; buf[i]; i++) {
int d;
if(buf[i] == '.' || buf[i] == '-') d = 0;
else d = buf[i] - '0';
if(!tri[node].ch[d]) tri[node].ch[d] = ++tri_tot;
node = tri[node].ch[d];
}
tri[node].ed = true;
}
void tri_out(double val) {
tri_ins(val);
printf("%.3f\n", val);
}
signed main() {
int C;
cin >> C;
int rt0 = seg.build(1, 201);
int pos = C + 101;
int rt1 = seg.upd(rt0, 1, 201, pos, C);
int val = seg.qry(rt1, 1, 201, pos);
long long c9 = mul9(val);
double c18 = newt_div5((double)c9);
double F = add32(c18);
tri_out(F);
return 0;
}
待会儿再补充一些。
<hr><p>Cloned from <a href='/article/19917' target='_blank'>tkbdawa's article</a>.</p>
对于P1001a,b的范围太大了,超过了short,需要高精度 AC Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long long ll;
const int mod=998244353,maxn=1<<21;
inline ll fstp(ll a,ll b){
ll ans=1,x=a;
while(b){
if(b&1)ans=ans*x%mod;
x=x*x%mod,b>>=1;
}
return ans;
}
void Nearly_Totally_TLE(int n,ll* a,int type){
if(n==1)return;
ll*a1=new ll[n>>1],*a2=new ll[n>>1];
for(int i=0;i<n;++i)if(i&1)a2[i/2]=a[i];else a1[i/2]=a[i];
Nearly_Totally_TLE(n>>1,a1,type),Nearly_Totally_TLE(n>>1,a2,type);
ll wmul=((type+1)?fstp(3,(mod-1)/n):fstp(fstp(3,mod-2),(mod-1)/n)),w=1;
for(int i=0;i<(n>>1);++i)a[i]=(a1[i]+w*a2[i])%mod,a[i+(n>>1)]=((a1[i]-w*a2[i])%mod+mod)%mod,w=w*wmul%mod;
delete[] a1;
delete[] a2;
}
struct BIGFLOAT;
struct BIGINT;
BIGFLOAT calc1_b( int n, const BIGFLOAT& b );
BIGINT devide_helper( const BIGINT& a, const BIGINT& b );
struct BIGINT {
int num[100005]; //存数字
bool f; //存符号
ll len; //存长度
BIGINT() : f( 0 ), len( 1 ) { num[0] = 0; }
BIGINT( const int& a ){ trans( a ); }
BIGINT( const BIGINT& b, const bool changef = 0 ) : f( b.f ^ changef ), len( b.len ) { memcpy( num, b.num, sizeof( unsigned ) * b.len ); }
inline size_t size(){ return len; }
friend ostream& operator<<( ostream& out, const BIGINT& a ){
return out << a.getS();
}
friend istream& operator>>( istream& in, BIGINT& a ){
string s;
in >> s;
a.trans( s );
return in;
}
void trans( int a ){
num[0] = 0;
f = 0, len = 0; //清空
if( a < 0 ) a = -a, f = 1; //负数
while( a ){
num[len] = a % 10; //当前位
a /= 10; //这样子第 len 位就可以除 len 次 base
len ++; //数字长度 +1
}
if( !len ) len = 1;
}
int getI() const{
int ret = 0;
for( int i = len - 1; i >= 0; -- i ){
ret = ret * 10; //这样子在 i 位一共会乘上 i 次 base
ret = ret + num[i]; //统计当前位
}
if( f ) ret = -ret;
return ret;
}
void trans( string a ){
num[0] = 0;
f = len = 0; //清空
reverse( a.begin(), a.end() ); //反转
if( a.back() == '-' ){ //负数
a.pop_back(); //删掉符号
f = 1;
}
for( int i = 0; i < a.size(); ++ i )
num[i] = a[i] - '0'; //注意 - '0' 后才是对应数字
len = a.size();
while( len > 0 && num[len - 1] == 0 ) -- len;
if( !len ) len = 1;
}
string getS() const{
string ret;
for( int i = 0; i < len; ++ i )
ret += char( num[i] + '0' ); //string 可以用加法进行拼接
if( f ) ret += '-';
reverse( ret.begin(), ret.end() );
if( ret.size() == 0 ) ret = "0"; //特判 0
return ret;
}
bool abs_less( const BIGINT& b ) const{ //绝对值更小的
if( len < b.len ) return 1;
if( len > b.len ) return 0;
for( int i = len - 1; i >= 0; -- i ){
if( b.num[i] < num[i] ) return 0;
if( num[i] < b.num[i] ) return 1;
}
return 0;
}
bool operator==( const BIGINT& b ) const{ //相等的
if( f != b.f ) return 0;
if( len != b.len ) return 0;
for( int i = len - 1; i >= 0; -- i ){
if( b.num[i] != num[i] ) return 0;
}
return 1;
}
bool operator<( const BIGINT& b ) const{ //小于
if( f && !b.f ) return 1;
if( !f && b.f ) return 0;
if( f && b.f ) return !abs_less( b ) && !( *this == b );
return abs_less( b );
}
bool operator>( const BIGINT& b ) const{ //大于
return !( *this < b ) && !( *this == b );
}
BIGINT simple_add( const BIGINT& b ) const{
BIGINT c( *this );
c.len = max( len, b.len );
for( int i = len; i <= c.len; ++ i ) c.num[i]=0; //防止 c.num 没清空,多清一位是要进位
for( int i = 0; i < b.len; ++ i ){
c.num[i] += b.num[i]; //加法
}
for( int i = 0; i < c.len; ++ i ){ //进位
c.num[i + 1] += c.num[i] / 10;
c.num[i] %= 10;
}
if( c.num[c.len] ) ++ c.len; //判断最高位
return c;
}
BIGINT operator-() const{ return BIGINT( *this, 1 ); }
BIGINT simple_minus( const BIGINT& b ) const{
if( *this == b ) return BIGINT( 0 ); //相等
if( b > *this ) return -( b.simple_minus( *this ) ); //b > a
//剩下 a > b
BIGINT c( *this );
for( int i = 0; i < b.len; ++ i ){
c.num[i] = c.num[i] - b.num[i]; //直接减
}
for( int i = 0; i < c.len; ++ i ){
if( c.num[i] < 0 ) c.num[i + 1] --, c.num[i] += 10; //退位
}
while( c.len && !c.num[c.len - 1] ) -- c.len; //重新计算长度
if( c.len == 0 ) ++ c.len;
return c;
}
BIGINT operator+( const BIGINT& b ) const{
if( !f && !b.f ) return this->simple_add( b );
if( f && b.f ) return -( ( -*this ).simple_add( -b ) );
if( !f && b.f ) return simple_minus( -b );
return b.simple_minus( -(*this) );
}
BIGINT operator-( const BIGINT& b ) const{
return *this + ( -b );
}
BIGINT operator*( const BIGINT& b ) const{
//计算长度
int n = 1;
while( n < len + b.len ) n *= 2;
//新建数组
ll* a = new ll[n], *c = new ll[n];
for( int i = 0; i < n; ++ i ) a[i] = c[i] = 0; //初始化
//乘法 3 次 NTT
for( int i = 0; i < len; ++ i ) a[i] = num[i];
for( int i = 0; i < b.len; ++ i ) c[i] = b.num[i];
Nearly_Totally_TLE( n, a, 1 );
Nearly_Totally_TLE( n, c, 1 );
for( int i = 0; i < n; ++ i ) a[i] = a[i] * c[i] % mod;
Nearly_Totally_TLE( n, a, -1 );
for( int i = 0; i < n; ++ i ) a[i] = a[i] * fstp( n, mod - 2 ) % mod; //记得要除掉 n!
//处理进位
for( int i = 0; i < n; ++ i ) a[i + 1] += a[i] / 10, a[i] %= 10;
//处理答案
BIGINT d;
d.len = n;
d.f = f ^ b.f;
for(int i = 0; i < n; ++ i ) d.num[i] = a[i];
while( d.len > 1 && !d.num[d.len - 1] ) -- d.len; //重新计算长度
//删除数组(不然可能内存泄漏)
delete[] a;
delete[] c;
return d;
}
BIGINT abs() const{
if( f ) return -*this;
return *this;
}
BIGINT operator/( const BIGINT& b ) const{
BIGINT c( 0 );
c = devide_helper( abs(), b.abs() );
c.f = f ^ b.f;
return c;
}
BIGINT operator%( const BIGINT& b ) const{
return *this - *this / b * b; //朴素取模
}
};
struct BIGFLOAT{
BIGINT a;
ll zero_pos; //0 的位置
BIGFLOAT() : a( BIGINT( 0 ) ), zero_pos( 0 ){}
BIGFLOAT( const BIGINT& b, const ll zero_p = 0 ) : a( b ), zero_pos( zero_p ){}
BIGFLOAT( const BIGFLOAT& b ) : a( b.a ), zero_pos( b.zero_pos ){}
BIGFLOAT operator<<( const ll x ) const{ // *10^x
if( a == 0 ) return BIGFLOAT( BIGINT( 0 ) );
if( zero_pos >= x ) return BIGFLOAT( a, zero_pos - x ); //可以直接右移小数点
ll mov = x - zero_pos;
BIGFLOAT b( BIGINT( 0 ) );
b.a.len = a.len + mov;
for( int i = b.a.len - 1; i >= 0; -- i ) b.a.num[i] = 0; //注意清空
for( int i = a.len - 1; i >= 0; -- i ) b.a.num[i + mov] = a.num[i]; //移位
b.a.f = a.f;
return b;
}
BIGFLOAT operator>>( const ll x ) const{ // /10^x
if( a == 0 ) return BIGFLOAT( BIGINT( 0 ) );
ll mov = 0;
while( mov < a.len && !a.num[mov] ) ++mov; //后缀 0
if( mov == a.len ) return BIGFLOAT( BIGINT( 0 ) ); //全 0
BIGFLOAT b( 0 );
b.a.len = a.len - mov; //长度
for( int i = b.a.len - 1; i >= 0; -- i )
b.a.num[i] = a.num[i + mov]; //右移
b.zero_pos = zero_pos + ( x - mov ); //计算个位数位置
return b;
}
BIGINT floor() const{
if( zero_pos >= a.len ) return BIGINT( 0 ); //小数点比整数还前面
BIGINT b( 0 );
b.len = a.len - zero_pos;
for( int i = b.len - 1; i >= 0; -- i ) b.num[i] = 0;
for( int i = b.len - 1; i >= 0; -- i ) b.num[i] = a.num[i + zero_pos]; //同移位
b.f = a.f;
return b;
}
BIGFLOAT lft_mov( const ll x ) const{ //数不变,精度增加 x 位,用于对齐
if( a == BIGINT( 0 ) ) return BIGFLOAT( BIGINT( 0 ) ); //特判 0
BIGFLOAT b( BIGINT( 0 ) );
b.a.len = a.len + x;
for( int i = b.a.len - 1; i >= 0; -- i ) b.a.num[i] = 0; //清空
for( int i = a.len - 1; i >= 0; -- i ) b.a.num[i + x] = a.num[i]; //左移
b.zero_pos = x + zero_pos; //小数点一起移动
b.a.f = a.f;
return b;
}
BIGFLOAT operator+( const BIGFLOAT& b ) const{ //加法
BIGFLOAT c( BIGINT( 0 ) );
c.zero_pos = max( zero_pos, b.zero_pos ); //小数点位置取最大,再对齐
ll m1 = c.zero_pos - zero_pos, m2 = c.zero_pos - b.zero_pos; //不定义这俩直接传参会 UB
c.a = lft_mov( m1 ).a + (b.lft_mov( m2 )).a; //位对齐
return c;
}
BIGFLOAT operator-() const{ return BIGFLOAT( -a, zero_pos ); } //取反
BIGFLOAT operator-( const BIGFLOAT& b ) const{ //减法
return ( (*this) + ( -b ) ); //偷懒
}
BIGFLOAT operator*( const BIGFLOAT& b ) const{ //乘法
BIGFLOAT c;
c.a = a * b.a; //直接乘
c.zero_pos = zero_pos + b.zero_pos; //小数点移动
auto zp = c.zero_pos;
return c;
}
friend ostream& operator<<( ostream& out, const BIGFLOAT& b ){
if( b.a.f ) out << '-';
if( b.zero_pos >= b.a.len ) out << '.';
for( ll i = b.zero_pos - 1; i >= b.a.len && i >= 0; -- i ) out <<'0';
for( ll i = b.a.len - 1; i >= 0; -- i ){
out << b.a.num[i];
if( i == b.zero_pos ) out << '.';
}
return out;
}
};
BIGFLOAT calc1_b( int n, const BIGFLOAT& b ){
auto x0 = BIGFLOAT( BIGINT( int( 1 ) ) ) >> b.a.len; //初始值不能取太大,10^(-len)
while( n-- ){
//迭代
x0 = BIGFLOAT( BIGINT( 2 ) ) * x0 - b * x0 * x0;
//限制精度,精度太高会爆内存
x0 = x0 << b.a.len * 2;
x0 = BIGFLOAT( x0.floor() );
x0 = x0 >> b.a.len * 2;
}
return x0;
}
BIGINT devide_helper( const BIGINT& a, const BIGINT& b ){
auto x = calc1_b( 20 , BIGFLOAT( b ) ); //得到迭代结果
x = x + ( BIGFLOAT( BIGINT( 1 ) ) >> ( b.len * 2 ) ); //加上极小值避免精度误差
return ( BIGFLOAT( a ) * x ).floor(); //返回相乘后下取整
}
int main(){
BIGINT a,b;
cin>>a>>b;
cout<<a+b;
}