ACM-ICPC 2018 焦作赛区网络预赛(A B E F G H I K L)

ACM-ICPC 2018 焦作赛区网络预赛(A B E F G H I K L)

发了博客一万年之后才发现H1写错了(tao


A. Magic Mirror

题目链接
题面:

Jessie has a magic mirror.

Every morning she will ask the mirror: ‘Mirror mirror tell me, who is the most beautiful girl in the world?’ If the mirror says her name, she will praise the mirror: ‘Good guy!’, but if the mirror says the name of another person, she will assail the mirror: ‘Dare you say that again?’

Today Jessie asks the mirror the same question above, and you are given a series of mirror’s answers. For each answer, please output Jessie’s response. You can assume that the uppercase or lowercase letters appearing anywhere in the name will have no influence on the answer. For example, ‘Jessie’ and ‘jessie’ represent the same person.

Input
The first line contains an integer T(1 \le T \le 100)T(1≤T≤100), which is the number of test cases.

Each test case contains one line with a single-word name, which contains only English letters. The length of each name is no more than 1515.

Output
For each test case, output one line containing the answer.

样例输入 复制
2
Jessie
Justin
样例输出 复制
Good guy!
Dare you say that again?

题意:

日常签到

思路:

日常签到

AC代码:

string go = "jessie";

int main()
{
    //freopen("output","r",stdin);
    //freopen("output","w+",stdout);
    string s;
    TCASE(_)
    {
        cin>>s;
        if(s.length()!=6)
        {
            printf("Dare you say that again?\n");
            continue;
        }
        for(int i=0;i<s.length();++i)
            if(s[i]>='A'&&s[i]<='Z')    s[i] = 'a'+(s[i]-'A');
        if(s==go)   printf("Good guy!\n");
        else    printf("Dare you say that again?\n");
    }
    return 0;
}


B. Mathmatical Curse(DP)

题目链接
题面:

A prince of the Science Continent was imprisoned in a castle because of his contempt for mathematics when he was young, and was entangled in some mathematical curses. He studied hard until he reached adulthood and decided to use his knowledge to escape the castle.

There are NN rooms from the place where he was imprisoned to the exit of the castle. In the i^{th}i
th
room, there is a wizard who has a resentment value of a[i]a[i]. The prince has MM curses, the j^{th}j
th
curse is f[j]f[j], and f[j]f[j] represents one of the four arithmetic operations, namely addition(’+’), subtraction(’-’), multiplication(’*’), and integer division(’/’). The prince’s initial resentment value is KK. Entering a room and fighting with the wizard will eliminate a curse, but the prince’s resentment value will become the result of the arithmetic operation f[j]f[j] with the wizard’s resentment value. That is, if the prince eliminates the j^{th}j
th
curse in the i^{th}i
th
room, then his resentment value will change from xx to (x\ f[j]\ a[i]x f[j] a[i]), for example, when x=1, a[i]=2, f[j]=x=1,a[i]=2,f[j]=’+’, then xx will become 1+2=31+2=3.

Before the prince escapes from the castle, he must eliminate all the curses. He must go from a[1]a[1] to a[N]a[N] in order and cannot turn back. He must also eliminate the f[1]f[1] to f[M]f[M] curses in order(It is guaranteed that N\ge MN≥M). What is the maximum resentment value that the prince may have when he leaves the castle?

Input
The first line contains an integer T(1 \le T \le 1000)T(1≤T≤1000), which is the number of test cases.

For each test case, the first line contains three non-zero integers: N(1 \le N \le 1000), M(1 \le M \le 5)N(1≤N≤1000),M(1≤M≤5) and K(-1000 \le K \le 1000K(−1000≤K≤1000), the second line contains NN non-zero integers: a[1], a[2], …, a[N](-1000 \le a[i] \le 1000)a[1],a[2],…,aN, and the third line contains MM characters: f[1], f[2], …, f[M](f[j] =f[1],f[2],…,f[M](f[j]=’+’,’-’,’*’,’/’, with no spaces in between.

Output
For each test case, output one line containing a single integer.

样例输入 复制
3
2 1 5
2 3
/
3 2 1
1 2 3
++
4 4 5
1 2 3 4
±*/
样例输出 复制
2
6
3

题意:

在给定的N个数中依次使用M个运算符,初始值为K,求最大值。
#####思路:
很显然就是DP,但是最大值既可以从之前的最小值转移过来,也可以从最大值转移过来,故用三维即可。

AC代码:

const int MAXN = 5005;
const ll MOD = 1e9+7 ;
const int INF = 1e9+7;

int _;

using namespace std;


ll dp[MAXN][10][2];
ll aaa[MAXN];
char op[10];

ll cal(ll x, char op, ll y)
{
    if(op=='+') return x+y;
    if(op=='-') return x-y;
    if(op=='*') return x*y;
    if(op=='/') return x/y;

}

int main()
{
    //freopen("test.in","r",stdin);
    //freopen("test.out","w+",stdout);
    TCASE(_)
    {
        memset(dp,0,sizeof(dp));
        int N, M, K;
        scanf("%d%d%d",&N,&M,&K);
        REP(i,1,N)  scanf("%lld",&aaa[i]);
        scanf(" %s",op+1);
        REP(i,0,N)
        REP(j,0,M)
        dp[i][j][1] = -1e18+1, dp[i][j][0]=1e18+1;
        REP(i,0,N)  dp[i][0][1]=dp[i][0][0] = 1LL*K;
        REP(i,1,N)
        REP(j,1,min(i,M))
        {
            dp[i][j][1] = max(dp[i-1][j][1],cal(dp[i-1][j-1][1],op[j],aaa[i]));
            dp[i][j][1] = max(dp[i][j][1],cal(dp[i-1][j-1][0],op[j],aaa[i]));
            dp[i][j][0] = min(dp[i-1][j][0],cal(dp[i-1][j-1][0],op[j],aaa[i]));
            dp[i][j][0] = min(dp[i][j][0],cal(dp[i-1][j-1][1],op[j],aaa[i]));
        }
        /*REP(i,0,N)
        {
            REP(j,0,M)
            cout<<dp[i][j][0]<<" ";
            cout<<endl;
        }
        cout<<endl;
        REP(i,0,N)
        {
            REP(j,0,M)
            cout<<dp[i][j][1]<<" ";
            cout<<endl;
        }*/
        ll ans = -1e18;
        REP(i,M,N)  ans = max(ans,dp[i][M][1]);
        printf("%lld\n",ans);
    }
    return 0;
}

E. Jiu Yuan Wants to Eat(树链剖分+线段树)

题目链接
题面:

题意:

给一棵树,完成针对点权的四种询问,分别是加法、乘法、取反和取和。其中和对2642^{64}取模。

思路:

树链剖分+线段树。
难点在处理取反操作,注意到取反可以转化为加法和乘法,那就直接上。详情见代码。

AC代码:


int head[MAXN];
int curedgeno;

struct Edge{
    int u, v;
    int next;
    Edge(int u=0, int v=0, int next=-1): u(u), v(v), next(next) {}
}edge[MAXN<<1];

void init()
{
    memset(head,-1,sizeof(head));
    curedgeno = 0;
}

void add_edge(int u, int v)
{
    ++curedgeno;
    edge[curedgeno] = Edge(u,v);
    edge[curedgeno].next = head[u];
    head[u] = curedgeno;
}

int dep[MAXN], fa[MAXN], size[MAXN];
int son[MAXN], top[MAXN];
int pos[MAXN], rpos[MAXN];  //position in seg tree
int cursz;



void find_heavy_edge(int u, int ffa, int depth)
{
    fa[u] = ffa;
    dep[u] = depth;
    size[u] = 1;
    int maxsize = 0;
    for(int i=head[u];~i;i=edge[i].next)
    {
        int v =edge[i].v;
        if(v!=ffa)
        {
            find_heavy_edge(v,u,depth+1);
            size[u] += size[v];
            if(size[v]>maxsize)
            {
                maxsize = size[v];
                son[u] = v;
            }
        }
    }
}


void connect_heavy_edge(int u, int ffa)
{
    pos[u] = ++cursz;
    rpos[cursz] = u;
    top[u] = ffa;
    if(son[u]==-1)   return;
    connect_heavy_edge(son[u],ffa);
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v = edge[i].v;
        if(v!=fa[u]&&son[u]!=v)
            connect_heavy_edge(v,v);
    }
}


void cut_tree(int root)
{
    memset(size,0,sizeof(size));
    memset(son,-1,sizeof(son));
    cursz = 0;
    find_heavy_edge(root,0,0);
    connect_heavy_edge(root,root);
}

int weight[MAXN];
int N, Q;
char opt[25];
ull summ[MAXN<<2];
ull mk_add[MAXN<<2];
ull mk_mul[MAXN<<2];
const ull go = (ull)18446744073709551615;

void push_up(int x)
{
    summ[x] = summ[x<<1]+summ[x<<1|1];
}

void push_down(int l, int r, int rt)
{
    if(mk_add[rt]==0&&mk_mul[rt]==1)    return;
    mk_add[rt<<1] = mk_add[rt<<1]*mk_mul[rt] + mk_add[rt];
    mk_add[rt<<1|1] = mk_add[rt<<1|1]*mk_mul[rt] + mk_add[rt];
    mk_mul[rt<<1] = mk_mul[rt]*mk_mul[rt<<1];
    mk_mul[rt<<1|1] = mk_mul[rt]*mk_mul[rt<<1|1];

    int mid = (r+l)>>1;
    summ[rt<<1] = summ[rt<<1]*mk_mul[rt] + mk_add[rt]*(ull)(mid-l+1);
    summ[rt<<1|1] = summ[rt<<1|1]*mk_mul[rt] + mk_add[rt]*(ull)(r-mid);

    mk_add[rt] = 0;
    mk_mul[rt] = 1;
}

void build(int l, int r, int rt)
{
    mk_add[rt] = 0;
    mk_mul[rt] = 1;
    if(l==r)
    {
        summ[rt] = 0;
        //cout<<rt<<" "<<weight[rpos[l]]<<endl;
        return;
    }
    int m = (l+r)>>1;
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
    push_up(rt);
}

// flag: 0 add, 1 multiply, 2 bitwise not
void update(int flag,int ql, int qr,  ull num, int l, int r, int rt)
{
    if(l>=ql && qr>=r)
    {
        if(flag==1)
        {
            summ[rt] *= num;
            mk_add[rt] *= num;
            mk_mul[rt] *= num;
        }
        else if(flag==0)
        {
            summ[rt] = summ[rt] + (ull)(r-l+1)*num;
            mk_add[rt] += num;
        }
        else
        {
            summ[rt] = summ[rt]*go + (ull)go*(r-l+1);
            mk_add[rt] = mk_add[rt]*go + go;
            mk_mul[rt] *= go;
        }
        return;
    }
    push_down(l,r,rt);
    int m = (l+r)>>1;
    if(ql<=m)  update(flag,ql,qr,num,l,m,rt<<1);
    if(qr>m)    update(flag,ql,qr,num, m+1,r,rt<<1|1);
    push_up(rt);
}



ull query_sum(int ql, int qr, int l, int r, int rt)
{
    if(l>=ql&&r<=qr)    return summ[rt];
    push_down(l,r,rt);
    int mid = (l+r)>>1;
    ull res = 0;
    if(ql<=mid) res+=query_sum(ql,qr,l,mid,rt<<1);
    if(qr>mid)  res+=query_sum(ql,qr,mid+1,r,rt<<1|1);
    return res;
}

ull find_sum(int u, int v)
{
    int f1 = top[u], f2 = top[v];
    ull res = 0;
    while(f1!=f2)
    {
        if(dep[f1]<dep[f2])
        {
            swap(f1,f2);
            swap(u,v);
        }
        res += query_sum(pos[f1],pos[u],1,cursz,1);
        u = fa[f1];
        f1 = top[u];
    }
    if(dep[u]>dep[v])   swap(u,v);
    return res + query_sum(pos[u],pos[v],1,cursz,1);
}

void update_path(int u, int v, ull c, int flag)
{
    //cout<<c<<endl;
    int f1 = top[u], f2 = top[v];
    while(f1!=f2)
    {
        if(dep[f1]<dep[f2])
        {
            swap(f1,f2);
            swap(u,v);
        }
        update(flag,pos[f1],pos[u],c,1,cursz,1);
        u = fa[f1];
        f1 = top[u];
    }
    if(dep[u]>dep[v])   swap(u,v);
    update(flag,pos[u],pos[v],c,1,cursz,1);
}

void print(int l, int r, int rt)
{
    if(l==r)
    {
        printf("%d %llu\n",rpos[r],summ[rt]);
        return;
    }
    int m = (l+r)>>1;
    print(l,m,rt<<1);
    print(m+1,r,rt<<1|1);
}

int main()
{
    //freopen("test.in","r",stdin);
    //freopen("test.out","w+",stdout);
    while(scanf("%d",&N)!=EOF)
    {
        init();
        int op, u, v;
        ull x;
        REP(i,2,N)
        {
            scanf("%d",&v);
            add_edge(i,v);
            add_edge(v,i);
        }
        cut_tree(1);
        build(1,cursz,1);
        scanf("%d",&Q);
        while(Q--)
        {
            scanf("%d",&op);
            if(op==1)
            {
                scanf("%d%d%llu",&u,&v,&x);
                update_path(u,v,x,1);
            }
            else if(op==2)
            {
                scanf("%d%d%llu",&u,&v,&x);
                update_path(u,v,x,0);
            }
            else if(op==3)
            {
                scanf("%d%d",&u,&v);
                update_path(u,v,x,2);
            }
            else
            {
                scanf("%d%d",&u,&v);
                printf("%llu\n",find_sum(u,v));
            }
            //print(1,cursz,1);
        }
    }
    return 0;
}


F. Modular Production Line (费用流)

题目链接
题面:

An automobile factory has a car production line. Now the market is oversupply and the production line is often shut down. To make full use of resources, the manager divides the entire production line into NN parts (1…N)(1…N). Some continuous parts can produce sub-products. And each of sub-products has their own value. The manager will use spare time to produce sub-products to make money. Because of the limited spare time, each part of the production line could only work at most KK times. And Because of the limited materials, each of the sub-products could be produced only once. The manager wants to know the maximum value could he make by produce sub-products.

Input
The first line of input is TT, the number of test case.

The first line of each test case contains three integers, N, KN,K and MM. (MM is the number of different sub-product).

The next MM lines each contain three integers A_i, B_i, W_iA
i
​ ,B
i
​ ,W
i
​ describing a sub-product. The sub-product has value W_iW
i
​ . Only A_iA
i
​ to B_iB
i
​ parts work simultaneously will the sub-product be produced (include A_iA
i
​ to B_iB
i
​ ).

1 \le T \le 1001≤T≤100

1 \le K \le M \le 2001≤K≤M≤200

1 \le N \le 10^51≤N≤10
5

1 \le A_i \le B_i \le N1≤A
i
​ ≤B
i
​ ≤N

1 \le W_i \le 10^51≤W
i
​ ≤10
5

Output
For each test case output the maximum value in a separate line.

样例输入 复制
4
10 1 3
1 2 2
2 3 4
3 4 8
10 1 3
1 3 2
2 3 4
3 4 8
100000 1 3
1 100000 100000
1 2 3
100 200 300
100000 2 3
1 100000 100000
1 150 301
100 200 300
样例输出 复制
10
8
100000
100301

题意:

工厂有N台机器,每台最多工作K次来生产M种物品,对于物品i,需要[Ai,Bi][A_i,B_i]这些机器工作一次来生成,并且获利WiW_i,每种物品最多生产一次,问最大获利。

思路:

最大费用网络流问题。
可以参考 POJ 3680。
首先对N台机器离散化,并对新的N个点一次连一条容量为K费用为0的边,对每种物品i,连边AiA_iBiB_i,容量为1,费用为Ci-C_i
最后添加一个源点N+1和汇点N+2,连上对应边即可。

AC代码:

const int MAXN = 5005;
const ll MOD = 1e9+7 ;
const int INF = 1e9+7;

int _;

using namespace std;


struct edge{
    int to, cap, rev;
    int cost;
    edge(int a=0, int b=0, int c=0, int d=0): to(a), cap(b), cost(c), rev(d) {}
};

vector<edge> G[MAXN];
int level[MAXN];
int iter[MAXN];
int pre[MAXN], pree[MAXN];
int dis[MAXN], visit[MAXN], incnt[MAXN];
int N;

int spfa(int s, int t)
{
    memset(pre,-1,sizeof(pre));
    memset(visit,0,sizeof(visit));
    memset(incnt,0,sizeof(incnt));
    for(int i=0;i<=N+1;++i)   dis[i]=INF;
    deque<int> q;
    q.push_front(s);
    visit[s] = 1;
    dis[s] = 0;
    int flag = 1;
    int now, i, len;
    while (!q.empty())
    {
        now = q.front();
        q.pop_front();
        visit[now] = 0;
        len = G[now].size();
        for(i=0; i<len;++i)
        {
            edge& tmp = G[now][i];
            if (tmp.cap>0&&dis[tmp.to]>dis[now]+tmp.cost)
            {
                dis[tmp.to] = dis[now]+tmp.cost;
                pre[tmp.to] = now;
                pree[tmp.to] = i;
                if (!visit[tmp.to])
                {
                    incnt[tmp.to]++;
                    if(incnt[tmp.to]>N)
                    {
                        flag = 0;
                        break;
                    }
                    // SLF
                    if(!q.empty()&&dis[tmp.to]<dis[q.front()]) q.push_front(tmp.to);
                    else    q.push_back(tmp.to);
                    //q.push(tmp.v);
                    visit[tmp.to] = 1;
                }
            }
        }
        if(!flag)   break;
    }
    if(flag)    return dis[t]!=INF;
    else    return 0;
}

void add_edge(int from, int to, int cap, double cost)
{
    G[from].push_back(edge(to,cap,cost,G[to].size()));
    G[to].push_back(edge(from,0,-cost,G[from].size()-1));
}


void bfs(int s)
{
    memset(level,-1,sizeof(level));
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while(!que.empty())
    {
        int v = que.front();
        que.pop();
        for(int i=0;i<G[v].size();++i)
        {
            edge& e = G[v][i];
            if(e.cap>0 && level[e.to]<0)
            {
                level[e.to] = level[v] + 1;
                que.push(e.to);
            }
        }
    }
}

int dfs(int v, int t, int f)
{
    if(v==t)    return f;
    for(int& i=iter[v]; i<G[v].size(); ++i)
    {
        edge& e = G[v][i];
        if(e.cap>0 && level[v]<level[e.to])
        {
            int d = dfs(e.to, t, min(f,e.cap));
            if(d>0)
            {
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s, int t)
{
    int flow = 0;
    while(1)
    {
        bfs(s);
        if(level[t]<0)  return flow;
        memset(iter,0,sizeof(iter));
        int f = dfs(s,t,INF);
        while(f>0)
        {
            flow += f;
            f = dfs(s,t,INF);
        }
    }
}
int maxflow;

int mfmc(int s, int t)
{
    int d;
    int mincost = 0;
    maxflow = 0;
    while(spfa(s,t))
    {
        d = INF;
        for(int i=t; i!=s; i=pre[i])
            d=min(d,G[pre[i]][pree[i]].cap);

        maxflow += d;
        for(int i=t; i!=s; i=pre[i])
        {
            edge& e = G[pre[i]][pree[i]];
            e.cap -= d;
            G[i][e.rev].cap += d;
        }
        mincost += dis[t]*d;
    }
    return mincost;
}

int hashx[100005];
struct point{
    int x, y;
    int w;
}points[MAXN];




void gomapping(int M)
{
    for(int i=1;i<=M;++i)
    {
        hashx[i] = points[i].x;
        hashx[i+M] = points[i].y;
    }

    sort(hashx+1,hashx+1+2*M);

    int cntx = unique(hashx+1,hashx+1+2*M) - hashx;
    N = --cntx;
    for(int i=1;i<=M;++i)
    {
        points[i].x = lower_bound(hashx+1,hashx+1+cntx,points[i].x) - hashx;
        points[i].y = lower_bound(hashx+1,hashx+1+cntx,points[i].y) - hashx;
    }
   // for(int i=1;i<=N;++i)   points[i].show();
}



int main()
{
    //freopen("output","r",stdin);
    //freopen("output","w+",stdout);
    TCASE(_)
    {
        int nN, M, K;
        scanf("%d%d%d",&nN,&K,&M);
        REP(i,1,M)
        {
            scanf("%d%d%d",&points[i].x,&points[i].y,&points[i].w);
            points[i].y++;
        }
        gomapping(M);

        REP(i,1,N+10) G[i].clear();
        REP(i,1,N)   add_edge(i,i+1,K,0);
        REP(i,1,M)  add_edge(points[i].x,points[i].y,1,-points[i].w);
        add_edge(N+2,1,K,0);
        int res = mfmc(N+2,N+1);
        printf("%d\n",-res);
    }
    return 0;
}


G. Give Candies(规律+欧拉降幂)

题目链接
题面:

There are NN children in kindergarten. Miss Li bought them NN candies. To make the process more interesting, Miss Li comes up with the rule: All the children line up according to their student number (1…N)(1…N), and each time a child is invited, Miss Li randomly gives him some candies (at least one). The process goes on until there is no candy. Miss Li wants to know how many possible different distribution results are there.

Input
The first line contains an integer TT, the number of test case.

The next TT lines, each contains an integer NN.

1 \le T \le 1001≤T≤100

1 \le N \le 10^{100000}1≤N≤10
100000

Output
For each test case output the number of possible results (mod 1000000007).

样例输入 复制
1
4
样例输出 复制
8
#####题意:
有N个蛋糕分给最多N个小朋友,每人依次获得随机至少1块蛋糕,问分配方案数。
#####思路:
往后推几个就可以得到方案总数为2N12^{N-1},但是N特别大,欧拉降幂一下即可。
#####AC代码:



ll get_phi(ll C)
{
    ll res = C;

    for (int i = 2; i * i <= C; i++)
    {
        if (C % i == 0)
        {
            res = res / i * (i - 1);
            while (C % i == 0)
                C /= i;
        }
    }

    if (C > 1)
        res = res / C * (C - 1);

    return res;
}


ll powermod(ll n,ll k, ll MOD)
{
    ll ans=1;
    while(k)
    {
        if(k&1)
        {
            ans=((ans%MOD)*(n%MOD))%MOD;
        }
        n=((n%MOD)*(n%MOD))%MOD;
        k>>=1;
    }
    return ans;
}

string s;

int main()
{
    //freopen("test.in","r",stdin);
    //freopen("test.in","w+",stdout);
    ll mod = get_phi(MOD);
    ll N;
    TCASE(_)
    {
        cin>>s;
        N = 0;
        for(int i=0;i<s.length();++i)
        {
            N = (N*10LL+1LL*(s[i]-'0'))%mod;
        }
        N = (N-1+mod+mod);
        ll ans = powermod(2,N,MOD);
        cout<<ans<<endl;
    }
    return 0;
}

H. String ad Times(后缀自动机)

题目链接
题面:

题意:

思路:

后缀自动机板子题。
(后缀自动机是啥?hihocoder讲的最好啦:Link

AC代码:

queue<int> togo;
int deg[MAXN<<1];

const int CHAR = 26;

struct SAM_Node
{
    SAM_Node *fa,*next[CHAR];
    int len;
    int id,pos;
    int endpos;
    SAM_Node() {}
    SAM_Node(int _len)
    {
        endpos=0;
        fa = 0;
        len = _len;
        memset(next,0,sizeof(next));
    }
};
SAM_Node SAM_node[MAXN*2], *SAM_root, *SAM_last;
int SAM_size;
SAM_Node *newSAM_Node(int len)
{
    SAM_node[SAM_size] = SAM_Node(len);
    SAM_node[SAM_size].id = SAM_size;
    return &SAM_node[SAM_size++];
}
SAM_Node *newSAM_Node(SAM_Node *p)
{
    SAM_node[SAM_size] = *p;
    SAM_node[SAM_size].id = SAM_size;
    SAM_node[SAM_size].endpos = 0;
    return &SAM_node[SAM_size++];
}
void SAM_init()
{
    SAM_size = 0;
    SAM_root = SAM_last = newSAM_Node(0);
    SAM_node[0].pos = 0;
}
void SAM_add(int x,int len)
{
    SAM_Node *p = SAM_last, *np = newSAM_Node(p->len+1);
    deg[np->id] = 0;
    np->endpos = 1LL;
    np->pos = len;
    SAM_last = np;
    for(; p && !p->next[x]; p = p->fa)
        p->next[x] = np;
    if(!p)
    {
        np->fa = SAM_root;
        deg[SAM_root->id]++;
        return;
    }
    SAM_Node *q = p->next[x];
    if(q->len == p->len + 1)
    {
        np->fa = q;
        deg[q->id]++;
        return;
    }
    SAM_Node *nq = newSAM_Node(q);
    nq->len = p->len + 1;
    deg[nq->id] = 2;
    q->fa = nq;
    np->fa = nq;
    for(; p && p->next[x] == q; p = p->fa)
        p->next[x] = nq;
}
void SAM_build(char *s)
{
    SAM_init();
    int len = strlen(s);
    for(int i = 0; i < len; i++)
        SAM_add(s[i] - 'A',i+1);
}


char sss[MAXN];

int main()
{
    //freopen("test.in","r",stdin);
    //freopen("test.out","w+",stdout);
    int a, b;
    while(scanf(" %s%d%d",sss,&a,&b)!=EOF)
    {
        SAM_build(sss);
        REP(i,1,SAM_size-1)
        if(deg[i]==0)
            togo.push(i);
        while(!togo.empty())
        {
            int top = togo.front();
            togo.pop();
            if(SAM_node[top].fa==0)   continue;
            deg[(*SAM_node[top].fa).id]--;
            (*SAM_node[top].fa).endpos += SAM_node[top].endpos;
            if(deg[(*SAM_node[top].fa).id]==0)  togo.push((*SAM_node[top].fa).id);
        }
        ll ans = 0;
        REP(i,1,SAM_size-1)
        {
            if(SAM_node[i].endpos>=a&&SAM_node[i].endpos<=b)
                ans += 1LL*(SAM_node[i].len-SAM_node[i].fa->len);
        }
        printf("%lld\n",ans);
    }
    return 0;
}



I. Save the Room

题目链接
题面:

Bob is a sorcerer. He lives in a cuboid room which has a length of AA, a width of BB and a height of CC, so we represent it as AA * BB * CC. One day, he finds that his room is filled with unknown dark energy. Bob wants to neutralize all the dark energy in his room with his light magic. He can summon a 11 * 11 * 22 cuboid at a time to neutralize dark energy. But the cuboid must be totally in dark energy to take effect. Can you foresee whether Bob can save his room or not?

Input
Input has TT test cases. T \le 100T≤100

For each line, there are three integers A, B, CA,B,C.

1 \le A, B, C \le 1001≤A,B,C≤100

Output
For each test case, if Bob can save his room, print"Yes", otherwise print"No".

样例输入 复制
1 1 2
1 1 4
1 1 1
样例输出 复制
Yes
Yes
No
#####题意:
日常签到
#####思路:
日常签到
#####AC代码:


int main()
{
    //freopen("test.in","r",stdin);
    //freopen("test.in","w+",stdout);
    int A, B, C;
    while(cin>>A>>B>>C)
    {

        int flag = 0;
        if(A%2==0||B%2==0||C%2==0)  flag = 1;
        if(flag)    cout<<"Yes"<<endl;
        else    cout<<"No"<<endl;
    }
    return 0;
}

K. Transport Ship(多重揹包DP)

题目链接
题面:

There are NN different kinds of transport ships on the port. The i^{th}i
th
kind of ship can carry the weight of V[i]V[i] and the number of the i^{th}i
th
kind of ship is 2^{C[i]} - 12
C[i]
−1. How many different schemes there are if you want to use these ships to transport cargo with a total weight of SS?

It is required that each ship must be full-filled. Two schemes are considered to be the same if they use the same kinds of ships and the same number for each kind.

Input
The first line contains an integer T(1 \le T \le 20)T(1≤T≤20), which is the number of test cases.

For each test case:

The first line contains two integers: N(1 \le N \le 20), Q(1 \le Q \le 10000)N(1≤N≤20),Q(1≤Q≤10000), representing the number of kinds of ships and the number of queries.

For the next NN lines, each line contains two integers: V[i](1 \le V[i] \le 20), C[i](1 \le C[i] \le 20)Vi,Ci, representing the weight the i^{th}i
th
kind of ship can carry, and the number of the i^{th}i
th
kind of ship is 2^{C[i]} - 12
C[i]
−1.

For the next QQ lines, each line contains a single integer: S(1 \le S \le 10000)S(1≤S≤10000), representing the queried weight.

Output
For each query, output one line containing a single integer which represents the number of schemes for arranging ships. Since the answer may be very large, output the answer modulo 10000000071000000007.

样例输入 复制
1
1 2
2 1
1
2
样例输出 复制
0
1

题意:

给定N种船,每种船有2Ci12^{C_i-1}艘,每艘船能装ViV_i的货物。
给Q个询问,问要装S的货物的方案总数。

思路:

将船理解为揹包,用多重揹包的拆分方法可以得到每种揹包能装载的货物方案,最后DP一下即可。

AC代码:

int val[MAXN];
int dp[MAXN];

int main()
{
    //freopen("test.in","r",stdin);
    //freopen("test.out","w+",stdout);
    TCASE(_)
    {
        int N,Q;
        scanf("%d%d",&N,&Q);
        int v, c, tmp;
        int tot = 0;
        REP(i,1,N)
        {
            scanf("%d%d",&v,&c);
            tmp = 1;
            REP(j,1,c)
            {

                val[++tot] = tmp*v;
                tmp<<=1;
            }
        }
        memset(dp,0,sizeof(dp));
        dp[0] = 1;
        REP(i,1,tot)
            IREP(j,10000,val[i])
                dp[j] = (dp[j]+dp[j-val[i]])%MOD;

        while(Q--)
        {
            scanf("%d",&tmp);
            printf("%d\n",dp[tmp]);
        }
    }
    return 0;
}



L. Poor God Water(AC自动机+矩阵快速幂)

题目链接
题面:

God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonous.

Every hour, God Water will eat one kind of food among meat, fish and chocolate. If there are 33 continuous hours when he eats only one kind of food, he will be unhappy. Besides, if there are 33 continuous hours when he eats all kinds of those, with chocolate at the middle hour, it will be dangerous. Moreover, if there are 33 continuous hours when he eats meat or fish at the middle hour, with chocolate at other two hours, it will also be dangerous.

Now, you are the doctor. Can you find out how many different kinds of diet that can make God Water happy and safe during NN hours? Two kinds of diet are considered the same if they share the same kind of food at the same hour. The answer may be very large, so you only need to give out the answer module 10000000071000000007.

Input
The fist line puts an integer TT that shows the number of test cases. (T \le 1000T≤1000)

Each of the next TT lines contains an integer NN that shows the number of hours. (1 \le N \le 10^{10}1≤N≤10
10
)

Output
For each test case, output a single line containing the answer.

样例输入 复制
3
3
4
15
样例输出 复制
20
46
435170

题意:

问长为N的仅由a, b, c三种字符组成且不含子串“aaa", “bbb”, “ccc”, “acb”, “bca”, “cac”, "cbc"的串的总数。

思路:

POJ原题:POJ-2778.
只不过卡时间,把矩阵改成维数为20的900ms卡过去(

AC代码:
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <stack>
#include <bitset>

using namespace std;

#define FSIO  ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define DEBUG(a)   cout<<"DEBUG: "<<(a)<<endl;
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define X  first
#define Y  second
#define REP(i,st,ed)    for(int i=st;i<=ed;++i)
#define IREP(i,st,ed)   for(int i=ed;i>=st;--i)
#define TCASE(T)    cin>>T;while(T--)


const int MAXN = 1005;
const ll MOD = 1e9+7 ;
const int INF = 1e9+7;

int _;

using namespace std;


struct Matrix
{
    ll a[20][20];
    ll R;
    void init()
    {
        memset(a, 0, sizeof(a));
        for(int i=0;i<R;++i)
            a[i][i] = 1LL;
    }
    void print()
    {
        for(int i=0;i<R;++i)
        {
            for(int j=0;j<R;++j)
                cout<<a[i][j]<<" ";
            cout<<endl;
        }
    }
};

Matrix mul(Matrix a, Matrix b)
{
    Matrix ans; ans.R = a.R;
    for(int i=0; i<a.R; ++i)
        for(int j=0; j<a.R; ++j)
        {
            ans.a[i][j] = 0;
            for(int k=0; k<a.R; ++k)
            {
                ans.a[i][j] = (ans.a[i][j]+a.a[i][k] * b.a[k][j])%MOD;
            }
        }
    return ans;
}

Matrix qpow(Matrix a, ll n)
{
    Matrix ans;
    ans.R = a.R;
    ans.init();
    while(n)
    {
        //ans.print();
        if(n&1LL) ans = mul(ans, a);
        a = mul(a, a);
        n >>= 1LL;
    }
    return ans;
}

const int demension = 3;

struct Trie
{
    int next[MAXN][demension],fail[MAXN],end[MAXN];
    int root,L;
    int newnode()
    {
        for (int i=0; i<demension; i++)
            next[L][i]=-1;
        end[L++]=0;
        return L-1;
    }
    void init()
    {
        L=0;
        root=newnode();
    }
    int change(char c)
    {
        if(c=='a')  return 0;
        else if(c=='b') return 1;
        else   return 2;
    }
    void insert(char *buf)
    {
        int len=strlen(buf);
        int now=root;
        for (int i=0; i<len; i++)
        {
            if (next[now][change(buf[i])]==-1)
                next[now][change(buf[i])]=newnode();
            now=next[now][change(buf[i])];
        }
        end[now]++;
    }
    void build ()
    {
        queue<int >Q;
        fail[root]=root;
        for (int i=0; i<demension; i++)
            if (next[root][i]==-1)
                next[root][i]=root;
            else
            {
                fail[next[root][i]]=root;
                Q.push(next[root][i]);
            }
        while (!Q.empty())
        {
            int now=Q.front();
            if(end[fail[now]])  end[now]=1;
            Q.pop();
            for (int i=0; i<demension; i++)
                if (next[now][i]==-1)
                    next[now][i]=next[fail[now]][i];
                else
                {
                    fail[next[now][i]]=next[fail[now]][i];
                    Q.push(next[now][i]);
                }
        }
    }
    int query(char *buf)
    {
        int len=strlen(buf);
        int now=root;
        int res=0;
        for (int i=0; i<len; i++)
        {
            now=next[now][change(buf[i])];
            int temp=now;
            while (temp!=root)
            {
                res+=end[temp];
                end[temp]=0;
                temp=fail[temp];
            }
        }
        return res;
    }
    Matrix rua()
    {
        Matrix aa;
        aa.R = L;
        memset(aa.a,0,sizeof(aa.a));
        for(int i=0;i<L;++i)
            for(int j=0;j<3;++j)
                if(!end[next[i][j]])
                    aa.a[i][next[i][j]]++;
        //aa.print();

        return aa;
    }

};

char str[15];
Trie AC;

int main()
{
    //freopen("test.in","r",stdin);
    //freopen("test.out","w+",stdout);
    ll N;
    AC.init();
    AC.insert("aaa");
    AC.insert("bbb");
    AC.insert("ccc");
    AC.insert("acb");
    AC.insert("bca");
    AC.insert("cac");
    AC.insert("cbc");
    AC.build();
    Matrix aa = AC.rua();
    //aa.print();
    TCASE(_)
    {
        scanf("%lld",&N);
        Matrix bb = qpow(aa,N);
        ll ans = 0;
        for(int i=0;i<bb.R;++i)
            ans = (ans+bb.a[0][i])%MOD;
        printf("%lld\n",ans);
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章