2016 Multi-University Training Contest 4
A – (KMP+DP)
#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
#define mod 1000000007
#define ll long long
char str1[maxn],str2[maxn];
int nex[maxn];
ll dp[maxn];
int n,m;
bool vis[maxn];
void makeNext()
{
int k=-1;
for(int i=1;i<m;i++){
while(k>=0&&str2[k+1]!=str2[i]) k=nex[k];
if(str2[k+1]==str2[i]) k++;
nex[i]=k;
}
}
void kmp()
{
makeNext();
int k=-1;
for(int i=-1;i<n-1;i++){
while(k>=0&&str2[k+1]!=str1[i+1]) k=nex[k];
if(str2[k+1]==str1[i+1]) k++;
if(k==m-1){
k=nex[k];
vis[i+1]=1;
}
}
}
int main()
{
int t,kase=1;
scanf("%d",&t);
while(t--){
memset(dp,0,sizeof(dp));
memset(nex,-1,sizeof(nex));
memset(vis,0,sizeof(vis));
scanf("%s %s",str1,str2);
n=strlen(str1);
m=strlen(str2);
kmp();
for(int i=0;i<m;i++) dp[i]=vis[i];
for(int i=1;i<n;i++){
if(vis[i]) dp[i]=(dp[i-1]+dp[i-m]+1)%mod;
else dp[i]=dp[i-1];
}
printf("Case #%d: %lld\n",kase++,(dp[n-1]+1)%mod);
}
return 0;
}
C – (状压DP+高维前缀和)
E – (中国剩余定理+容斥)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 15 + 5;
int n;
ll l, r;
ll exp_gcd(ll a, ll b, ll &x, ll &y) {
if(b == 0) {
x = 1; y = 0; return a;
}
ll res = exp_gcd(b, a % b, y, x);
y -= a / b * x;
return res;
}
ll p[N], a[N];
vector <int> vec;
ll qui(ll a, ll b, ll mod) {
ll rtn = 0;
while(b) {
if(b & 1) rtn = (rtn + a) % mod;
a = (a + a) % mod;
b >>= 1;
}
return rtn;
}
void chi(ll &res, ll &mod) {
res = 0;
ll m = 1;
for(auto id : vec) m *= p[id];
for(auto id : vec) {
ll tmp = m / p[id];
ll x, y;
exp_gcd(p[id], tmp, x, y);
res = (res + qui(qui(y, tmp, m),a[id], m)) % m;
}
res = (res + m) % m;
mod = m;
}
ll coun(ll in) {
ll n7 = in / 7;
for(int s = 1; s < (1<<n); s ++) {
vec.clear();
vec.push_back(n);
int count = 0;
for(int i = 0; i < n; i ++) if(s & (1<<i)) {
count ++;
vec.push_back(i);
}
ll res, mod, num = 0;
chi(res, mod);
if(in >= res)
num = (in - res) / mod + 1;
if(count & 1) n7 -= num;
else n7 += num;
}
return n7;
}
int main() {
int T, kase = 1;
scanf("%d", &T);
while(T --) {
scanf("%d%lld%lld", &n, &l, &r);
for(int i=0; i<n; i++)
scanf("%lld%lld", p+i, a+i);
p[n] = 7; a[n] = 0;
printf("Case #%d: %lld\n", kase ++, coun(r) - coun(l-1));
}
return 0;
}
J – (DP)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int c[2 * N], a[N];
int main() {
int T, kase = 1;
scanf("%d", &T);
while(T --) {
int n, cnt = 0;
int ans = 0;
scanf("%d", &n);
for(int i=0; i<n; i++)
scanf("%d", a+i);
memset(c, 0x3f, sizeof(c));
for(int i=0; i<n; i++) {
if(a[i]) {
int na = a[i] - cnt;
int idx = lower_bound(c+n-cnt, c+n+N, na) - c;
c[idx] = na;
ans = max(ans, idx + cnt - n + 1);
} else {
cnt ++;
c[n-cnt] = -cnt;
ans ++;
}
}
printf("Case #%d: %d\n", kase++, ans);
}
return 0;
}
K – (暴力)
#include<bits/stdc++.h>
using namespace std;
char s[100][100]={"Cleveland Cavaliers",
"Golden State Warriors",
"San Antonio Spurs",
"Miami Heat",
"Miami Heat",
"Dallas Mavericks",
"L.A. Lakers",
"L.A. Lakers",
"Boston Celtics",
"San Antonio Spurs",
"Miami Heat",
"San Antonio Spurs",
"Detroit Pistons",
"San Antonio Spurs",
"L.A. Lakers",
"L.A. Lakers",
"L.A. Lakers",
"San Antonio Spurs",
"Chicago Bulls",
"Chicago Bulls",
"Chicago Bulls",
"Houston Rockets",
"Houston Rockets",
"Chicago Bulls",
"Chicago Bulls",
"Chicago Bulls",
"Detroit Pistons",
"Detroit Pistons",
"L.A. Lakers",
"L.A. Lakers",
"Boston Celtics",
"L.A. Lakers",
"Boston Celtics",
"Philadelphia 76ers",
"L.A. Lakers",
"Boston Celtics",
"L.A. Lakers",
"Seattle Sonics",
"Washington Bullets",
"Portland Trail Blazers",
"Boston Celtics",
"Golden State Warriors",
"Boston Celtics",
"New York Knicks",
"L.A. Lakers",
"Milwaukee Bucks",
"New York Knicks",
"Boston Celtics",
"Boston Celtics",
"Philadelphia 76ers",
"Boston Celtics",
"Boston Celtics",
"Boston Celtics",
"Boston Celtics",
"Boston Celtics",
"Boston Celtics",
"Boston Celtics",
"Boston Celtics",
"St. Louis Hawks",
"Boston Celtics",
"Philadelphia Warriors",
"Syracuse Nats",
"Minneapolis Lakers",
"Minneapolis Lakers",
"Minneapolis Lakers",
"Rochester Royals",
"Minneapolis Lakers",
"Minneapolis Lakers",
"Baltimore Bullets",
"Philadelphia Warriors"
};
int main()
{
char n[100];
int ji=1;
int t;
scanf("%d",&t);
getchar();
while(t--)
{
gets(n);
int ans=0;
for(int i=0;i<70;i++) {
if(strcmp(s[i],n)==0) ans++;
}
printf("Case #%d: %d\n",ji++,ans);
}
return 0;
}
L – (结论题+树状数组维护)
#include<bits/stdc++.h>
using namespace std;
int lowbit(int i) {return i&(-i);}
int shu[100000*10],n[100010];
int l[100010],r[100010],id[100010];
void add(int id,int w)
{
while(id<=100000) {
shu[id]+=w;
id+=lowbit(id);
}
}
int query(int id)
{
int ans=0;
while(id){
ans+=shu[id];
id-=lowbit(id);
}
return ans;
}
int main()
{
int m,ji=1,t;
scanf("%d",&m);
while(m--)
{
memset(shu,0,sizeof(shu));
scanf("%d",&t);
for(int i=0;i<t;i++) scanf("%d",&n[i]),id[n[i]]=i+1;
for(int i=0;i<t;i++)
{
r[n[i]]=query(100000)-query(n[i]);
add(n[i],1);
}
memset(shu,0,sizeof(shu));
for(int i=t-1;i>=0;i--)
{
l[n[i]]=query(n[i]);
add(n[i],1);
}
printf("Case #%d:",ji++);
for(int i=1;i<=t;i++)
{
printf(" %d",id[i]+r[i]-min(id[i],id[i]+r[i]-l[i]));
}
cout<<endl;
}
return 0;
}