problem 1001 (hdu 4696)
Answers
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4696
思路:在d>0的情况下,当c[]全部为2,且d%2==1的情况下为NO,否则为yes
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 #include<stack> 7 using namespace std; 8 const int maxn=100010; 9 int T[maxn]; 10 int C[maxn]; 11 int main() 12 { 13 int n,q; 14 while(scanf("%d%d",&n,&q)!=EOF) 15 { 16 int i; 17 18 for(i=1; i<=n; i++) 19 { 20 scanf("%d",&T[i]); 21 } 22 int flag=0; 23 for(i=1; i<=n; i++) 24 { 25 scanf("%d",&C[i]); 26 if(C[i]==1) 27 flag=1; 28 } 29 int que; 30 for(i=1;i<=q;i++) 31 { 32 scanf("%d",&que); 33 if(que<=0) 34 { 35 printf("NO "); 36 continue; 37 } 38 if(flag==0&&que%2==1) 39 { 40 printf("NO "); 41 continue; 42 } 43 printf("YES "); 44 } 45 } 46 return 0; 47 }
problem 1009 (hdu 4704)
Sum
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4704
思路:可以看出结果为2^(n-1)
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 #include<stack> 7 #define LL __int64 8 using namespace std; 9 const int maxn=100010; 10 const int mod=1000000007; 11 int num[maxn]; 12 char str[maxn]; 13 LL qmod(LL a,LL n) 14 { 15 LL sp=1; 16 while(n>0) 17 { 18 if(n%2==1) 19 sp=sp*a%mod; 20 a=a*a%mod; 21 n=n>>1; 22 } 23 return sp; 24 } 25 int main() 26 { 27 while(scanf("%s",str)!=EOF) 28 { 29 int len=strlen(str); 30 int i; 31 for(i=0; i<len; i++) 32 { 33 num[i]=str[i]-'0'; 34 } 35 num[len-1]--; 36 for(i=len-1; i>=0; i--) 37 { 38 if(num[i]<0) 39 { 40 num[i]+=10; 41 num[i-1]--; 42 continue; 43 } 44 break; 45 } 46 LL sum=0; 47 for(i=0; i<len; i++) 48 { 49 sum=sum*10+num[i]; 50 sum=sum%(mod-1); 51 } 52 sum+=(mod-1); 53 printf("%I64d ",qmod(2,sum)); 54 } 55 return 0; 56 }
problem 1010 (hdu 4705)
Y
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4705
思路:以u为根节点,其子树的根为v[],则满足条件的分为以下两种情况
(1)在子树v[]中选取三个子树,每个子树任意选择一个点,组成的三个点一定满足题意
(2)在子树v[]中选取两个子树,每个子树任意选择一个点,整个树中选取除去u和其子树的所有点后剩下的点,这三个点一定满足题意
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 #include<stack> 7 #define LL __int64 8 #pragma comment(linker, "/STACK:102400000,102400000") 9 using namespace std; 10 const int maxn=100010; 11 struct node 12 { 13 int u,v; 14 int next; 15 } edge[maxn*2]; 16 int head[maxn]; 17 int num[maxn]; 18 bool vis[maxn]; 19 LL tt[maxn]; 20 int cnt; 21 int n; 22 LL sum; 23 vector<int>vec; 24 void init() 25 { 26 memset(vis,false,sizeof(vis)); 27 memset(head,-1,sizeof(head)); 28 cnt=0; 29 sum=0; 30 } 31 void add(int u,int v) 32 { 33 edge[cnt].u=u; 34 edge[cnt].v=v; 35 edge[cnt].next=head[u]; 36 head[u]=cnt++; 37 } 38 LL dfs(int u,int sn) 39 { 40 vis[u]=true; 41 int i; 42 if(vis[edge[head[u]].v]==true&&edge[head[u]].next==-1) 43 { 44 return 1; 45 } 46 int j; 47 LL s; 48 49 int nn=0; 50 for(i=head[u]; i!=-1; i=edge[i].next) 51 { 52 int v=edge[i].v; 53 if(!vis[v]) 54 { 55 s=dfs(v,sn+nn); 56 tt[sn+nn]=s; 57 nn++; 58 } 59 } 60 //printf("u=%d ",u); 61 62 LL ss=0; 63 vec.clear(); 64 sort(tt+sn,tt+sn+nn); 65 /*for(i=sn; i<sn+nn; i++) 66 { 67 printf("tt[%d]=%d ",i,tt[i]); 68 }*/ 69 //printf(" "); 70 vec.push_back(tt[sn]); 71 ss=tt[sn]; 72 num[sn]=1; 73 int xx=0; 74 for(i=sn+1; i<sn+nn; i++) 75 { 76 if(tt[i-1]==tt[i]) 77 num[sn+xx]++; 78 else 79 { 80 vec.push_back(tt[i]); 81 xx++; 82 num[sn+xx]=1; 83 } 84 ss+=tt[i]; 85 } 86 //printf("ss=%d ",ss); 87 88 int len=vec.size(); 89 /*for(i=0; i<len; i++) 90 { 91 printf("tt[i]=%d num=%d ",vec[i],num[sn+i]); 92 }*/ 93 LL w=0; 94 LL ww=0; 95 for(i=0; i<len; i++) 96 { 97 for(j=i; j<len; j++) 98 { 99 LL ii=num[sn+i]; 100 LL jj=num[sn+j]; 101 if(i==j&&ii<2) 102 continue; 103 if(i==j) 104 { 105 w+=(ii*(ii-1))/2*vec[i]*vec[i]*(n-1-ss); 106 ww+=(ii*(ii-1))/2*vec[i]*vec[i]*(ss-vec[i]-vec[j]); 107 } 108 else 109 { 110 w+=ii*jj*vec[i]*vec[j]*(n-1-ss); 111 ww+=ii*jj*vec[i]*vec[j]*(ss-vec[i]-vec[j]); 112 } 113 //printf("w2=%d ",w); 114 //printf("ww2=%d ",ww); 115 } 116 } 117 ww/=3; 118 sum+=w; 119 sum+=ww; 120 //printf("sum2=%d ",sum); 121 return ss+1; 122 } 123 int main() 124 { 125 //freopen("out.txt","r",stdin); 126 while(scanf("%d",&n)!=EOF) 127 { 128 int i; 129 int u,v; 130 init(); 131 for(i=0; i<n-1; i++) 132 { 133 scanf("%d%d",&u,&v); 134 add(u,v); 135 add(v,u); 136 } 137 dfs(1,0); 138 printf("%I64d ",sum); 139 } 140 return 0; 141 }