1. 程式人生 > >bzoj 2159: Crash 的文明世界

bzoj 2159: Crash 的文明世界

clas ref 兩個 printf res 簡單的 var esp 重設

2159: Crash 的文明世界

Time Limit: 10 Sec Memory Limit: 259 MB
Submit: 478 Solved: 233
[Submit][Status][Discuss]

Description

Crash 小朋友最近迷上了一款遊戲——文明5(Civilization V)。在這個遊戲中,玩家可以建立和發展自己的國家,通過外交和別的國家交流,或是通過戰爭征服別的國家。現在Crash 已經擁有了一個N 個城市的國家,這些城市之間通過道路相連。由於建設道路是有花費的,因此Crash 只修建了N-1 條道路連接這些城市,不過可以保證任意兩個城市都有路徑相通。在遊戲中,Crash 需要選擇一個城市作為他的國家的首都,選擇首都需要考慮很多指標,有一個指標是這樣的: 技術分享圖片

其中S(i)表示第i 個城市的指標值,dist(i, j)表示第i 個城市到第j 個城市需要經過的道路條數的最小值,k 為一個常數且為正整數。因此Crash 交給你一個簡單的任務:給出城市之間的道路,對於每個城市,輸出這個城市的指標值,由於指標值可能會很大,所以你只需要輸出這個數mod 10007 的值。

Input

輸入的第一行包括兩個正整數N 和k。下面有N-1 行,每行兩個正整數u、v (1 ≤ u, v ≤ N),表示第u 個城市和第v 個城市之間有道路相連。這些道路保證能符合題目的要求。

Output

輸出共N 行,每行一個正整數,第i 行的正整數表示第i 個城市的指標值 mod 10007 的值。

Sample Input

5 2
1 2
1 3
2 4
2 5

Sample Output

10
7
23
18
18

HINT

20%的數據滿足N ≤ 5000、k ≤ 30。 50%的數據滿足N ≤ 50000、k ≤ 30。 100%的數據滿足N ≤ 50000、k ≤ 150。 【特別註意】由於數據大小限制為5MB,我只好對測試時的輸入文件進行壓縮處理。下面的函數可以將壓縮的輸入文件轉化為原始輸入文件。(函數從infile 中讀入壓縮的輸入文件,將解壓縮後的輸入文件輸出到outfile 中) C/C++版本: void Uncompress(FILE *infile, FILE *outfile) { int N, k, L, i, now, A, B, Q, tmp; fscanf(infile, "%d%d%d", &N, &k, &L); fscanf(infile, "%d%d%d%d", &now, &A, &B, &Q); fprintf(outfile, "%d %d\n", N, k); for (i = 1; i < N; i ++) { now = (now * A + B) % Q; tmp = (i < L) ? i : L; fprintf(outfile, "%d %d\n", i - now % tmp, i + 1); } } Pascal 版本: procedure Uncompress(var infile, outfile : text); var N, k, L, i, now, A, B, Q, tmp : longint; begin read(infile, N, k, L, now, A, B, Q); writeln(outfile, N, ‘ ‘, k); for i := 1 to N - 1 do begin now := (now * A + B) mod Q; if i < L then tmp := i else tmp := L; writeln(outfile, i - now mod tmp, ‘ ‘, i + 1); end; end; 下面給出一個具體的例子。civiliazation_compressed.in 表示壓縮的輸入文件, civilization.in 表示解壓縮後的輸入文件。 civilization_compressed.in 7 26 4 29643 2347 5431 54209 civilization.in 7 26 1 2 2 3 2 4 3 5 4 6 5 7



2016.2.19重設時限為10s

http://blog.csdn.net/qq_33229466/article/details/73065525
#include<iostream>
#include<cstring>
#include<cstdio>
#define maxn 50010
#define mod 10007
using namespace std;
int n,m,num,L,now,A,B,Q,head[maxn],f[maxn][155],g[maxn][155],s[155][155],fac[155];
struct node{int to,pre;}e[maxn*2];
void Insert(int from,int to){
    e[++num].to=to;
    e[num].pre=head[from];
    head[from]=num;
}
void ad(int &x,int y){x+=y;if(x>=mod)x-=mod;}
void dl(int &x,int y){x-=y;if(x<mod)x+=mod;}
void dfs1(int x,int fa){
    f[x][0]=1;
    for(int i=head[x];i;i=e[i].pre){
        int to=e[i].to;
        if(to==fa)continue;
        dfs1(to,x);ad(f[x][0],f[to][0]);
        for(int i=1;i<=m;i++)ad(f[x][i],(f[to][i]+f[to][i-1])%mod);
    }
}
void dfs2(int x,int fa){
    if(fa){
        g[x][0]=n-f[x][0];
        for(int i=1;i<=m;i++){
            ad(g[x][i],((g[fa][i]+g[fa][i-1]+f[fa][i]+f[fa][i-1]-f[x][i]-(f[x][i-1]<<1))%mod+mod)%mod);  
            if(i>1)dl(g[x][i],f[x][i-2]);
        }
    }
    for(int i=head[x];i;i=e[i].pre){
        int to=e[i].to;
        if(to!=fa)dfs2(to,x);
    }
}
int main(){
    scanf("%d%d%d%d%d%d%d",&n,&m,&L,&now,&A,&B,&Q);
    int x,y,tmp;
    for(int i=1;i<n;i++){
        now=(now*A+B)%Q;tmp=min(i,L);
        x=i-now%tmp;y=i+1;
        Insert(x,y);Insert(y,x);
    }
    s[0][0]=1;
    for(int i=1;i<=m;i++)
        for(int j=1;j<=i;j++)
            s[i][j]=(s[i-1][j]*j+s[i-1][j-1])%mod;
    fac[1]=1;
    for(int i=2;i<=m;i++)fac[i]=fac[i-1]*i%mod;
    dfs1(1,0);dfs2(1,0);
    for(int i=1;i<=n;i++){
        x=0;
        for(int j=1;j<=m;j++)ad(x,s[m][j]*fac[j]%mod*(f[i][j]+g[i][j])%mod);
        printf("%d\n",x);
    }
    return 0;
}

bzoj 2159: Crash 的文明世界