2014多校4--1005 The Romantic Hero (HDU 4901 )
来源:互联网 发布:防闪退软件下载 编辑:程序博客网 时间:2024/04/30 14:44
题意:
给n个数,把它们分成两份S、T,使得S中元素的下标在T中元素下标的左边,注意这里是全部在左边,使得S中元素异或值等于T中元素与值。
解法:
1.刚开始想到DP就已经比较晚了。。。刚开始队友看到1024想到了枚举最终的状态,后来复杂度太高了,而且要确定异或后再乘非常慢。
2.后来我突然发现从左往右扫每多一个数只有两种情况,要不然这个数和之前的所有异或出新的一个状态,要不就不取这个数那就是直接把之前的值copy到下一个里面
3.开两个三维数组,f1[i][j][k]表示从左往右扫到i位状态是j且用到了这一位数字(k=1)/没用到这一位(k=0)有几种
4.最后再扫一边n和状态,注意左边的值一定要用到,也就是用f1[i-1][j][1]*(f2[i][j][1]+f2[i][j][0])
坑:
(其实是我自己挖出来坑)
1.f1和f2数组要开long long。结果要不断的mod不然爆long long也可能好像,以防万一。
2.注意边界情况,n=0,1,2
3.这里就算是元素相同也算不同的set。
AC代码:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;int a[1005];long long f1[1005][1300][2];long long f2[1005][1300][2];const int mod=1e9+7;int main(){ int T; int i,j; int n; scanf("%d",&T); while (T--) { memset(f1,0,sizeof(f1)); memset(f2,0,sizeof(f2)); scanf("%d",&n); for (i=1;i<=n;i++) scanf("%d",&a[i]); //bool flag=true; if(n==2) { if(a[1]!=a[2]) { printf("0\n"); continue; } else { printf("1\n"); continue; } } if(n==1 || n==0) { printf("0\n"); continue; } f1[1][a[1]][1]=1; for(i=2;i<n;i++) { f1[i][a[i]][1]+=1; for (j=0;j<1024;j++) { f1[i][j][0]=(f1[i-1][j][0]+f1[i-1][j][1])%mod; if (f1[i-1][j][0]+f1[i-1][j][1]) f1[i][j^a[i]][1]+=((f1[i-1][j][0]+f1[i-1][j][1])%mod); } } f2[n][a[n]][1]=1; for (i=n-1;i>1;i--) { f2[i][a[i]][1]+=1; for (j=0;j<1024;j++) { f2[i][j][0]=(f2[i+1][j][0]+f2[i+1][j][1])%mod; if (f2[i+1][j][0]+f2[i+1][j][1]) f2[i][j&a[i]][1]+=((f2[i+1][j][0]+f2[i+1][j][1])%mod); } } long long ans=0; for (i=2;i<=n;i++) { for (j=0;j<1024;j++) { //if(f2[i][j][0]+f2[i][j][1]==f1[i-1][j][0]+f1[i-1][j][1]) ans=(ans+((f1[i-1][j][1]*((f2[i][j][1]+f2[i][j][0])%mod))%mod))%mod; /*if (f1[i-1][j] || f2[i][j]) { printf("i=%d j=%d\n",i,j); printf("%d %d\n",f1[i-1][j],f2[i][j]); }*/ } } printf("%I64d\n",ans); } return 0;}
0 0
- 2014多校4--1005 The Romantic Hero (HDU 4901 )
- hdu 4901 The Romantic Hero(计数dp)2014多校训练第4场1005
- hdu 4901 The Romantic Hero(计数dp)2014多校训练第4场1005
- hdu 4901 The Romantic Hero(计数dp)2014多校训练第4场1005
- HDU-4901-The Romantic Hero
- hdu 4901 The Romantic Hero
- HDU 4901 The Romantic Hero
- hdu 4901 The Romantic Hero
- hdu 4901 The Romantic Hero
- HDU--4901--The Romantic Hero
- HDU 4901 The Romantic Hero
- HDU 4901 The Romantic Hero
- HDU 4901 The Romantic Hero
- hdu 4901 The Romantic Hero
- HDU 4901 The Romantic Hero
- hdu 4901 The Romantic Hero
- HDU 4901 The Romantic Hero
- HDU 4901 The Romantic Hero
- J2EE设计模式之Dao
- 杭电ACM 2050折线分割平面----20140731
- 随机产生20个正整数存入数组a中,且每个数均在1000-9999之间(包含1000和9999)。对数组进行排序,要求按每个数的后三位的大小进行升序排列,然后取出满足此条件的前10个数放入数组b中,如果
- 单例模式
- Android NFC近场通信01----介绍
- 2014多校4--1005 The Romantic Hero (HDU 4901 )
- Manifest.permission
- hdu-1799-循环多少次?
- POJ 1562 Oil Deposits
- J2EE下传递数据的方式
- MyBatis使用
- hdoj 1071 The area 【简单数学题】
- WebLogic之eclipse安装WebLogic插件
- HDOJ 1212 Big Number