码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4901

时间:2014-08-01 04:40:21      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   os   strong   

The Romantic Hero

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Problem Description
There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and can’t refuse any request from the devil. Also, this devil is looking like a very cute Loli.

You may wonder why this country has such an interesting tradition? It has a very long story, but I won‘t tell you :).

Let us continue, the party princess‘s knight win the algorithm contest. When the devil hears about that, she decided to take some action.

But before that, there is another party arose recently, the ‘MengMengDa‘ party, everyone in this party feel everything is ‘MengMengDa‘ and acts like a ‘MengMengDa‘ guy.

While they are very pleased about that, it brings many people in this kingdom troubles. So they decided to stop them.

Our hero z*p come again, actually he is very good at Algorithm contest, so he invites the leader of the ‘MengMengda‘ party xiaod*o to compete in an algorithm contest.

As z*p is both handsome and talkative, he has many girl friends to deal with, on the contest day, he find he has 3 dating to complete and have no time to compete, so he let you to solve the problems for him.

And the easiest problem in this contest is like that:

There is n number a_1,a_2,...,a_n on the line. You can choose two set S(a_s1,a_s2,..,a_sk) and T(a_t1,a_t2,...,a_tm). Each element in S should be at the left of every element in T.(si < tj for all i,j). S and T shouldn‘t be empty.

And what we want is the bitwise XOR of each element in S is equal to the bitwise AND of each element in T.

How many ways are there to choose such two sets? You should output the result modulo 10^9+7.
 

 

Input
The first line contains an integer T, denoting the number of the test cases.
For each test case, the first line contains a integers n.
The next line contains n integers a_1,a_2,...,a_n which are separated by a single space.

n<=10^3, 0 <= a_i <1024, T<=20.
 

 

Output
For each test case, output the result in one line.
 

 

Sample Input
2 3 1 2 3 4 1 2 3 3
 

 

Sample Output
1 4
 
 
虽然题目递推方程简单,但实现细节总是不时出现差错。
 
f1[i][j]表示i及其左边的数通过异或得到j的总方案数
f2[i][j]表示i及其右边的数通过and得到j的总方案数
而如果单纯ans=sigama(f1[i][j]*f2[i+1][j])会造成重复计算
  因此开一新数组f3[i][j]表示i及其右边的数中,取到i并且得到j的总方案数,就可以避免重复计算。因为取到i能够保证其唯一性。
 
bubuko.com,布布扣
#include <cstdio>
#include <cstring>
int n,q,a[100005],x[100005],p,l[100005],r[100005],t[100005];
int tree[1000005];
void build(int l, int r, int rt)
{
    tree[rt]=-1;
    if(l==r){
        tree[rt]=0;
        return;
    }
    int m=(l+r)/2;
    build(l,m,rt*2);
    build(m+1,r,rt*2+1);
}
void update(int x, int y, int z, int l, int r, int rt)
{
    if(x<=l&&y>=r){
        tree[rt]=z;
        return;
    }
    if(tree[rt]!=-1) {
        tree[rt*2]=tree[rt];
        tree[rt*2+1]=tree[rt];
        tree[rt]=-1;
    }
    int m=(l+r)/2;
    if(x<=m) update(x,y,z,l,m,rt*2);
    if(y>m) update(x,y,z,m+1,r,rt*2+1);
}
int query(int k, int l, int r, int rt)
{
    if(tree[rt]!=-1) return tree[rt];
    int m=(l+r)/2;
    if(k<=m) return(query(k,l,m,rt*2));
    else return(query(k,m+1,r,rt*2+1));
}
int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--){
        memset(tree,-1,sizeof(tree));
        scanf("%d",&n);
        build(1,n,1);
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        scanf("%d",&q);
        for(int i=1; i<=q; i++)
        {
            scanf("%d%d%d%d",&t[i],&l[i],&r[i],&x[i]);
            if(t[i]==1) update(l[i],r[i],i,1,n,1);
        }
        for(int i=1; i<=n; i++){
            p=query(i,1,n,1);
            if(p) a[i]=x[p];
            for(int j=p+1; j<=q; j++)
            if(t[j]==2&&l[j]<=i&&r[j]>=i){
                if(a[i]>x[j]){
                    int aa=a[i],bb=x[j],t=aa%bb;
                    while(t!=0){
                        aa=bb;
                        bb=t;
                        t=aa%bb;
                    }
                    a[i]=bb;
                }
            }
        }
        for(int i=1; i<=n; i++)
            printf("%d ",a[i]);
        printf("\n");
    }
}
View Code

 

总体上来说,这类题目实现时细节很重要,需要悉心考量,切忌急躁。

 

HDU 4901,布布扣,bubuko.com

HDU 4901

标签:des   style   blog   http   color   java   os   strong   

原文地址:http://www.cnblogs.com/Mathics/p/3883790.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!