A 取余最长路

不难发现路径可以拆成三条线段,只要知道两个转折点的位置就能计算出答案。

设sum(i,l,r)表示第i行从l到r元素的和,则答案可以表示为sum(1,1,x)+sum(2,x,y)+sum(3,y,n)%p。

前缀和一下转化成(S3[n]-S3[y-1])+S2[y]+(S1[x]-S2[x-1])%p,从小到大枚举y,将所有(S1[x]-S2[x-1])扔到一个集合里,用个set就能轻松实现了。

时间复杂度为O(NlogN)。

#include<cstdio>
#include<cctype>
#include<set>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
const int BufferSize=1<<16;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
if(head==tail) {
int l=fread(buffer,1,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
inline int read() {
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
typedef long long ll;
const int maxn=100010;
ll S1[maxn],S2[maxn],S3[maxn],ans;
set<ll> S;
set<ll>::iterator it;
int main() {
int n=read(),p=read();
rep(i,1,n) S1[i]=(S1[i-1]+read())%p;
rep(i,1,n) S2[i]=(S2[i-1]+read())%p;
rep(i,1,n) S3[i]=read();
dwn(i,n,1) S3[i]+=S3[i+1];
rep(i,1,n) {
ll val;val=(S1[i]-S2[i-1]+p)%p;
S.insert(val);val=(S3[i]+S2[i])%p;
it=S.lower_bound(p-val);
if(it==S.begin()) ans=max(ans,(val+(*(--S.end())))%p);
else ans=max(ans,(val+(*(--it)))%p);
}
printf("%lld\n",ans);
return 0;
}

B 树有几多愁

不难发现这样几个性质:

1.节点的编号肯定是按深度递减的。(可以用相邻交换法证明)

2.如果知道了叶节点大小的相对顺序就能唯一还原出整棵树的编号。(由性质1不难推出)

那么我们设f[S]表示将S集合的叶节点都标上号后的答案,因为乘积会很大,所以我们取对数记录一下方案,最后再乘回去就行了。

然后为了方便转移随便dfs算算每种状态标了多少号就行了,时间复杂度为O(2^c*c+c*n)。

#include<cstdio>
#include<cctype>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
const int BufferSize=1<<16;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
if(head==tail) {
int l=fread(buffer,1,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
inline int read() {
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
typedef long long ll;
const int maxn=100010;
int n,first[maxn],in[maxn],next[maxn<<1],to[maxn<<1],e;
void AddEdge(int u,int v) {
in[v]++;to[++e]=v;next[e]=first[u];first[u]=e;
in[u]++;to[++e]=u;next[e]=first[v];first[v]=e;
}
int val[maxn],A[maxn],fa[maxn],pa[maxn],m,dep[maxn];
int S[maxn],vis[maxn],top;
void dfs(int x) {
S[++top]=x;
while(top) {
x=S[top];
if(!vis[x]) {
int tp=pa[x];vis[x]=1;
dep[x]=dep[fa[x]]+1;if(!fa[x]||in[x]!=2) tp=x;
ren if(to[i]!=fa[x]) fa[to[i]]=x,pa[to[i]]=tp,S[++top]=to[i];
}
else {
ren if(to[i]!=fa[x]) val[x]|=val[to[i]];
if(in[x]==1&&fa[x]) A[m++]=x,val[x]=1<<m-1;
top--;
}
}
}
int g[1<<20],p[1<<20];
const int mod=1000000007;
int calc(int x,int S) {
int res=dep[x];
while(x&&((S&val[x])==val[x])) x=pa[x];
return res-dep[x];
}
double f[1<<20];
int num[maxn];
void solve(int S) {
if(!S) return;
solve(S^(1<<p[S]));
num[A[p[S]]]=g[S^(1<<p[S])]+1;
}
int main() {
n=read();
rep(i,2,n) AddEdge(read(),read());
dfs(1);
rep(S,1,(1<<m)-1) {
rep(i,0,m-1) if(S>>i&1) {
g[S]=g[S^(1<<i)]+calc(A[i],S);
break;
}
}
rep(S,1,(1<<m)-1) {
f[S]=-1e50;
rep(i,0,m-1) if(S>>i&1) {
double res=f[S^(1<<i)]+log2(g[S^(1<<i)]+1);
if(res>f[S]) f[S]=res,p[S]=i;
}
}
solve((1<<m)-1);
ll ans=1;
rep(i,0,m-1) (ans*=num[A[i]])%=mod;
printf("%lld\n",ans);
return 0;
}

C 比大小

首先解方程得到B[0]=-1,然后打一下表相信你就能发现规律(提示:按mod4分类)

#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
const int BufferSize=1<<16;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
if(head==tail) {
int l=fread(buffer,1,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
typedef long long ll;
inline ll read() {
ll x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
struct Matrix {
int A[2][2];
Matrix operator * (Matrix& b) {
Matrix c;
rep(i,0,1) rep(j,0,1) {
c.A[i][j]=0;
rep(k,0,1) c.A[i][j]+=A[i][k]*b.A[k][j];
c.A[i][j]%=4;
}
return c;
}
void print() {
rep(i,0,1) rep(j,0,1) printf("%d%c",A[i][j],j==1?'\n':' ');
}
};
void pow(Matrix& ans,ll n) {
Matrix A;A=ans;
ans.A[0][0]=1;ans.A[0][1]=0;
ans.A[1][0]=0;ans.A[1][1]=1;
while(n) {
if(n&1) ans=ans*A;
A=A*A;n>>=1;
}
}
void solve() {
ll A0=read(),a=read(),b=read(),n=read();
Matrix T,ans;
T.A[0][0]=a%4;T.A[0][1]=b%4;
T.A[1][0]=0;T.A[1][1]=1;
ans.A[0][0]=A0%4;ans.A[0][1]=0;
ans.A[1][0]=1;ans.A[1][1]=0;
pow(T,n);ans=T*ans;
if(ans.A[0][0]==1) puts("=");
else if(ans.A[0][0]%2==0) puts("<");
else puts(">");
}
int main() {
dwn(T,read(),1) solve();
return 0;
}

D 有限背包计数问题

我们先考虑一种暴力的DP做法:设f[i][j]表示用前i个物品装满容量为j的背包的方案数,然后做个多重背包就行了,时间复杂度O(N^2)。

我们再来考虑一种暴力的DP做法:设f[i][j]表示用i个物品(不考虑个数限制)装满容量为j的背包的方案数,考虑这i个物品中最小的物品,如果它是1,则f[i][j]+=f[i-1][j-1],否则说明这i个物品均大于1,f[i][j]+=f[i][j-i]。

然后我们发现对于<=sqrt(N)的物品,用第一种做法就行了,对于>sqrt(N)的物品,肯定不会使用超过sqrt(N)个,而且每个物品肯定够用,用第二种做法就行了。

最后滚动一下数组把答案合并起来就行了,时间复杂度为O(Nsqrt(N))。

#include<cstdio>
#include<cctype>
#include<queue>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
const int BufferSize=1<<16;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
if(head==tail) {
int l=fread(buffer,1,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
inline int read() {
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int maxn=100010;
const int mod=23333333;
typedef long long ll;
int n;
int f[2][maxn],g[2][maxn],g2[maxn],sum[maxn];
int main() {
n=read();int SIZE=(int)sqrt(n);
if(n==1) puts("1");
else if(n==2) puts("1");
else if(n==3) puts("2");
else if(n==4) puts("3");
else {
int cur=0;f[0][0]=1;
rep(i,1,SIZE) {
cur^=1;
rep(j,0,i-1) {
for(int k=j;k<=n;k+=i) sum[k]=((k<i?0:sum[k-i])+f[cur^1][k])%mod;
for(int k=j;k<=n;k+=i) {
f[cur][k]=(sum[k]-(k<i*(i+1)?0:sum[k-i*(i+1)])+mod)%mod;
}
}
}
ll ans=0;int cr=0;
g[0][0]=g2[0]=1;
rep(i,1,SIZE) {
cr^=1;memset(g[cr],0,sizeof(g[cr]));
if(i>1) rep(j,SIZE+1,n) g[cr][j]=(g[cr][j-i]+g[cr^1][j-SIZE-1])%mod;
else rep(j,SIZE+1,n) g[cr][j]=1;
rep(j,SIZE+1,n) (g2[j]+=g[cr][j])%=mod;
}
rep(A,0,n) (ans+=(ll)g2[A]*f[cur][n-A])%=mod;
printf("%lld\n",ans);
}
return 0;
}

E B君的骗局

我们可以设f[x][S]表示当前在x节点,已走过状态为S,期望再走几步才能结束整个过程。

直接暴力消元肯定是不行的,但我们按S分一下层,做512次消元再压一下常数就行了。

时间复杂度为O(2^c*N^3)。

#include<cstdio>
#include<cctype>
#include<queue>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
const int BufferSize=1<<16;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
if(head==tail) {
int l=fread(buffer,1,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
inline int read() {
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int maxn=55;
const double eps=1e-12;
typedef double Matrix[maxn][maxn];
const int maxm=550;
int n,m,s,x[9],e[maxn][maxn];
void gauss(Matrix& A) {
rep(i,0,n-1) {
int r=i;
rep(j,i+1,n-1) if(fabs(A[j][i])>fabs(A[r][i])) r=j;
if(fabs(A[r][i])<eps) continue;
if(r!=i) rep(j,0,n) swap(A[i][j],A[r][j]);
rep(k,0,n-1) if(k!=i)
dwn(j,n,i) A[k][j]-=A[k][i]/A[i][i]*A[i][j];
}
}
double f[maxn][maxm];
int check(int S) {
int c1=0,c2=0,c3=0,c=0;
if(S&1) c1++;if(S&2) c1++;if(S&4) c1++;
if(S&8) c2++;if(S&16) c2++;if(S&32) c2++;
if(S&64) c3++;if(S&128) c3++;if(S&256) c3++;
if(c1>=2) c++;if(c2>=2) c++;if(c3>=2) c++;
return c>=2;
}
Matrix A;
int main() {
n=read();m=read();
rep(i,1,m) {
int u=read(),v=read();
e[u][v]=e[v][u]=1;
}
rep(i,0,8) x[i]=read();s=read();
dwn(S,511,0) {
if(!check(S)) {
rep(i,0,n) rep(j,0,n) A[i][j]=0;
rep(i,0,n-1) {
int j=0;
rep(k,0,8) if(x[k]==i) j|=(1<<k);
if(j&&(!(S&j))) {A[i][i]=1;A[i][n]=f[i][S|j];continue;}
A[i][i]=1;A[i][n]=1;int cnt=0;
rep(v,0,n-1) if(e[i][v]) cnt++;
rep(v,0,n-1) if(e[i][v]) A[i][v]=-1.0/cnt;
}
gauss(A);
rep(i,0,n-1) if(fabs(A[i][i])>eps) f[i][S]=A[i][n]/A[i][i];
}
}
printf("%.6lf\n",f[s][0]);
return 0;
}

  

51nod算法马拉松13的更多相关文章

  1. 51NOD 算法马拉松8

    题目戳这里:51NOD算法马拉松8 某天晚上kpm在玩OSU!之余让我看一下B题...然后我就被坑进了51Nod... A.还是01串 水题..怎么乱写应该都可以.记个前缀和然后枚举就行了.时间复杂度 ...

  2. 51nod 算法马拉松 34 Problem D 区间求和2 (FFT加速卷积)

    题目链接  51nod 算法马拉松 34  Problem D 在这个题中$2$这个质数比较特殊,所以我们先特判$2$的情况,然后仅考虑大于等于$3$的奇数即可. 首先考虑任意一个点对$(i, j)$ ...

  3. 随便玩玩系列之一:SPOJ-RNG+51nod 算法马拉松17F+51nod 1034 骨牌覆盖v3

    先说说前面的SPOJ-RNG吧,题意就是给n个数,x1,x2,...,xn 每次可以生成[-x1,x1]范围的浮点数,把n次这种操作生成的数之和加起来,为s,求s在[A,B]内的概率 连续形的概率 假 ...

  4. 51Nod 算法马拉松21(迎新年)

    这次打算法马拉松是在星期五的晚上,发挥还算正常(废话,剩下的题都不会= =). 讲讲比赛经过吧. 8:00准时发题,拿到之后第一时间开始读. A配对,看上去像是二分图最大权匹配,一看范围吓傻了,先跳过 ...

  5. 51Nod 算法马拉松15 记一次悲壮而又开心的骗分比赛

    OwO 故事的起源大概是zcg前天发现51Nod晚上有场马拉松,然后他就很开心的过去打了 神奇的故事就开始了: 晚上的时候我当时貌似正在写线段树?然后看见zcg一脸激动告诉我第一题有九个点直接输出B就 ...

  6. 51Nod 算法马拉松23 开黑记

    惨啊……虽然开了半天黑,但是还是被dalao们踩了…… 第二次开黑,还是被卡在rank20了,我好菜啊……= = 写一写比赛经过吧…… 看到题之后习惯性都打开,A~D看上去似乎并没有什么思路,F应该是 ...

  7. 51Nod 算法马拉松22 开黑记

    这是一场惨烈的开黑大战,始于全机房开黑指望刷进rank前十拿钱的壮志,终于被各路神犇怒踩成rank20,差点200点头盾不保的落魄,想起将近一年前ad和zcg等学长挤进rank10的壮举,不由得唏嘘, ...

  8. 51nod算法马拉松 contest7

    A题 链接:http://www.51nod.com/contest/problem.html#!problemId=1417 推荐链接:http://blog.csdn.net/a837199685 ...

  9. 51nod算法马拉松15

    智力彻底没有了...看来再也拿不到奖金了QAQ... A B君的游戏 因为数据是9B1L,所以我们可以hash试一下数据... #include<cstdio> #include<c ...

随机推荐

  1. uploadify文件批量上传

    uploadify能够时间文件的批量上传,JS文件包下载地址,使用说明可以参考官网文档(http://www.uploadify.com/documentation/) 使用方法如下代码: $(&qu ...

  2. 【PHP小项目使用MVC架构】

    小项目名称是雇员管理系统. mvc是一种项目的开发模式,中文名称为模式视图控制器,是强制程序员将数据的输入.处理.输出分开的一种开发模式. 在这个小项目中,控制器使用service作为后缀名. 项目u ...

  3. [WebService] the namespace on the "definitions" element, is not a valid SOAP version

    公司对外通过webservice访问别人接口,对方webservice IP地址发生变化,切换过去之后,始终报错,在网上搜索了各种办法之后,暂时总结该问题几种可能解决办法,待真正解决时用的到. 异常详 ...

  4. shell判断文件是否存在

    转自:http://www.cnblogs.com/sunyubo/archive/2011/10/17/2282047.html 1. shell判断文件,目录是否存在或者具有权限 2. #!/bi ...

  5. iOS经典面试题(转)

    iOS经典面试题   前言 写这篇文章的目的是因为前两天同学想应聘iOS开发,从网上找了iOS面试题和答案让我帮忙看看.我扫了一眼,倒吸了一口冷气,仔细一看,气的发抖.整篇题目30多个没有一个答案是对 ...

  6. .NET中的六个重要概念:栈、堆、值类型、引用类型、装箱和拆箱

    为何要翻译 一来是为了感受国外优秀技术社区知名博主的高质量文章,二来是为了复习对.NET技术的基础拾遗达到温故知新的效果,最后也是为了锻炼一下自己的英文读写能力.因为是首次翻译英文文章(哎,原谅我这个 ...

  7. POJ 3241 Object Clustering 曼哈顿最小生成树

    Object Clustering   Description We have N (N ≤ 10000) objects, and wish to classify them into severa ...

  8. Build better apps: Windows 10 by 10 development series

    http://blogs.windows.com/buildingapps/2015/08/05/build-better-apps-windows-10-by-10-development-seri ...

  9. WPF RoadMap

    最近ms 更新对WPF支持,http://blogs.msdn.com/b/dotnet/archive/2014/11/12/the-roadmap-for-wpf.aspx Work on imp ...

  10. python学习第三天

    小结: 总体上,python是解释型语言,开源比较好,速度较慢,装逼神器,解释器较常用的是CPython,安装后python进入运行环境 exit()退出 第一个hello world : print ...