B.Quadratic equation
由题意可设
x
+
y
=
k
p
+
b
x+y=kp+b
x+y=kp+b
代入第二个式子中可以得到
k
p
x
+
b
x
−
x
2
≡
c
(
m
o
d
kpx+bx-x^2 \equiv c(mod
kpx+bx−x2≡c(mod
p
)
p)
p)
第一项是p的倍数可以约掉,所以有
x
2
−
b
x
+
c
≡
0
(
m
o
d
x^2-bx+c \equiv 0(mod
x2−bx+c≡0(mod
p
)
p)
p)
配方得
(
x
−
b
2
)
2
≡
b
2
4
−
c
(
m
o
d
(x-\frac{b}{2})^2 \equiv \frac{b^2}{4}-c(mod
(x−2b)2≡4b2−c(mod
p
)
p)
p)
为了避免除法,两边先同乘4,得到
(
2
x
−
b
)
2
≡
b
2
−
4
c
(
m
o
d
(2x-b)^2 \equiv b^2-4c(mod
(2x−b)2≡b2−4c(mod
p
)
p)
p)
直接套用二次剩余的板子即可
注意这里得出来的答案的奇偶性(正负性),比如算出来的答案是
a
n
s
ans
ans
那么
2
x
−
b
=
a
n
s
2x-b=ans
2x−b=ans有可能解出来的x不是一个整数,这时候通过变换
2
x
−
b
=
m
o
d
−
a
n
s
2x-b=mod-ans
2x−b=mod−ans就有整数解了(因为mod是一个奇数)
(mod-ans本质上是对ans取负)
#include<bits/stdc++.h>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
int T;
long long mod,x,y,b,c,t;
/*long long pow_mod(long long a,long long x,long long mod)
{
long long ans = 1; long long e = a;
while (x)
{
if (x&1) ans = (ans * e) % mod;
e = (e * e) % mod;
x >>= 1;
}
return ans;
}*/
long long pow_mod(long long a,long long i,long long n)
{
if (i == 0) return 1 % n;
long long temp = pow_mod(a,i>>1,n);
temp = temp * temp % n;
if (i & 1) temp = temp * a % n;
return temp;
}
long long modsqr(long long a,long long n)
{
long long b=0,k=0,i=0,x=0;
if (n == 2) return a%n;
if (a == 0) return 0;
//cout<<a<<endl;
//cout<<pow_mod(a,(n-1)/2,n)<<endl;
if (pow_mod(a,(n-1)/2,n) == 1)
{
if (n % 4 == 3) x = pow_mod(a,(n+1)/4,n);
else
{
for (b = 1;pow_mod(b,(n-1)/2,n) == 1;b++);
i = (n-1)/2;
k = 0;
do
{
i /= 2; k /= 2;
if ((pow_mod(a,i,n) * pow_mod(b,k,n)+1) % n == 0) k += (n-1)/2;
} while (i % 2 == 0);
x = (pow_mod(a,(i+1)/2,n)*pow_mod(b,k/2,n))%n;
}
if (x*2>n) x = n - x;
return x;
}
return -1;
}
int main()
{
mod = 1000000007;
scanf("%d",&T);
while (T--)
{
scanf("%lld%lld",&b,&c);
//cout<<((b*b-4*c)%mod+mod)%mod<<endl;
t = modsqr(((b*b-4*c)%mod+mod)%mod,mod);
//cout<<t*t%mod<<endl;
if (t == -1) x = y = -1; else
{
if ((t+b)%2==0) x = (t+b)/2; else x = (mod+b-t)/2;
y = b - x; if (y < 0) y += mod;
if (x > y) swap(x,y);
}
printf("%lld %lld\n",x,y);
}
return 0;
}
H.Cutting Bamboos
显然这里要用到主席树维护区间k大值以及这k个数的和
二分答案
a
n
s
ans
ans,然后在主席树上找比这个答案高的竹子的个数
n
u
m
num
num和这些竹子的总高度
s
u
m
sum
sum
这一步之需要不断用
a
n
s
ans
ans和中间竹子的高度比较,然后选择左递归或右递归即可
那么当前砍下来的竹子的高度应该就是
s
u
m
−
a
n
s
∗
n
u
m
sum-ans*num
sum−ans∗num
实际上并不需要二分答案,只需要跑一次主席树也可以
对于当前节点,如果把左子树全部砍掉答案足够了那么就左递归,如果不够就右递归
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iomanip>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#define ll long long
#define inf 1000000000
#define mod 1000000007
#define N 200005
using namespace std;
int a[N],b[N],root[N*20],ls[N*20],rs[N*20],num[N*20];
long long sum[N*20],pre[N];
int n,q,i,tot,k,ql,qr,pp,qq;
double l,r,mid;
void build(int l,int r,int &rt)
{
rt = ++tot;
num[rt] = 0; if (l == r) return;
int m = (l + r) >> 1;
build(l,m,ls[rt]);
build(m+1,r,rs[rt]);
}
void update(int l,int r,int &rt,int last,int p)
{
rt = ++tot;
ls[rt] = ls[last]; rs[rt] = rs[last];
num[rt] = num[last] + 1;
sum[rt] = sum[last] + b[p];
if (l == r) return;
int m = (l + r) >> 1;
if (p <= m) update(l,m,ls[rt],ls[last],p);
else update(m+1,r,rs[rt],rs[last],p);
}
int query1(int ss,int tt,int l,int r,double x)
{
if (l == r) if (x > b[l]) return num[tt] - num[ss]; else return 0;
int m = (l + r) >> 1;
int cnt = num[ls[tt]] - num[ls[ss]];
int res = 0;
if (x <= b[m]) res = query1(ls[ss],ls[tt],l,m,x);
else res = query1(rs[ss],rs[tt],m+1,r,x) + num[ls[tt]] - num[ls[ss]];
return res;
}
long long query2(int ss,int tt,int l,int r,double x)
{
if (l == r) if (x > b[l]) return sum[tt] - sum[ss]; else return 0;
int m = (l + r) >> 1;
int cnt = num[ls[tt]] - num[ls[ss]];
if (x <= b[m]) return query2(ls[ss],ls[tt],l,m,x);
else return query2(rs[ss],rs[tt],m+1,r,x) + sum[ls[tt]] - sum[ls[ss]];
}
bool check(double x)
{
int rank = query1(root[ql-1],root[qr],1,k,x);
long long ksum = query2(root[ql-1],root[qr],1,k,x);
ksum = pre[qr]-pre[ql-1] - ksum;
double cur = 1.0 * ksum - x * (qr - ql + 1 - rank);
//cout<<x<<" "<<cur<<" "<<1.0*pp/qq*(pre[qr]-pre[ql-1])<<endl;
if (cur >= 1.0*pp/qq*(pre[qr]-pre[ql-1])) return true; else return false;
}
int main()
{
scanf("%d%d",&n,&q);
for (i = 1;i <= n; i++) scanf("%d",&a[i]);
for (i = 1;i <= n; i++) pre[i] = pre[i-1] + a[i];
for (i = 1;i <= n; i++) b[i] = a[i];
sort(b+1,b+n+1);
k = unique(b+1,b+n+1) - (b+1);
for (i = 1;i <= n; i++) a[i] = lower_bound(b+1,b+k+1,a[i])-b;
tot = 0;
build(1,k,root[0]);
for (i = 1;i <= n; i++) update(1,k,root[i],root[i-1],a[i]);
while (q--)
{
scanf("%d%d%d%d",&ql,&qr,&pp,&qq);
l = 0; r = 100000;
while (fabs(r-l)>1e-10)
{
mid = (l + r) / 2;
if (check(mid)) l = mid; else r = mid;
}
printf("%.10lf\n",l);
}
return 0;
}
J.Symmetrical Painting
为了避免出现小数依然是先乘2
然后考虑每一块对每一条对称轴的贡献
比如某一块的坐标是[2,8],那么在y=2的贡献是0,y=3的贡献是2,y=4的贡献是4…以此类推
所以贡献一定是一段递增然后一段递减
差分一次之后就是两段相同的数
差分两次之后就是三个数,分别是2 -4 2
累加之后再恢复到初始状态即可
#include<bits/stdc++.h>
#define fo(i,a,b) for(i=a;i<=b;i++)
#define N 300005
using namespace std;
struct node{int pos,v;} f[N*3];
int n,i,k;
long long res,sum1,sum2;
int l[N],r[N],pos[N*3],v[N*3];
bool cmp(const node &a,const node &b) {return a.pos < b.pos;}
int main()
{
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
scanf("%d",&n);
fo(i,1,n) scanf("%d%d",&l[i],&r[i]);
//fo(i,1,n) l[i]++;
fo(i,1,n) l[i] *= 2,r[i] *= 2;
k = 0;
fo(i,1,n)
{
f[++k].pos = l[i]+1; f[k].v = 2;
f[++k].pos = 1ll*(1ll*l[i]+1ll*r[i])/2+1; f[k].v = -4;
f[++k].pos = r[i]+1; f[k].v = 2;
}
sort(f+1,f+k+1,cmp);
n = 0;
fo(i,1,k)
if (f[i].pos != f[i-1].pos)
{
n++; pos[n] = f[i].pos; v[n] = f[i].v;
} else v[n] += f[i].v;
res = 0; sum1 = 0; sum2 = 0;
fo(i,1,n-1)
{
sum1 += v[i];
sum2 = sum2 + 1ll * sum1 * (pos[i+1] - pos[i]);
res = max(res,sum2);
}
cout<<res/2<<endl;
}