DP + prefix tree and optimized nkoj P3723

Benchmark Description: time 1000ms
Problem Description

【Problem Description】

    Po batch dragon, also known as Bao gas. Although dinosaurs are extinct thousands of years, but Po batch dragon remained tough and live in this world, and often made treasure.

    Once a mountain, the mountain there are many black Qu Ma fear of bomb shelters, each of Aspergillus Ma fear of the dugout has a number of treasure batch dragon, Po batch dragon have made the same treasure.

There was once a mountain, the mountain is only a hill. There are a lot of mountain road up the mountain strip, the end of each route is top of the hill.

There are many black mountain song Ma fear of air-raid shelter, where the top of the hill there is a fear of black Qu Ma dugout. In addition to the summit of Aspergillus Ma fear of air-raid shelter, the mountain every other black Qu Ma fear of air-raid shelter can go in the direction of the summit, and this direction is unique. After the song from a black linen fear of the dugout toward the uphill walk some distance to reach another black Qu Ma fear of air-raid shelter, to reach this shelter Aspergillus Ma called on the fear that the black hole below Qu Ma fear of bomb shelters.

Each black Qu Ma fear of the dugout has a number of treasure batch dragon, Aspergillus capacity of each dugout fear of hemp is limited, a treasure batch number can not be too long Aspergillus Ma fear of the dugout to live, otherwise it will be very crowded.

Po batch dragon have made the same treasure, they believe that the number of treasure batch of black dragon on each hole dugout Qu Ma fear of living can not exceed the number of treasure batch of black dragon this song Ma fear of the dugout to live, unless the hole exactly lived. If you violate this rule, it is FABAO fail, they are no longer approved treasure Long, Po batch dragon from extinction.

Now that you know the capacity of each of Aspergillus dugout of fear of hemp, Po batch dragon you want to calculate how many mountains of the distribution.

Input Format

A first line integer representing the number of shelter hemp fear Aspergillus. Each Aspergillus hemp fear shelter by number, wherein a number of Aspergillus hemp fear shelter at the top.

The second line integer, the i-th, showing capacity of the i-th dugout Aspergillus Ma fear, i.e., the number of Long Bao batch Aspergillus hemp fear air-raid shelter must stay within range.

The third row integer, the i-th number is showing on the i + 1 hole Ma fear Aspergillus shelter number.

Output Format

Line An integer representing the distribution of the mountain Long Bao batch number mod 1234567891 (this is a prime number) results. 

Sample input 1


2 3 3 
1 1

Sample output 1

41

Sample input 2


3 1 3 
1 1 

 
F [i] [j] i is represented by i root prefix and the number of the j-th program of optimization can live
nodgd out of the question really is cancer 
Push the card is a good space equation
Search my memory card
Seeking top learned sequence DP (it should be called DAG + DP)
code:
//
#include<stdio.h>
#include<bits/stdc++.h>
#define mod 1234567891
#define ll long long 
using namespace std;
#define maxnn 500001
unsigned int n;
unsigned int c[maxnn];
unsigned int sum[maxnn][101];
unsigned int  las[maxnn],nex[maxnn],tot,en[maxnn];
unsigned int topp[maxnn],tot1;
void add(unsigned int a,unsigned int b)
{
    en[++tot]=b;
    nex[tot]=las[a];
    las[a]=tot;
}
void dfs(int p)
{
    for(int i=las[p];i;i=nex[i]) dfs(en[i]);
    topp[++tot1]=p;
} 

int main()
{
    cin>>n;
    unsigned int x;
    for(unsigned int i=1;i<=n;i++)
        scanf("%d",&c[i]);
    for(unsigned int i=1;i<n;i++)
    {
        scanf("%d",&x);
        add(x,i+1);
    }
    dfs(1);
    for(int z=1;z<=n;z++)
    {
        int v=topp[z];
    for(unsigned int cii=0;cii<=c[v];cii++)
    {
        unsigned int ans=1;
        if(cii==c[v])
        {
            for(unsigned int i=las[v];i;i=nex[i])
            {
                unsigned int y=en[i];
                ans=(((ll)ans%mod)*(sum[y][c[y]]%mod))%mod;
            }
        }
        else
        {
            unsigned int now;
            for(int i=las[v];i;i=nex[i])
            {
                now=0;    
                unsigned int y=en[i];
                if(cii>0&&c[y]>=cii)
                now=(sum[y][c[y]]%mod-sum[y][cii-1]%mod+mod)%mod;
                else
                if(c[y]>=cii)
                now=sum[y][c[y]]%mod;
                else
                {
                    ans=0;break;
                }
            ans=(((ll)ans%mod)*(now%mod))%mod;
            }
        }
        if(cii>=1)
            sum[v][cii]=(sum[v][cii-1]%mod+ans%mod)%mod;
        else
        {
            sum[v][cii]=ans%mod;
        }
    }
    }
    printf("%d\n",sum[1][c[1]]%mod);
}

 

Guess you like

Origin www.cnblogs.com/OIEREDSION/p/11314185.html