hdu 4810 2013南京区域赛 杨辉三角组合数+容斥原理思维+找规律

来源:互联网 发布:什么叫大数据思维 编辑:程序博客网 时间:2024/05/10 05:32

Wall Painting

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2902    Accepted Submission(s): 940


Problem Description
Ms.Fang loves painting very much. She paints GFW(Great Funny Wall) every day. Every day before painting, she produces a wonderful color of pigments by mixing water and some bags of pigments. On the K-th day, she will select K specific bags of pigments and mix them to get a color of pigments which she will use that day. When she mixes a bag of pigments with color A and a bag of pigments with color B, she will get pigments with color A xor B.
When she mixes two bags of pigments with the same color, she will get color zero for some strange reasons. Now, her husband Mr.Fang has no idea about which K bags of pigments Ms.Fang will select on the K-th day. He wonders the sum of the colors Ms.Fang will get with different plans.

For example, assume n = 3, K = 2 and three bags of pigments with color 2, 1, 2. She can get color 3, 3, 0 with 3 different plans. In this instance, the answer Mr.Fang wants to get on the second day is 3 + 3 + 0 = 6.
Mr.Fang is so busy that he doesn’t want to spend too much time on it. Can you help him?
You should tell Mr.Fang the answer from the first day to the n-th day.
 

Input
There are several test cases, please process till EOF.
For each test case, the first line contains a single integer N(1 <= N <= 103).The second line contains N integers. The i-th integer represents the color of the pigments in the i-th bag.
 

Output
For each test case, output N integers in a line representing the answers(mod 106 +3) from the first day to the n-th day.
 

Sample Input
41 2 10 1
 

Sample Output
14 36 30 8
 

Source
2013ACM/ICPC亚洲区南京站现场赛——题目重现



题意:

给出n个数字,代表不同的颜色染料

第一天取一种染料,第二天取两种染料,,,,第n天取n种染料

第x天就要取x种染料异或(混合)得到新的染料 y

求解第x天有几种方式,将所有方式的 y 相加得到的就是答案

题解:

因为n太大,不能进行简单的容斥原理,需要找规律

将样例给的数字分解成二进制:

1:  0 0 0 1

2:  0 0 1 0

10:  1 0 1 0

1:  0 0 0 1

第二天的时候;

因为两个数字异或值==两个数字化为二进制各个位置异或和

第一位c【1】【1】*c【3】【1】*(1<<3)==24

第二位c【0】【1】*c【3】【1】*(1<<2)==0

第三位c【2】【1】*c【2】【1】*(1<<1)==8

第四位c【2】【1】*c【2】【1】*(1<<0)==4

总和为36

由此可求得其他答案



杨辉三角来打表组合数

#define NUM 1005LL c[NUM][NUM];void init(){    c[0][0]=c[1][0]=c[1][1]=1;    for(int i=2;i<NUM;i++){        c[i][0]=1;        for(int j=1;j<=i;j++)            c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;    }}


#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define MAXN 3000#define mod 1000003#define LL long longLL n,pos;int graph[MAXN][40];#define NUM 1005LL c[NUM][NUM];void init(){    c[0][0]=c[1][0]=c[1][1]=1;    for(int i=2;i<NUM;i++){        c[i][0]=1;        for(int j=1;j<=i;j++)            c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;    }}void devide(int k,LL num){    pos=0;    while(num)    {        graph[k][pos]=num%2;        graph[n][pos]+=graph[k][pos];        pos++;        num/=2;    }}int main(){    init();    //freopen("in.txt","r",stdin);    while(scanf("%lld",&n)!=EOF)    {        LL temp,ans;        memset(graph,0,sizeof(graph));        for(int i=0;i<n;i++){            scanf("%lld",&temp);            devide(i,temp);        }        for(int i=1;i<=n;i++){            ans=0;            for(int k=0;k<33;k++){                int t1=graph[n][k];                int t0=n-t1;                for(int j=1;j<=t1&&j<=i;j+=2){                    if(i-j>=0&&i-j<=t0){                        LL temp=(1<<k)%mod;                        LL num=c[t1][j]*c[t0][i-j]%mod;                        ans=(ans+(temp*num)%mod)%mod;                    }                }            }            printf("%lld",ans);            printf("%c",i==n?'\n':' ');        }    }    return 0;}


0 0
原创粉丝点击