D1T1:
首先转化成统计AA型字符串有几种。
st[i]表示从i位置开始的AA型字符串有几个,ed[i]表示到i结束的有几个。
ans=∑st[i]*ed[i-1]
然后枚举A的长度L(AA长度的一半),i=k*L,j=(k+1)*L
观察x=lcp(i,j)和y=lcs(i-1,j-1)发现只有当x+y>=L时存在长度为L的AA型,然后显然是连续的一段,算算从哪开始到哪结束,差分一下,最后求和就好了。
lcp可以用hash或者kmp(hash有时候要卡常数)
#include <bits/stdc++.h>
#define ll long long
#define N 30009
#define A 131
#define mod 998244353
#define mid (l+r>>1)
#define pd(x) ((x)<0?(x+mod):x)
using namespace std;
ll n,st[N],ed[N],h[N],H[N],p[N];
char str[N];
inline ll get(ll x,ll len)
{
return pd(h[x+len-1]-h[x-1]*p[len]%mod);
}
inline ll lcp(ll i,ll j)
{
ll l=1,r=(n-j+1),ans=0;
while (l<=r)
{
if (get(i,mid)==get(j,mid)) ans=mid,l=mid+1;
else r=mid-1;
}
return ans;
}
inline ll Get(ll x,ll len)
{
return pd(H[x-len+1]-H[x+1]*p[len]%mod);
}
inline ll lcs(ll i,ll j)
{
ll l=1,r=i,ans=0;
while (l<=r)
{
if (Get(i,mid)==Get(j,mid)) ans=mid,l=mid+1;
else r=mid-1;
}
return ans;
}
int main()
{
ll T;
scanf("%lld",&T);
while (T--)
{
scanf("%s",str+1);
n=strlen(str+1);
for (ll i=0;i<=n+1;i++) st[i]=ed[i]=0;
p[0]=1,h[0]=H[0]=h[n+1]=H[n+1]=0;
for (ll i=1;i<=n;i++) p[i]=p[i-1]*A%mod;
for (ll i=1;i<=n;i++) h[i]=(h[i-1]*A+str[i]-'a'+1)%mod;
for (ll i=n;i>=1;i--) H[i]=(H[i+1]*A+str[i]-'a'+1)%mod;
for (ll L=1;L<=(n>>1);L++)
for (ll k=1;(k+1)*L<=n;k++)
{
ll i=k*L,j=i+L;
ll x=min(lcp(i,j),L),y=min(lcs(i-1,j-1),L-1);
if (x+y>=L)
{
ll t=x+y-L+1;
st[i-y]++;
st[i-y+t]--;
ed[j+x-1]++;
ed[j+x-1-t]--;
}
}
for (ll i=1;i<=n;i++) st[i]+=st[i-1];
for (ll i=n;i>=1;i--) ed[i]+=ed[i+1];
ll ans=0;
for (ll i=1;i<n;i++)
ans+=st[i+1]*ed[i];
printf("%lld\n",ans);
}
return 0;
}
D1T2:
仔细撕烤后发现答案<=2,-1情况肯定要么c>=n*m-1,要么c==n*m-2且剩下两只连在一起。
然后答案为零就是原来就有两块及以上,也就是不完全联通。
答案等于1就是完全联通但是存在割点。否则答案等于2。
这样我们想到了tarjan求割点跟强连通分量。
首先将蛐蛐的联通情况求出来。
然后每个点的5*5的方阵内的点都是有希望的。然后对于这些点中不属于蛐蛐的点跑一遍tarjan,算出来的割点如果有满足旁边3*3的方阵中确实有蛐蛐的才是真的割点。(画个图自己想想就好啦)。整幅图不连通的话,必然有一个蛐蛐的周围24个格子属于两个或以上的联通块。然后根据这个判断联通情况就好了。
#include <bits/stdc++.h>
#define gc getchar()
#define N 100009
#define mp make_pair
#define pb push_back
#define pa pair<int,int>
#define ll long long
using namespace std;
const int sed=195337;
struct hash
{
#define mod 100007
#define M 5000010
int head[mod],dh[mod];
int x[M],y[M],w[M],next[M],siz,cnt;
inline void clear()
{
++cnt,siz=0;
}
void ins(int _x,int _y,int _i)
{
int s=(1LL*_x*sed+_y)%mod;
if (dh[s]!=cnt) dh[s]=cnt,head[s]=0;
next[++siz]=head[s],head[s]=siz;
x[siz]=_x,y[siz]=_y,w[siz]=_i;
}
int count(int _x,int _y)
{
int s=(1LL*_x*sed+_y)%mod;
if (dh[s]!=cnt) return -1;
for (s=head[s];s;s=next[s])
if (x[s]==_x&&y[s]==_y) return w[s];
return -1;
}
}pos,Pos;
const int TT=24;
const int dx[8]={0,0,1,-1,1,-1,1,-1};
const int dy[8]={1,-1,0,0,-1,1,1,-1};
int n,m,c,cnt,flag,Bel,bel[N],Be,be[N*TT],dfn[N*TT],low[N*TT],num;
bool is_cut[N*TT];
pa p[N],q[N*TT];
vector<int> go[N],Go[N*TT],point[N];
int read()
{
char ch;
int x=1;
while (ch=gc,ch<'0'||ch>'9') if (ch=='-') x=-1;
int s=ch-'0';
while (ch=gc,ch>='0'&&ch<='9') s=s*10+ch-'0';
return s*x;
}
bool check(int x)
{
for (int i=0;i<8;i++)
if (pos.count(q[x].first+dx[i],q[x].second+dy[i])!=-1)
return 1;
return 0;
}
void dfs(int x)
{
bel[x]=Bel;
for (int i=0;i<(int)go[x].size();i++)
if (!bel[go[x][i]]) dfs(go[x][i]);
}
void tarjan(int x,int fa)
{
int son=0;
low[x]=dfn[x]=++cnt,be[x]=Be;
for (int i=0;i<Go[x].size();i++)
{
int to=Go[x][i];
if (to!=fa)
{
if (!dfn[to])
{
++son,tarjan(to,x);
low[x]=min(low[x],low[to]);
if (low[to]>=dfn[x]) is_cut[x]=1;
}
else low[x]=min(low[x],dfn[to]);
}
}
if (fa==-1&&son==1) is_cut[x]=0;
}
inline bool Not_Connected()
{
for (int i=1;i<=c;i++)
for (int j=0;j<8;j++)
{
int now=pos.count(p[i].first+dx[j],p[i].second+dy[j]);
if (now!=-1) go[i].pb(now);
}
num=cnt=Bel=Be=0;
for (int i=1;i<=c;i++)
if (!bel[i]) ++Bel,dfs(i);
for (int i=1;i<=c;i++)
for (int x=-2;x<=2;x++)
for (int y=-2;y<=2;y++)
if (x!=0||y!=0)
{
pa g=mp(p[i].first+x,p[i].second+y);
if (g.first<1||g.first>n||g.second<1||g.second>m)
continue;
if (pos.count(p[i].first+x,p[i].second+y)==-1&&Pos.count(p[i].first+x,p[i].second+y)==-1)
{
q[++num]=g,Pos.ins(p[i].first+x,p[i].second+y,num);
point[bel[i]].pb(num);
}
}
if (num==TT*c) return 0;
for (int i=1;i<=num;i++)
for (int j=0;j<4;j++)
{
int now=Pos.count(q[i].first+dx[j],q[i].second+dy[j]);
if (now!=-1) Go[i].pb(now);
}
for (int i=1;i<=num;i++)
if (!dfn[i]) ++Be,tarjan(i,-1);
for (int i=1;i<=num;i++)
if (is_cut[i]&&check(i)) flag=1;
for (int i=1;i<=Bel;i++)
{
int Num=-1;
for (int j=0;j<point[i].size();j++)
if (Num==-1) Num=be[point[i][j]];
else if (be[point[i][j]]!=Num) return 1;
}
return 0;
}
inline void CL()
{
for (int i=1;i<=num;i++)
Go[i].clear(),is_cut[i]=dfn[i]=0;
for (int i=1;i<=c;i++)
point[i].clear(),go[i].clear(),bel[i]=0;
pos.clear(),Pos.clear();
}
int main()
{
int T=read();
while (T--)
{
n=read(),m=read(),c=read();
for (int i=1;i<=c;i++)
p[i].first=read(),p[i].second=read();
if (c>=(ll)n*m-1)
{
puts("-1");
continue;
}
int ans=2;
flag=(n==1)|(m==1);
for (int i=1;i<=c;i++) pos.ins(p[i].first,p[i].second,i);
if (Not_Connected())
{
puts("0");
CL();
continue;
}
if ((ll)n*m==c+2)
{
if (Be==1||!c) puts("-1");
else puts("0");
CL();
continue;
}
puts(flag?"1":"2");
CL();
}
return 0;
}
D1T3:
有趣的数论题。。感觉自己数论还是太差啦。。。
考虑一个数是否为纯循环小数。
如果x是的话,必然有
x−⌊x⌋=x⋅kp−⌊x⋅kp⌋(p∈N+)
因为
⌊x⋅kp⌋−⌊x⌋∈Z
所以
x⋅kp−x∈Z
设
x=ab(a,b∈N+)
所以
b|kp−1
即
kp≡1modb
由欧拉定理,当k,b互质时,有
kφ(b)≡1modb
所以只要k,b互质即可
那么原题也就是
设 f(n)=∑ni=1[(i,k)=1]
发现 f(n)=⌊nk⌋f(k)+f(nmodk)
(应该挺显然的)
然后可以预处理出n<=k的f(n,k),就可以o(1)求它的前缀和,就可以轻松分块啦。
然后就需要快速算出 g(n,k)=∑ni=1[(i,k)=1]μ(i)
的前缀和啦。
设 n=pc⋅q(p∈P)
那么 g(n,k)=∑ni=1[(i,q)=1]μ(i)−∑⌊np⌋i=1[(i⋅p,q)=1]μ(i⋅p)
因为所有跟q互质且跟n不互质的数a均有 a=px⋅y(x>=1)
而x>=1时 μ(a)=0
所以只要考虑x=1的情况。
则 g(n,k)=g(n,q)−∑⌊np⌋i=1[(i,q)=1][(i,p)=1]μ(i)⋅μ(p)
因为(i,p)不等于1时 μ(p⋅i)=0
所以 g(n,k)=g(n,q)−μ(p)∑⌊np⌋i=1[(i,k)=1]μ(i)=g(n,q)−μ(p)g(⌊np⌋,k)=g(n,q)+g(⌊np⌋,k)
然后我们想到了递归求解,显然递归层数小于等于n的质因子个数。
然后考虑边界情况:当n=0时,g(n,k)=0;
当k=1时, g(n,k)=g(n,1)=∑ni=1μ(i)
这显然可以杜教筛求解。
注意对杜教筛和递归都用上记忆化搜索(map)就可以轻易艹过去啦。
#include <bits/stdc++.h>
#define gc getchar()
#define ll long long
#define N 2009
#define M 1000009
using namespace std;
ll n,m,k,f[N],P[M],n_now,pri[M],cnt,mu[M],sum[M];
bool pd[M];
vector<ll> q;
map<ll,ll> mp;
map<pair<ll,ll>,ll> Mp;
ll get_mu(ll x)
{
return (x<M)?sum[x]:mp[x];
}
ll get(ll n)
{
if (n<M||mp.count(n)) return get_mu(n);
ll ans=1;
for (ll i=2,j;i<=n;i=j+1)
{
j=n/(n/i),get(n/i);
ans-=(j-i+1)*get_mu(n/i);
}
return mp[n]=ans;
}
ll g(ll n,ll k)
{
//g(n,k)=g(n,q)+g(n/p,k);
if (!k) return get(n);
if (n<=1) return n;
if (Mp.count(make_pair(n,k))) return Mp[make_pair(n,k)];
return Mp[make_pair(n,k)]=g(n,k-1)+g(n/q[k-1],k);
}
int main()
{
memset(pd,0,sizeof(pd));
mu[1]=pd[1]=1;
for (ll i=2;i<M;i++)
{
if (!pd[i]) pri[++cnt]=i,mu[i]=-1;
for (ll j=1;j<=cnt&&pri[j]*i<M;j++)
{
pd[pri[j]*i]=1;
if (i%pri[j]==0)
{
mu[i*pri[j]]=0;
break;
}
mu[i*pri[j]]=-mu[i];
}
}
for (ll i=1;i<M;i++) sum[i]=sum[i-1]+mu[i];
scanf("%lld%lld%lld",&n,&m,&k);
for (int i=1;pri[i]<=k;i++)
if (k%pri[i]==0) q.push_back(pri[i]);
n_now=n;
for (ll i=1;i<=k;i++) f[i]=f[i-1]+(__gcd(i,k)==1ll);
ll ans=0;
for (ll i=1,j;i<=min(n,m);i=j+1)
{
j=min(n/(n/i),m/(m/i));
ans+=((m/i/k)*f[k]+f[m/i%k])*(n/i)*(g(j,q.size())-g(i-1,q.size()));
}
printf("%lld\n",ans);
return 0;
}
D2T1:
按长度从大到小排序,对x,y离散化。
从大到小插入线段,原问题可以变为一段区间上的(r-l+1)条线段覆盖的点中有一个点被覆盖大于等于m次。
然后每次插入一条线段,不断拿掉现存的最长的线段(保证最大覆盖次数大于等于m),然后加入一条更短的线段后,显然如果对 上一条长线段从l开始是成立的,对它肯定也是成立的,即l肯定是单调递增的,所以类似单调队列的方式搞一搞就好了。
#include <iostream>
#include <cstdio>
#include <algorithm>
#define inf 0x7fffffff
#define gc (*buf++)
#define N 500009
using namespace std;
int n,m,b[N<<1],c[N<<1],d[N<<1],Max[N<<3],add[N<<3],l=1,r=0,ans=inf,num;
char Buf[30000000],*buf=Buf;
struct seg
{
int x,y,len;
inline bool operator <(const seg& rhs) const
{
return len>rhs.len;
}
}a[N];
inline int read()
{
char ch;
int x=1;
while (ch=gc,ch<'0'||ch>'9') if (ch=='-') x=-1;
int s=ch-'0';
while (ch=gc,ch>='0'&&ch<='9') s=s*10+ch-48;
return s*x;
}
inline void ins(int l,int r,int L,int R,int k,int x)
{
if (L<=l&&R>=r)
{
add[k]+=x,Max[k]+=x;
return;
}
int mid=l+r>>1;
if (L<=mid) ins(l,mid,L,R,k<<1,x);
if (R>mid) ins(mid+1,r,L,R,k<<1|1,x);
Max[k]=max(Max[k<<1],Max[k<<1|1]);
Max[k]+=add[k];
}
int low(int x)
{
int l=1,r=num,mid;
while (l<r)
{
mid=(l+r)>>1;
if (b[mid]>=x) r=mid;
else l=mid+1;
}
return l;
}
int main()
{
fread(Buf,1,30000000,stdin);
n=read(),m=read();
num=0;
for (int i=1;i<=n;i++)
{
a[i].x=read();a[i].y=read();
a[i].len=a[i].y-a[i].x;
b[++num]=a[i].x;
b[++num]=a[i].y;
}
sort(a+1,a+n+1);
sort(b+1,b+num+1);
for (int i=1;i<=n;i++) a[i].x=low(a[i].x),a[i].y=low(a[i].y);
for (int r=1;r<=n;r++)
{
ins(1,num,a[r].x,a[r].y,1,1);
while (Max[1]>=m)
{
ans=min(ans,a[l].len-a[r].len);
ins(1,num,a[l].x,a[l].y,1,-1);
l++;
}
}
if (ans-inf) printf("%d\n",ans); else puts("-1");
return 0;
}
D2T2:
结论太TM多了。要不扔课件跑(丢链接跑)。。?
// This is an empty program with decimal lib
#include <cstdlib>
#include <cstring>
#include <string>
// ---------- decimal lib start ----------
const int PREC = 3100;
class Decimal {
public:
Decimal();
Decimal(const std::string &s);
Decimal(const char *s);
Decimal(int x);
Decimal(long long x);
Decimal(double x);
bool is_zero() const;
// p (p > 0) is the number of digits after the decimal point
std::string to_string(int p) const;
double to_double() const;
friend Decimal operator + (const Decimal &a, const Decimal &b);
friend Decimal operator + (const Decimal &a, int x);
friend Decimal operator + (int x, const Decimal &a);
friend Decimal operator + (const Decimal &a, long long x);
friend Decimal operator + (long long x, const Decimal &a);
friend Decimal operator + (const Decimal &a, double x);
friend Decimal operator + (double x, const Decimal &a);
friend Decimal operator - (const Decimal &a, const Decimal &b);
friend Decimal operator - (const Decimal &a, int x);
friend Decimal operator - (int x, const Decimal &a);
friend Decimal operator - (const Decimal &a, long long x);
friend Decimal operator - (long long x, const Decimal &a);
friend Decimal operator - (const Decimal &a, double x);
friend Decimal operator - (double x, const Decimal &a);
friend Decimal operator * (const Decimal &a, int x);
friend Decimal operator * (int x, const Decimal &a);
friend Decimal operator / (const Decimal &a, int x);
friend bool operator < (const Decimal &a, const Decimal &b);
friend bool operator > (const Decimal &a, const Decimal &b);
friend bool operator <= (const Decimal &a, const Decimal &b);
friend bool operator >= (const Decimal &a, const Decimal &b);
friend bool operator == (const Decimal &a, const Decimal &b);
friend bool operator != (const Decimal &a, const Decimal &b);
Decimal & operator += (int x);
Decimal & operator += (long long x);
Decimal & operator += (double x);
Decimal & operator += (const Decimal &b);
Decimal & operator -= (int x);
Decimal & operator -= (long long x);
Decimal & operator -= (double x);
Decimal & operator -= (const Decimal &b);
Decimal & operator *= (int x);
Decimal & operator /= (int x);
friend Decimal operator - (const Decimal &a);
// These can't be called
friend Decimal operator * (const Decimal &a, double x);
friend Decimal operator * (double x, const Decimal &a);
friend Decimal operator / (const Decimal &a, double x);
Decimal & operator *= (double x);
Decimal & operator /= (double x);
private:
static const int len = PREC / 9 + 1;
static const int mo = 1000000000;
static void append_to_string(std::string &s, long long x);
bool is_neg;
long long integer;
int data[len];
void init_zero();
void init(const char *s);
};
Decimal::Decimal() {
this->init_zero();
}
Decimal::Decimal(const char *s) {
this->init(s);
}
Decimal::Decimal(const std::string &s) {
this->init(s.c_str());
}
Decimal::Decimal(int x) {
this->init_zero();
if (x < 0) {
is_neg = true;
x = -x;
}
integer = x;
}
Decimal::Decimal(long long x) {
this->init_zero();
if (x < 0) {
is_neg = true;
x = -x;
}
integer = x;
}
Decimal::Decimal(double x) {
this->init_zero();
if (x < 0) {
is_neg = true;
x = -x;
}
integer = (long long)x;
x -= integer;
for (int i = 0; i < len; i++) {
x *= mo;
if (x < 0) x = 0;
data[i] = (int)x;
x -= data[i];
}
}
void Decimal::init_zero() {
is_neg = false;
integer = 0;
memset(data, 0, len * sizeof(int));
}
bool Decimal::is_zero() const {
if (integer) return false;
for (int i = 0; i < len; i++) {
if (data[i]) return false;
}
return true;
}
void Decimal::init(const char *s) {
this->init_zero();
is_neg = false;
integer = 0;
// find the first digit or the negative sign
while (*s != 0) {
if (*s == '-') {
is_neg = true;
++s;
break;
} else if (*s >= 48 && *s <= 57) {
break;
}
++s;
}
// read the integer part
while (*s >= 48 && *s <= 57) {
integer = integer * 10 + *s - 48;
++s;
}
// read the decimal part
if (*s == '.') {
int pos = 0;
int x = mo / 10;
++s;
while (pos < len && *s >= 48 && *s <= 57) {
data[pos] += (*s - 48) * x;
++s;
x /= 10;
if (x == 0) {
++pos;
x = mo / 10;
}
}
}
}
void Decimal::append_to_string(std::string &s, long long x) {
if (x == 0) {
s.append(1, 48);
return;
}
char _[30];
int cnt = 0;
while (x) {
_[cnt++] = x % 10;
x /= 10;
}
while (cnt--) {
s.append(1, _[cnt] + 48);
}
}
std::string Decimal::to_string(int p) const {
std::string ret;
if (is_neg && !this->is_zero()) {
ret = "-";
}
append_to_string(ret, this->integer);
ret.append(1, '.');
for (int i = 0; i < len; i++) {
// append data[i] as "%09d"
int x = mo / 10;
int tmp = data[i];
while (x) {
ret.append(1, 48 + tmp / x);
tmp %= x;
x /= 10;
if (--p == 0) {
break;
}
}
if (p == 0) break;
}
if (p > 0) {
ret.append(p, '0');
}
return ret;
}
double Decimal::to_double() const {
double ret = integer;
double k = 1.0;
for (int i = 0; i < len; i++) {
k /= mo;
ret += k * data[i];
}
if (is_neg) {
ret = -ret;
}
return ret;
}
bool operator < (const Decimal &a, const Decimal &b) {
if (a.is_neg != b.is_neg) {
return a.is_neg && (!a.is_zero() || !b.is_zero());
} else if (!a.is_neg) {
// a, b >= 0
if (a.integer != b.integer) {
return a.integer < b.integer;
}
for (int i = 0; i < Decimal::len; i++) {
if (a.data[i] != b.data[i]) {
return a.data[i] < b.data[i];
}
}
return false;
} else {
// a, b <= 0
if (a.integer != b.integer) {
return a.integer > b.integer;
}
for (int i = 0; i < Decimal::len; i++) {
if (a.data[i] != b.data[i]) {
return a.data[i] > b.data[i];
}
}
return false;
}
}
bool operator > (const Decimal &a, const Decimal &b) {
if (a.is_neg != b.is_neg) {
return !a.is_neg && (!a.is_zero() || !b.is_zero());
} else if (!a.is_neg) {
// a, b >= 0
if (a.integer != b.integer) {
return a.integer > b.integer;
}
for (int i = 0; i < Decimal::len; i++) {
if (a.data[i] != b.data[i]) {
return a.data[i] > b.data[i];
}
}
return false;
} else {
// a, b <= 0
if (a.integer != b.integer) {
return a.integer < b.integer;
}
for (int i = 0; i < Decimal::len; i++) {
if (a.data[i] != b.data[i]) {
return a.data[i] < b.data[i];
}
}
return false;
}
}
bool operator <= (const Decimal &a, const Decimal &b) {
if (a.is_neg != b.is_neg) {
return a.is_neg || (a.is_zero() && b.is_zero());
} else if (!a.is_neg) {
// a, b >= 0
if (a.integer != b.integer) {
return a.integer < b.integer;
}
for (int i = 0; i < Decimal::len; i++) {
if (a.data[i] != b.data[i]) {
return a.data[i] < b.data[i];
}
}
return true;
} else {
// a, b <= 0
if (a.integer != b.integer) {
return a.integer > b.integer;
}
for (int i = 0; i < Decimal::len; i++) {
if (a.data[i] != b.data[i]) {
return a.data[i] > b.data[i];
}
}
return true;
}
}
bool operator >= (const Decimal &a, const Decimal &b) {
if (a.is_neg != b.is_neg) {
return !a.is_neg || (a.is_zero() && b.is_zero());
} else if (!a.is_neg) {
// a, b >= 0
if (a.integer != b.integer) {
return a.integer > b.integer;
}
for (int i = 0; i < Decimal::len; i++) {
if (a.data[i] != b.data[i]) {
return a.data[i] > b.data[i];
}
}
return true;
} else {
// a, b <= 0
if (a.integer != b.integer) {
return a.integer < b.integer;
}
for (int i = 0; i < Decimal::len; i++) {
if (a.data[i] != b.data[i]) {
return a.data[i] < b.data[i];
}
}
return true;
}
}
bool operator == (const Decimal &a, const Decimal &b) {
if (a.is_zero() && b.is_zero()) return true;
if (a.is_neg != b.is_neg) return false;
if (a.integer != b.integer) return false;
for (int i = 0; i < Decimal::len; i++) {
if (a.data[i] != b.data[i]) return false;
}
return true;
}
bool operator != (const Decimal &a, const Decimal &b) {
return !(a == b);
}
Decimal & Decimal::operator += (long long x) {
if (!is_neg) {
if (integer + x >= 0) {
integer += x;
} else {
bool last = false;
for (int i = len - 1; i >= 0; i--) {
if (last || data[i]) {
data[i] = mo - data[i] - last;
last = true;
} else {
last = false;
}
}
integer = -x - integer - last;
is_neg = true;
}
} else {
if (integer - x >= 0) {
integer -= x;
} else {
bool last = false;
for (int i = len - 1; i >= 0; i--) {
if (last || data[i]) {
data[i] = mo - data[i] - last;
last = true;
} else {
last = false;
}
}
integer = x - integer - last;
is_neg = false;
}
}
return *this;
}
Decimal & Decimal::operator += (int x) {
return *this += (long long)x;
}
Decimal & Decimal::operator -= (int x) {
return *this += (long long)-x;
}
Decimal & Decimal::operator -= (long long x) {
return *this += -x;
}
Decimal & Decimal::operator /= (int x) {
if (x < 0) {
is_neg ^= 1;
x = -x;
}
int last = integer % x;
integer /= x;
for (int i = 0; i < len; i++) {
long long tmp = 1LL * last * mo + data[i];
data[i] = tmp / x;
last = tmp - 1LL * data[i] * x;
}
if (is_neg && integer == 0) {
int i;
for (i = 0; i < len; i++) {
if (data[i] != 0) {
break;
}
}
if (i == len) {
is_neg = false;
}
}
return *this;
}
Decimal & Decimal::operator *= (int x) {
if (x < 0) {
is_neg ^= 1;
x = -x;
} else if (x == 0) {
init_zero();
return *this;
}
int last = 0;
for (int i = len - 1; i >= 0; i--) {
long long tmp = 1LL * data[i] * x + last;
last = tmp / mo;
data[i] = tmp - 1LL * last * mo;
}
integer = integer * x + last;
return *this;
}
Decimal operator - (const Decimal &a) {
Decimal ret = a;
// -0 = 0
if (!ret.is_neg && ret.integer == 0) {
int i;
for (i = 0; i < Decimal::len; i++) {
if (ret.data[i] != 0) break;
}
if (i < Decimal::len) {
ret.is_neg = true;
}
} else {
ret.is_neg ^= 1;
}
return ret;
}
Decimal operator + (const Decimal &a, int x) {
Decimal ret = a;
return ret += x;
}
Decimal operator + (int x, const Decimal &a) {
Decimal ret = a;
return ret += x;
}
Decimal operator + (const Decimal &a, long long x) {
Decimal ret = a;
return ret += x;
}
Decimal operator + (long long x, const Decimal &a) {
Decimal ret = a;
return ret += x;
}
Decimal operator - (const Decimal &a, int x) {
Decimal ret = a;
return ret -= x;
}
Decimal operator - (int x, const Decimal &a) {
return -(a - x);
}
Decimal operator - (const Decimal &a, long long x) {
Decimal ret = a;
return ret -= x;
}
Decimal operator - (long long x, const Decimal &a) {
return -(a - x);
}
Decimal operator * (const Decimal &a, int x) {
Decimal ret = a;
return ret *= x;
}
Decimal operator * (int x, const Decimal &a) {
Decimal ret = a;
return ret *= x;
}
Decimal operator / (const Decimal &a, int x) {
Decimal ret = a;
return ret /= x;
}
Decimal operator + (const Decimal &a, const Decimal &b) {
if (a.is_neg == b.is_neg) {
Decimal ret = a;
bool last = false;
for (int i = Decimal::len - 1; i >= 0; i--) {
ret.data[i] += b.data[i] + last;
if (ret.data[i] >= Decimal::mo) {
ret.data[i] -= Decimal::mo;
last = true;
} else {
last = false;
}
}
ret.integer += b.integer + last;
return ret;
} else if (!a.is_neg) {
// a - |b|
return a - -b;
} else {
// b - |a|
return b - -a;
}
}
Decimal operator - (const Decimal &a, const Decimal &b) {
if (!a.is_neg && !b.is_neg) {
if (a >= b) {
Decimal ret = a;
bool last = false;
for (int i = Decimal::len - 1; i >= 0; i--) {
ret.data[i] -= b.data[i] + last;
if (ret.data[i] < 0) {
ret.data[i] += Decimal::mo;
last = true;
} else {
last = false;
}
}
ret.integer -= b.integer + last;
return ret;
} else {
Decimal ret = b;
bool last = false;
for (int i = Decimal::len - 1; i >= 0; i--) {
ret.data[i] -= a.data[i] + last;
if (ret.data[i] < 0) {
ret.data[i] += Decimal::mo;
last = true;
} else {
last = false;
}
}
ret.integer -= a.integer + last;
ret.is_neg = true;
return ret;
}
} else if (a.is_neg && b.is_neg) {
// a - b = (-b) - (-a)
return -b - -a;
} else if (a.is_neg) {
// -|a| - b
return -(-a + b);
} else {
// a - -|b|
return a + -b;
}
}
Decimal operator + (const Decimal &a, double x) {
return a + Decimal(x);
}
Decimal operator + (double x, const Decimal &a) {
return Decimal(x) + a;
}
Decimal operator - (const Decimal &a, double x) {
return a - Decimal(x);
}
Decimal operator - (double x, const Decimal &a) {
return Decimal(x) - a;
}
Decimal & Decimal::operator += (double x) {
*this = *this + Decimal(x);
return *this;
}
Decimal & Decimal::operator -= (double x) {
*this = *this - Decimal(x);
return *this;
}
Decimal & Decimal::operator += (const Decimal &b) {
*this = *this + b;
return *this;
}
Decimal & Decimal::operator -= (const Decimal &b) {
*this = *this - b;
return *this;
}
// ---------- decimal lib end ----------
#include <bits/stdc++.h>
#define gc getchar()
#define N 8009
#define K(a,b) double(y[a]-y[b])/((a)-(b))
using namespace std;
int n,k,p,W,a[N],b[N],m,g[15][N],q[N],ed[15],h[N];
double f[15][N],y[N];
int read()
{
char ch;
int x=1;
while (ch=gc,ch<'0'||ch>'9') if (ch=='-') x=-1;
int s=ch-'0';
while (ch=gc,ch>='0'&&ch<='9') s=s*10+ch-'0';
return s*x;
}
//most 14 kinds
int main()
{
n=read(),k=read(),p=read();
for (int i=1;i<=n;i++)
{
h[i]=read();
if (h[i]<h[1]) n--,i--;
}
sort(h+1,h+n+1);
for (int i=2;i<=n;i++) h[i]+=h[i-1];
for (int i=1;i<=n;i++) f[0][i]=h[1];
k=min(k,n-1);
W=min(k,14);
for (int i=1;i<=W;i++)
{
int l=1,r=0;
for (int j=2;j<=n;j++)
{
y[j-1]=h[j-1]-f[i-1][j-1];
while (l<r&&K(q[r-1],q[r])>=K(q[r],j-1)) r--;
q[++r]=j-1;
y[j+1]=h[j];
while (l<r&&K(q[l],j+1)<=K(q[l+1],j+1)) l++;
f[i][j]=K(q[l],j+1);
g[i][j]=q[l];
}
}
ed[W]=n-(k-W);
for (int i=W;i;i--) ed[i-1]=g[i][ed[i]];
Decimal ans=Decimal(h[1]);
for (int i=1;i<=W;i++)
ans=(ans+h[ed[i]]-h[ed[i-1]])/(ed[i]-ed[i-1]+1);
for (int i=ed[W]+1;i<=n;i++)
ans=(ans+h[i]-h[i-1])/2;
cout<<ans.to_string(p+2)<<endl;
return 0;
}
D2T3:
似乎是一个提答题。。(smg,第一次遇见哎)?
前3个点好像是送分的?
第5个点我好像也会。。
剩下似乎都只会骗分啦。
似乎很复杂。。。还是丢链接跑。。
6、7、9、10的程序:
#include <bits/stdc++.h>
using namespace std;
int cnt;
int in()
{
puts("I");
return ++cnt;
}
void out(int x)
{
printf("O %d\n",x);
++cnt;
}
int add(int i,int j)
{
printf("+ %d %d\n",i,j);
return ++cnt;
}
int C(int i,string j)
{
printf("C %d %s\n",i,j.c_str());
return ++cnt;
}
int rev(int i)
{
printf("- %d\n",i);
return ++cnt;
}
int L(int i,int j)
{
printf("< %d %d\n",i,j);
return ++cnt;
}
int R(int i,int j)
{
printf("> %d %d\n",i,j);
return ++cnt;
}
int S(int i)
{
printf("S %d\n",i);
return ++cnt;
}
string zeros(int n)
{
string s;
for (int i=1;i<=n;i++) s+='0';
return s;
}
void x_to_bits(int now,int *bits)
{
now=L(now,500);
long long t=702955280397374434ll;
char now_string[100];
for (int i=31;i;i--)
{
sprintf(now_string,"-%lld",t);
bits[i]=S(C(now,now_string+zeros(142)));
now=add(now,rev(L(bits[i],500+i)));
t/=2;
}
bits[0]=R(now,500);
}
int min_and_0(int x)
{
int p=L(S(L(C(x,"0."+zeros(29)+"1"),500)),151);
int y=S(add(R(x,150),p));
return add(L(C(y,"-0.5"),152),rev(p));
}
void solve6()
{
cnt=0;
int bits[32],now=in();
x_to_bits(now,bits);
for (int i=31;i>=0;i--) out(bits[i]);
}
void solve7()
{
cnt=0;
int a=in(),b=in(),bits[3][32];
x_to_bits(a,bits[0]);
x_to_bits(b,bits[1]);
for (int i=0;i<=31;i++)
{
int now=add(bits[0][i],bits[1][i]);
bits[2][i]=add(now,rev(L(S(L(C(now,"-1.5"),500)),1)));
}
for (int i=1;i<=31;i++) bits[2][i]=L(bits[2][i],i);
for (int i=1;i<=31;i++) bits[2][0]=add(bits[2][0],bits[2][i]);
out(bits[2][0]);
}
void solve9()
{
cnt=0;
int a[17];
for (int i=1;i<=16;i++) a[i]=in();
for (int i=1;i<=16;i++)
for (int j=i+1;j<=16;j++)
{
int s=add(a[i],a[j]);
a[i]=add(a[i],min_and_0(add(rev(a[i]),a[j])));
a[j]=add(s,rev(a[i]));
}
for (int i=1;i<=16;i++) out(a[i]);
}
int p(int x)
{
return S(L(x,600));
}
int get(int x,int y)
{
int p=L(x,151);
return add(L(C(S(add(rev(p),R(y,150))),"-0.5"),152),p);
}
void solve10()
{
cnt=0;
int a=in(),b=in(),m=in(),m_fu=rev(m);
int mm=C(m,"-0."+zeros(29)+"1");
int bits[32],Bits[32];
int zero=R(m,1000),ans;
x_to_bits(a,Bits);
x_to_bits(b,bits);
int A=Bits[31];
A=add(A,get(p(add(mm,rev(A))),m_fu));
for (int i=30;i>=0;i--)
{
A=add(add(A,A),Bits[i]);
A=add(A,get(p(add(mm,rev(A))),m_fu));
}
a=A;
ans=get(C(rev(bits[0]),"1"),a);
ans=add(ans,get(p(add(mm,rev(ans))),m_fu));
for (int i=1;i<32;i++)
{
a=add(a,a);
a=add(a,get(p(add(mm,rev(a))),m_fu));
ans=add(ans,get(C(rev(bits[i]),"1"),a));
ans=add(ans,get(p(add(mm,rev(ans))),m_fu));
}
out(ans);
}
int main()
{
freopen("nodes6.out","w",stdout);
solve6();
freopen("nodes7.out","w",stdout);
solve7();
freopen("nodes9.out","w",stdout);
solve9();
freopen("nodes10.out","w",stdout);
solve10();
return 0;
}