mod函数

[CF438E] The Child and Binary Tree

。_饼干妹妹 提交于 2019-11-26 20:26:17
link $solution:$ 考虑朴素 $dp$,设 $f_i$ 为权值为 $i$ 的二叉树个数,$C_i$ 表示 $i$ 出现的次数。 则 $$f_i=\sum_{j=0}^i C_j\sum_{k=0}^{i-j} f_k\times f_{i-j-k}$$ 时间复杂度 $O(n^3)$ 。 而简单思考发现 $j+k+(i-j-k)=i$ ,其实整个 $dp$ 过程是三个式子的卷积。 考虑将$f,c$ 写成生成函数。 则 $$F=1+C\times F^2$$ $+1$ 是因为 $f_0=1$ 。 则 $F=\dfrac{2}{1+\sqrt{1-4F}}$ 。 直接多项式操作即可,时间复杂度 $O(n\log n)$ 。 #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #define int long long #define mod 998244353 using namespace std; inline int read(){ int f=1,ans=0;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){ans=ans*10+c-'0';c

P4071 [SDOI2016]排列计数

為{幸葍}努か 提交于 2019-11-26 10:22:55
P4071 [SDOI2016]排列计数 C(n,m)*f[n-m],f函数为错排数,特判n==m的时候,ans=1 #include <iostream> #include <cstdio> #include <queue> #include <algorithm> #include <cmath> #include <cstring> #define inf 2147483647 #define N 1000010 #define mod 1000000007 #define p(a) putchar(a) #define For(i,a,b) for(long long i=a;i<=b;++i) //by war //2019.8.7 using namespace std; long long T,n,m,f[N],a[N],x,y,ans; void in(long long &x){ long long y=1;char c=getchar();x=0; while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();} while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();} x*=y; } void o(long long x){ if(x<0){p('-');x=-x;}

【CF 850E】Random Elections(FWT)

≡放荡痞女 提交于 2019-11-26 09:18:00
传送门 题解: 题意差评。题意 传送门 。 其实就是 n n n 个位,每个对于ABC有一个排名,询问哪个大的时候会返回一个bool,把 n n n 个bool排成一个 01 01 0 1 串 然后我们发现当两次传入变量都为 0 0 0 或者 1 1 1 的时候有两种合法排列,两次传入值不同的时候有唯一合法排列。 对于得分函数求xor卷积,则所有合法的情况 f [ S ] = 1 & & f [ T ] = 1 f[S]=1\&\&f[T]=1 f [ S ] = 1 & & f [ T ] = 1 会对 f [ S ⊕ T ] f[S\oplus T] f [ S ⊕ T ] 产生贡献,然后考虑为 0 0 0 的位是原来相同的,需要考虑两种合法排列,方案数需要乘上 2 n − p o p c o u n t ( S ⊕ T ) 2^{n-popcount(S\oplus T)} 2 n − p o p c o u n t ( S ⊕ T ) 。 显然每个人赢两场是互斥事件,总方案数乘上3就行了。 代码: # include <bits/stdc++.h> # define ll long long # define re register # define cs const cs int mod = 1e9 + 7 , inv2 = mod + 1 >> 1 ; inline