2014 Asia AnShan Regional Contest (dp)
来源:互联网 发布:数据库层次模型 编辑:程序博客网 时间:2024/04/29 08:55
Hatsune Miku
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 705 Accepted Submission(s): 506
Problem Description
Hatsune Miku is a popular virtual singer. It is very popular in both Japan and China. Basically it is a computer software that allows you to compose a song on your own using the vocal package.
Today you want to compose a song, which is just a sequence of notes. There are only m different notes provided in the package. And you want to make a song with n notes.
Also, you know that there is a system to evaluate the beautifulness of a song. For each two consecutive notes a and b, if b comes after a, then the beautifulness for these two notes is evaluated as score(a, b).
So the total beautifulness for a song consisting of notes a1, a2, . . . , an, is simply the sum of score(ai, ai+1) for 1 ≤ i ≤ n - 1.
Now, you find that at some positions, the notes have to be some specific ones, but at other positions you can decide what notes to use. You want to maximize your song’s beautifulness. What is the maximum beautifulness you can achieve?
Today you want to compose a song, which is just a sequence of notes. There are only m different notes provided in the package. And you want to make a song with n notes.
Also, you know that there is a system to evaluate the beautifulness of a song. For each two consecutive notes a and b, if b comes after a, then the beautifulness for these two notes is evaluated as score(a, b).
So the total beautifulness for a song consisting of notes a1, a2, . . . , an, is simply the sum of score(ai, ai+1) for 1 ≤ i ≤ n - 1.
Now, you find that at some positions, the notes have to be some specific ones, but at other positions you can decide what notes to use. You want to maximize your song’s beautifulness. What is the maximum beautifulness you can achieve?
Input
The first line contains an integer T (T ≤ 10), denoting the number of the test cases.
For each test case, the first line contains two integers n(1 ≤ n ≤ 100) and m(1 ≤ m ≤ 50) as mentioned above. Then m lines follow, each of them consisting of m space-separated integers, the j-th integer in the i-th line for score(i, j)( 0 ≤ score(i, j) ≤ 100). The next line contains n integers, a1, a2, . . . , an (-1 ≤ ai ≤ m, ai ≠ 0), where positive integers stand for the notes you cannot change, while negative integers are what you can replace with arbitrary notes. The notes are named from 1 to m.
For each test case, the first line contains two integers n(1 ≤ n ≤ 100) and m(1 ≤ m ≤ 50) as mentioned above. Then m lines follow, each of them consisting of m space-separated integers, the j-th integer in the i-th line for score(i, j)( 0 ≤ score(i, j) ≤ 100). The next line contains n integers, a1, a2, . . . , an (-1 ≤ ai ≤ m, ai ≠ 0), where positive integers stand for the notes you cannot change, while negative integers are what you can replace with arbitrary notes. The notes are named from 1 to m.
Output
For each test case, output the answer in one line.
Sample Input
25 383 86 7715 93 3586 92 493 3 3 1 210 536 11 68 67 2982 30 62 23 6735 29 2 22 5869 67 93 56 1142 29 73 21 19-1 -1 5 -1 4 -1 -1 -1 4 -1
Sample Output
270625
题意:n个片段,组成一段音乐,音乐的score是score(ai,a[i+1])的和,现在告诉你矩阵wi,j表示ij组合的分数,下面n个片段,如果为负数,你可以自己安排
为最大的score是多少
思路:dp[i][j]表示前i个,最后一个是j的最大得分
#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=110;int g[maxn][maxn],a[maxn];int N,M;int dp[maxn][55];int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d%d",&N,&M); for(int i=1;i<=M;i++) for(int j=1;j<=M;j++)scanf("%d",&g[i][j]); for(int i=1;i<=N;i++)scanf("%d",&a[i]); memset(dp,0,sizeof(dp)); for(int i=2;i<=N;i++) { if(a[i]>0) { if(a[i-1]>0)dp[i][a[i]]=max(dp[i][a[i]],dp[i-1][a[i-1]]+g[a[i-1]][a[i]]); else { for(int j=1;j<=M;j++) dp[i][a[i]]=max(dp[i][a[i]],dp[i-1][j]+g[j][a[i]]); } } else { if(a[i-1]>0) { for(int j=1;j<=M;j++) dp[i][j]=max(dp[i][j],dp[i-1][a[i-1]]+g[a[i-1]][j]); } else { for(int j=1;j<=M;j++) { for(int k=1;k<=M;k++) dp[i][j]=max(dp[i][j],dp[i-1][k]+g[k][j]); } } } } int ans=0; if(a[N]>0)printf("%d\n",dp[N][a[N]]); else { for(int i=1;i<=M;i++) ans=max(ans,dp[N][i]); printf("%d\n",ans); } } return 0;}
Coprime
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1103 Accepted Submission(s): 447
Problem Description
There are n people standing in a line. Each of them has a unique id number.
Now the Ragnarok is coming. We should choose 3 people to defend the evil. As a group, the 3 people should be able to communicate. They are able to communicate if and only if their id numbers are pairwise coprime or pairwise not coprime. In other words, if their id numbers are a, b, c, then they can communicate if and only if [(a, b) = (b, c) = (a, c) = 1] or [(a, b) ≠ 1 and (a, c) ≠ 1 and (b, c) ≠ 1], where (x, y) denotes the greatest common divisor of x and y.
We want to know how many 3-people-groups can be chosen from the n people.
Now the Ragnarok is coming. We should choose 3 people to defend the evil. As a group, the 3 people should be able to communicate. They are able to communicate if and only if their id numbers are pairwise coprime or pairwise not coprime. In other words, if their id numbers are a, b, c, then they can communicate if and only if [(a, b) = (b, c) = (a, c) = 1] or [(a, b) ≠ 1 and (a, c) ≠ 1 and (b, c) ≠ 1], where (x, y) denotes the greatest common divisor of x and y.
We want to know how many 3-people-groups can be chosen from the n people.
Input
The first line contains an integer T (T ≤ 5), denoting the number of the test cases.
For each test case, the first line contains an integer n(3 ≤ n ≤ 105), denoting the number of people. The next line contains n distinct integers a1, a2, . . . , an(1 ≤ ai ≤ 105) separated by a single space, where ai stands for the id number of the i-th person.
For each test case, the first line contains an integer n(3 ≤ n ≤ 105), denoting the number of people. The next line contains n distinct integers a1, a2, . . . , an(1 ≤ ai ≤ 105) separated by a single space, where ai stands for the id number of the i-th person.
Output
For each test case, output the answer in a line.
Sample Input
151 3 9 10 2
Sample Output
4
思路:设这三个数为a,b,c,现在假设a与b互质,b与c不互质,设满足这样的集合数位temp,那么答案为C(n,3)-temp/2,之所以除2是因为b作为中间的数算重了一次。 现在问题转为求解temp。 对于每个b,设与它互质的数个数为cnt1,与它不互质的个数为cnt2,显然cnt1+cnt2+1=n。 那么对于b的temp值为cnt1*cnt2,其中只需要求出cnt2即可,将b分解质因子,然后这些质因子的个数不会超过6个,显然可以状压以后容斥原理求解。 现在问题进而转化为求出一个数x有多少个数是x的倍数,这个用素数筛选可以求得,问题得以解决。
#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=100010;typedef long long LL;int N,a[maxn],num;int prime[maxn];bool is_prime[maxn];int have[maxn];void getprime(){ num=0; memset(is_prime,0,sizeof(is_prime)); is_prime[1]=1; for(int i=2;i<maxn;i++) { if(!is_prime[i]) prime[num++]=i; for(int j=0;j<num;j++) { if(i*prime[j]>maxn)break; is_prime[i*prime[j]]=1; if(i%prime[j]==0)break; } }}LL cal(int x){ vector<int> q; for(int i=0;i<num&&x>=prime[i];i++) { if(x%prime[i]==0)q.push_back(prime[i]); while(x%prime[i]==0)x/=prime[i]; if(!is_prime[x]){q.push_back(x);break;} } int len=q.size(); LL ans=0; for(int s=1;s<(1<<len);s++) { int cnt=0; int u=1; for(int i=0;i<len;i++) if(s&(1<<i)) cnt++,u*=q[i]; if(cnt&1)ans=ans+have[u]; else ans=ans-have[u]; } if(ans)ans--; return ans*(N-1-ans);}int main(){ int T; scanf("%d",&T); getprime(); while(T--) { scanf("%d",&N); for(int i=1;i<=N;i++)scanf("%d",&a[i]); memset(have,0,sizeof(have)); for(int i=1;i<=N;i++) { int x=a[i]; for(int j=1;j*j<=a[i];j++) if(a[i]%j==0) { have[j]++; if(a[i]/j!=j)have[a[i]/j]++; } } LL ans=0; for(int i=1;i<=N;i++) ans+=cal(a[i]); printf("%I64d\n",(LL)N*(N-1)*(N-2)/6-ans/2); } return 0;}
0 0
- 2014 Asia AnShan Regional Contest (dp)
- dp --- 2014 Asia AnShan Regional Contest --- HDU 5074 Hatsune Miku
- HDU 5079(2014 Asia AnShan Regional Contest J)
- 2014 Asia AnShan Regional Contest 题解
- [HDU 5079][2014 Asia AnShan Regional Contest]Square(DP套DP)
- HDU 5074 Hatsune Miku 2014 Asia AnShan Regional Contest dp(水
- hdu 5074 Hatsune Miku 2014 Asia AnShan Regional Contest
- HDU 5072 Coprime 2014 Asia AnShan Regional Contest 容斥
- HDU 5073 Galaxy 2014 Asia AnShan Regional Contest 规律题
- hdu 5073 Galaxy 2014 Asia AnShan Regional Contest
- HDU 5072 Coprime(2014 Asia AnShan Regional Contest C)
- 2014 Asia AnShan Regional Contest --- HDU 5078 Osu!
- 2014 Asia AnShan Regional Contest --- HDU 5073 Galaxy
- hdu5072 2014 Asia AnShan Regional Contest C Coprime
- 2014 Asia AnShan Regional Contest D题Galaxy(数学)(hdu5073)
- 2014 Asia anshan Regional Chat
- hdu 5072 Coprime 2014 Asia AnShan Regional Contest 单色三角形模型+容斥 好题!
- HDU5072Coprime 容斥原理+双色三角形 2014 Asia AnShan Regional Contest
- 【自绘TreeCtrl】CColumnTreeCtrl
- 【iOS开发】跨平台开发再次袭来
- 找出链表的中间结点
- JS DOM 编程艺术(第2版)读书笔记 第8章 充实文档的内容
- 【TSP】 HDU 4568 Hunter
- 2014 Asia AnShan Regional Contest (dp)
- win7和ubuntu双系统下完全删除ubuntu系统
- Lua 环境安装(Windows)
- JS DOM 编程艺术(第2版)读书笔记 第9章 CSS-DOM
- 【最大流】【HDU3572】Task Schedule
- Objective-c中的self和super
- 键值监听KVO机制
- JS DOM 编程艺术(第2版)读书笔记 第10章 用JavaScript实现动画效果
- KIF自动化框架环境搭建