# 牛客网暑期ACM多校训练营（第一场）

---恢复内容开始---

Ai, j ∈ {0, 1, 2} for all 1 ≤ i ≤ n, 1 ≤ j ≤ m.

Ai, j ≤ Ai + 1, j for all 1 ≤ i < n, 1 ≤ j ≤ m.

Ai, j ≤ Ai, j + 1 for all 1 ≤ i ≤ n, 1 ≤ j < m.

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <cstring>
#include <iomanip>
//#include <unordered_map>
#include <set>
#include <ctime>
#include <sstream>
#include <stack>
//CLOCKS_PER_SEC
#define se second
#define fi first
#define ll long long
#define Pii pair<int,int>
#define Pli pair<ll,int>
#define ull unsigned long long
#define pb push_back
#define fio ios::sync_with_stdio(false);cin.tie(0)
const int N=2e3+10;
const int INF=0x3f3f3f3f;
using namespace std;
const ll mod=1e9+7;
ll c[N][N];
int main()
{
int n,m;
c[0][0]=1;
for(int i=1;i<=2000;i++){
for(int j=0;j<=i;j++){
if(j==0)c[i][j]=1;
else c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
}
}
while(~scanf("%d %d",&n,&m)){
cout<<(c[n+m][n]*c[n+m][n]%mod-c[n+m][n-1]*c[n+m][m-1]%mod+mod)%mod<<endl;
}
return 0;
}
/*
3 1 3
1 2
1 2
2 3
1 3
*/
View Code

#include<bits/stdc++.h>
//CLOCKS_PER_SEC
#define se second
#define fi first
#define ll long long
#define Pii pair<int,int>
#define Pli pair<ll,int>
#define ull unsigned long long
#define pb push_back
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N=1e5+10;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
using namespace std;

ll f[N];
int main(){
ll n,m,k;
f[1]=0;f[2]=f[3]=1;
while(~scanf("%lld%lld",&n,&m)){
for(ll i=4;i<=n;i++){
f[i]=((i-1)*(f[i-1]+f[i-2])-(((i-1)*(i-2)/2)%m)*f[i-3]%m+m)%m;

}
cout<<f[n]<<endl;
}
return 0;
}
View Code

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <cstring>
#include <iomanip>
//#include <unordered_map>
#include <set>
#include <ctime>
#include <sstream>
#include <stack>
//CLOCKS_PER_SEC
#define se second
#define fi first
#define ll long long
#define Pii pair<int,int>
#define Pli pair<ll,int>
#define ull unsigned long long
#define pb push_back
#define fio ios::sync_with_stdio(false);cin.tie(0)
const int N=1e3+10;
const int INF=0x3f3f3f3f;
using namespace std;
const ll mod=1e9+7;
Pii p[N],P[N];
int a[N];
set<vector<bool> >s;
int main()
{
int n,m1,m2;
while(scanf("%d%d%d",&n,&m1,&m2)==3){
for(int i=1;i<=m1;i++){
cin>>p[i].fi>>p[i].se;
}
for(int i=1;i<=m2;i++){
int u,v;cin>>u>>v;
P[i].fi=u,P[i].se=v;
}
for(int i=1;i<=n;i++)a[i]=i;
vector<bool>vec(50);
do{
int f=0;
for(int i=1;i<=m1;i++){
int u=p[i].fi,v=p[i].se;
f=1;
break;
}
vis[a[u]][a[v]]=vis[a[v]][a[u]]=1;
}
if(!f){
for(int i=1;i<=m2;i++){
int u=P[i].fi,v=P[i].se;
if(vis[u][v]){
vec[i]=1;
}
}
s.insert(vec);
}
for(int i=1;i<=m2;i++)vec[i]=0;
memset(vis,0,sizeof(vis));
}while(next_permutation(a+1,a+1+n));
cout<<(int)s.size()<<endl;
s.clear();
}

return 0;
}
/*
3 1 3
1 2
1 2
2 3
1 3
*/
View Code

#include<bits/stdc++.h>
//CLOCKS_PER_SEC
#define se second
#define fi first
#define ll long long
#define Pii pair<int,int>
#define Pli pair<ll,int>
#define ull unsigned long long
#define pb push_back
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N=1e5+10;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
using namespace std;
ll dp[N][20];
int pre[N];
int prem[N];
int a[N];
int main(){
int n,m,k;
while(~scanf("%d%d%d",&n,&m,&k)){
memset(prem,0,sizeof(prem));
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++){
scanf("%d",a+i);
pre[i]=prem[a[i]];
prem[a[i]]=i;
}
dp[0][0]=1;
for(int i=1;i<=n;i++){
dp[i][0]=dp[i-1][0];
for(int j=1;j<=m;j++){
if(i<j)break;
dp[i][j]=dp[i-1][j-1]+dp[i-1][j];
if(pre[i]&&j-(i-pre[i])>=0)dp[i][j]-=dp[pre[i]-1][j-(i-pre[i])];
dp[i][j]%=mod;
dp[i][j]=(dp[i][j]+mod)%mod;
}
}
printf("%lld\n",dp[n][m]);
}
return 0;
}
View Code

以及拉格朗日插值

收获杜教拉格朗日插值模版

#include <bits/stdc++.h>
//CLOCKS_PER_SEC
#define se second
#define fi first
#define ll long long
#define Pii pair<int,int>
#define Pli pair<ll,int>
#define ull unsigned long long
#define pb push_back
#define fio ios::sync_with_stdio(false);cin.tie(0)
const double Pi=3.14159265;
const int N=8e5+5;
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
using namespace std;
namespace polysum {
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
const int D=2010;
ll a[D],f[D],g[D],p[D],p1[D],p2[D],b[D],h[D][2],C[D];
ll powmod(ll a,ll b){ll res=1;a%=mod;assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll calcn(int d,ll *a,ll n) { // a[0].. a[d]  a[n]
if (n<=d) return a[n];
p1[0]=p2[0]=1;
rep(i,0,d+1) {
ll t=(n-i+mod)%mod;
p1[i+1]=p1[i]*t%mod;
}
rep(i,0,d+1) {
ll t=(n-d+i+mod)%mod;
p2[i+1]=p2[i]*t%mod;
}
ll ans=0;
rep(i,0,d+1) {
ll t=g[i]*g[d-i]%mod*p1[i]%mod*p2[d-i]%mod*a[i]%mod;
if ((d-i)&1) ans=(ans-t+mod)%mod;
else ans=(ans+t)%mod;
}
return ans;
}
void init(int M) {
f[0]=f[1]=g[0]=g[1]=1;
rep(i,2,M+5) f[i]=f[i-1]*i%mod;
g[M+4]=powmod(f[M+4],mod-2);
per(i,1,M+4) g[i]=g[i+1]*(i+1)%mod;
}
ll polysum(ll m,ll *a,ll n) { // a[0].. a[m] \sum_{i=0}^{n-1} a[i]
ll b[D];
for(int i=0;i<=m;i++) b[i]=a[i];
b[m+1]=calcn(m,b,m+1);
rep(i,1,m+2) b[i]=(b[i-1]+b[i])%mod;
return calcn(m+1,b,n-1);
}
ll qpolysum(ll R,ll n,ll *a,ll m) { // a[0].. a[m] \sum_{i=0}^{n-1} a[i]*R^i
if (R==1) return polysum(n,a,m);
a[m+1]=calcn(m,a,m+1);
ll r=powmod(R,mod-2),p3=0,p4=0,c,ans;
h[0][0]=0;h[0][1]=1;
rep(i,1,m+2) {
h[i][0]=(h[i-1][0]+a[i-1])*r%mod;
h[i][1]=h[i-1][1]*r%mod;
}
rep(i,0,m+2) {
ll t=g[i]*g[m+1-i]%mod;
if (i&1) p3=((p3-h[i][0]*t)%mod+mod)%mod,p4=((p4-h[i][1]*t)%mod+mod)%mod;
else p3=(p3+h[i][0]*t)%mod,p4=(p4+h[i][1]*t)%mod;
}
c=powmod(p4,mod-2)*(mod-p3)%mod;
rep(i,0,m+2) h[i][0]=(h[i][0]+h[i][1]*c)%mod;
rep(i,0,m+2) C[i]=h[i][0];
ans=(calcn(m,C,n)*powmod(R,n)-c)%mod;
if (ans<0) ans+=mod;
return ans;
}
} // polysum::init();
ll pow2(ll a,ll b)
{
ll res=1;
while(b)
{
if(b&1) res=res*a%mod;
a=a*a%mod;
b>>=1;
}
return res;
}
ll a[N],b[N];
int main(){
polysum::init(1010);
int n;while(~scanf("%d",&n)){
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
sort(a+1,a+1+n);
ll now=1;
ll ans=0;
for(int i=1;i<=n;i++){
if(a[i]==a[i-1]){
now*=a[i];now%=mod;
continue;
}
b[0]=0;
for(int j=1;j<=n-i+1;j++){
b[j]=j*(((pow2(j,n-i+1)-pow2(j-1,n-i+1))+mod)%mod)%mod;
}
ll tmp=(polysum::polysum(n-i+1,b,a[i]+1)-polysum::polysum(n-i+1,b,a[i-1]+1)+mod)%mod;
ans+=now*tmp%mod;
ans%=mod;
now*=a[i];now%=mod;
}
printf("%lld\n",ans);
}
return 0;
}
View Code

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <cstring>
#include <iomanip>
//#include <unordered_map>
#include <set>
#include <ctime>
#include <sstream>
#include <stack>
//CLOCKS_PER_SEC
#define se second
#define fi first
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define Pii pair<int,int>
#define Pli pair<ll,int>
#define ull unsigned long long
#define pb push_back
#define fio ios::sync_with_stdio(false);cin.tie(0)
const int N=1e5+10;
const int INF=0x3f3f3f3f;
using namespace std;
const ll mod=1e9+7;
int x=0;char ch=getchar();
while (ch<'0'||ch>'9') ch=getchar();
while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
return x;
}
struct BIT{
int val[N];
inline int lowbit(int x){
return x&(-x);
}
inline void init(){
memset(val,0,sizeof(val));
}
while(x<N){
val[x]+=d;
x+=lowbit(x);
}
}
inline int query(int x){
int ans=0;
while(x){
ans+=val[x];
x-=lowbit(x);
}
return ans;
}
inline int Q(int l,int r){
return query(r)-query(l-1);
}
}T;
int l[N],r[N];
int a[N];
int ans[N];
vector<int>L[N];
vector<pair<Pii,int> >Q[N];
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
int tot=0;
T.init();
memset(l,0,sizeof(l));
for(int i=1;i<=n;i++){
if(!l[a[i]])l[a[i]]=i,tot++;
r[a[i]]=i;
}
for(int i=1;i<=n;i++){
if(l[i])L[l[i]].pb(r[i]);
}
for(int i=1;i<=m;i++){
if(x>y)ans[i]=tot;
else  Q[x].pb({{x,y},i});
}
for(int i=n;i>=0;i--){
for(int j=0;j<(int)L[i].size();j++){
}
for(int j=0;j<(int)Q[i].size();j++){
ans[Q[i][j].se]=tot-T.query(Q[i][j].fi.se);
}
L[i].clear();
Q[i].clear();
}
for(int i=1;i<=m;i++){
printf("%d\n",ans[i]);
}
}
return 0;
}

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <cstring>
#include <iomanip>
//#include <unordered_map>
#include <set>
#include <ctime>
#include <sstream>
#include <stack>
//CLOCKS_PER_SEC
#define se second
#define fi first
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define Pii pair<int,int>
#define Pli pair<ll,int>
#define ull unsigned long long
#define pb push_back
#define fio ios::sync_with_stdio(false);cin.tie(0)
const int N=1e5+10;
const int INF=0x3f3f3f3f;
using namespace std;
const ll mod=1e9+7;
int a[N];
int cnt[N];
int n,m;
int L=1,R=0;
struct node {
int l,r,id;
}Q[N];
int ans[N];int pos[N];
int vis[N];
int tot=0;
bool cmp(node x,node y){
if(pos[x.l]==pos[y.l]){
return x.r<y.r;
}
return pos[x.l]<pos[y.l];
}
vis[a[x]]++;
if(vis[a[x]]==cnt[a[x]])tot++;
}
void del(int x){
if(vis[a[x]]==cnt[a[x]])tot--;
vis[a[x]]--;
}
int x=0;char ch=getchar();
while (ch<'0'||ch>'9') ch=getchar();
while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
return x;
}
int main(){
fio;
while(~scanf("%d %d",&n,&m)){
memset(cnt,0,sizeof(cnt));
memset(vis,0,sizeof(vis));
L=1,R=0;
tot=0;
int sz=1000;
int pp=0;
for(int i=1;i<=n;i++){
if(cnt[a[i]]==1)pp++;
}
for(int i=1;i<=m;i++){
Q[i].l++;
Q[i].r--;
Q[i].id=i;
}
sort(Q+1,Q+1+m,cmp);
for(int i=1;i<=m;i++){
if(Q[i].l>Q[i].r){
ans[Q[i].id]=pp;
continue;
}
while(L<Q[i].l){
del(L);
L++;
}
while(L>Q[i].l){
L--;
}
while(R<Q[i].r){
R++;
}
while(R>Q[i].r){
del(R);
R--;
}
ans[Q[i].id]=pp-tot;
}
for(int i=1;i<=m;i++)printf("%d\n",ans[i]);
}
return 0;
}

posted @ 2018-07-27 22:22  采蘑菇的小西佬  阅读(291)  评论(0编辑  收藏