题目链接:

  http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=701

1001 :

 矩阵快速幂

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include<queue>
#include<vector>
using namespace std;
const int N = 5e5+, M = 1e2+, mod = ,inf = 1e9;
typedef long long ll;
struct Matix {
ll arr[M][M];
};
ll x,m,k,c;
Matix mul(Matix a,Matix b,ll hang ,ll lie) {
Matix ans;
memset(ans.arr,,sizeof(ans.arr));
for(int i=;i<=hang;i++) {
for(int t=;t<=lie;t++)
for(int j=;j<=lie;j++) {
ans.arr[i][t]+=(a.arr[i][j]*b.arr[j][t]);
ans.arr[i][t]%=k;
}
}
return ans;
}
Matix pow(Matix ans,Matix a,ll x) {
while(x) {
if(x&) ans=mul(ans,a,,);
a=mul(a,a,,);
x/=;
}
return ans;
}
int main(){
ll x,y,z;
int T,cas=;
scanf("%d",&T);
while(T--) {
scanf("%I64d%I64d%I64d%I64d",&x,&m,&k,&c);
Matix fir,sec;
Matix now;
now.arr[][]=x;
now.arr[][]=x;
memset(fir.arr,,sizeof(fir.arr));
sec.arr[][]=;sec.arr[][]=;
sec.arr[][]=;sec.arr[][]=;
fir.arr[][]=;
fir.arr[][]=;
fir=pow(fir,sec,m-);fir=mul(now,fir,,);
printf("Case #%d:\n",cas++);
if(fir.arr[][]==c)puts("Yes");
else puts("No");
}
return ;
}

1002:

 状态压缩DP

  我们设定dp[1<<16][16]:dp[i][j]:i为当前选取的状态并以第j个数结尾的最大值,那么答案就是 max{dp[全集][k]} k属于0到n-1

  对于dp[i][j] , i这个状态已经填了x个数,我们准备填第x+1个数时, 如果当前x+1位置必填某个数,那么 就只更新以规定的这个数结尾转移方程

  如果没有那就 枚举那么可以任意放的数来更新相应的状态及答案

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include<queue>
#include<vector>
using namespace std;
const int N = <<, M = 1e6+, mod = ,inf = 1e9;
typedef long long ll; ll dp[<<][];
int n,a[N],p[N],H[N],F[N];
int main() {
int T,cas = ;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
memset(H,,sizeof(H));
memset(F,-,sizeof(F));
int tmp = ;
for(int i=;i<n;i++) {
scanf("%d%d",&a[i],&p[i]);
if(p[i]!=-)
H[i] = ,F[p[i]] = i;
}
for(int i=;i<(<<n);i++)
for(int j=;j<n;j++) dp[i][j] = -1e18;
// for(int i=0;i<n;i++)
// for(int j=0;j<n;j++) if(i!=j&&!H[i]&&!H[j]) dp[(1<<i)|(1<<j)][j] = a[i]*a[j], dp[(1<<i)|(1<<j)][i] = a[i]*a[j];
if(F[]!=-) dp[(<<F[])][F[]] = ;
else {
for(int i=;i<n;i++) {
if(!H[i]) dp[(<<i)][i] = ;
}
}
int U = (<<n) - ;
for(int i=;i<=U;i++) {
int counts = ;
for(int j=;j<n;j++) if((<<j)&(i)) counts++;
if(F[counts]!=-) {
counts = F[counts];
for(int j=;j<n;j++) if(i&(<<j)&&counts!=j)dp[i|(<<(counts))][counts] = max(dp[i][j]+a[j]*a[counts],dp[i|(<<counts)][counts]);
}
else {
for(int k=;k<n;k++) {
if((<<k)&(i))
for(int j=;j<n;j++) {
if(!((<<j)&i)) {
dp[i|(<<j)][j] = max(dp[i|(<<j)][j],dp[i][k]+a[k]*a[j]);
}
}
}
}
}
printf("Case #%d:\n",cas++);
ll ans = -1e18;
for(int i=;i<n;i++) ans = max(dp[U][i],ans) ;
printf("%I64d\n",ans);
}
return ;
}

 1004:D Game

  区间dp

  设定dp[i][j]为i到j这个区间的最大答案,每次去选择连续不连续的2个/3个去删除,暴力转移就好

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<map>
using namespace std;
const int N = , M = , mod = 1e9 + , inf = 0x3f3f3f3f;
typedef long long ll; int dp[N][N];
ll a[N],d;
map<ll,int > mp;
int dfs(int l,int r) {
if(dp[l][r]!=-) return dp[l][r];
int& ret = dp[l][r];
ret = ;
if(l>=r) return ret; if(l+==r) {
if(mp[a[r]-a[l]]) return (ret = );
else return (ret = );
} int tmp = ;
if(mp[a[r]-a[l]]) tmp+=;// cout<<a[r]-a[l]<<endl;
if(dfs(l+,r-)==((r-)-(l+)+)) ret = tmp+((r-)-(l+)+);
if(abs(a[r]-a[l])%==&&mp[(a[r]-a[l])/])
for(int i=l+;i<r;i++) {
tmp = ;
if(a[r]-a[i]==a[i]-a[l]&&mp[a[i]-a[l]]) {
tmp = ;
}
else continue;
if(tmp+dfs(l+,i-)+dfs(i+,r-)==r-l+) {
ret = r-l+;break;
}
} for(int i=l+;i<r;i++) {
ret = max(ret,dfs(l,i)+dfs(i+,r));
}
return ret;
}
int main() {
int T,m,n;
scanf("%d",&T);
while(T--) {
mp.clear();
memset(dp,-,sizeof(dp));
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%I64d",&a[i]);
for(int i=;i<=m;i++) scanf("%I64d",&d),mp[d] = ;
printf("%d\n",dfs(,n));
}
return ;
} /*
1
7 2
1 5 8 101 59 62 201
100 3
*/

1005:

 预处理+dfs

 预处理每种长度,每种长度的答案

 再对对应长度dfs求出答案

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include<queue>
#include<vector>
using namespace std;
const int N = 1e3+, M = 1e6+, mod = ,inf = 1e9;
typedef long long ll; ll len[N],dp[N],L,R;
ll dfs(ll x){
if(x<=) return ;
if(x<=) return x;
if(x==) return ;
ll ans=;
int pos = upper_bound(len+,len+,x) - len - ;
if(len[pos]==x) return dp[pos];
if(x-len[pos]==) return dp[pos]+;
ll ret = dp[pos]+;
ll shen = x-len[pos]-;
return ret+shen - dp[pos] + dfs(len[pos] - shen);
}
int main() {
dp[] = ;
len[] = ;
for(int i=;i<=;i++) {
len[i] = len[i-]*+;
}
for(int i=;i<=;i++) {
dp[i] = dp[i-] + + len[i-] - dp[i-];
}
int T;
scanf("%d",&T);
while(T--) {
scanf("%I64d%I64d",&L,&R);
printf("%I64d\n",dfs(R)-dfs(L-));
}
return ;
}

1006

 优先队列

 首先按照拓扑,将入度为0的压入,优先位置大的放前面。。。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include<queue>
#include<vector>
using namespace std;
const int N = 5e5+, M = 1e6+, mod = ,inf = 1e9;
typedef long long ll; int k,c,x,d[N],n,m,vis[N];
vector<int >G[N]; int ans[N];
int main() {
int T;
int cas = ;
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) G[i].clear();
for(int i=;i<=n;i++) vis[i] = ,d[i] = ;
for(int i=;i<=m;i++) {
int a,b;
scanf("%d%d",&a,&b);
G[a].push_back(b);
d[b]++;
}
priority_queue<int > q; for(int i=;i<=n;i++) {
if(d[i]==) q.push(i);
}
int cnt = ;
while(!q.empty()) {
int k = q.top();
ans[++cnt] = k;
// cout<<k<<endl;
q.pop();
for(int i=;i<G[k].size();i++) {
d[G[k][i]]--;
if(d[G[k][i]]==&&!vis[G[k][i]]) {
q.push(G[k][i]); vis[G[k][i]] = ;
}
}
}
int mi = 1e9;
ll aa = ;
for(int i=;i<=cnt;i++) {
mi = min(ans[i],mi);
aa = aa + mi;
}
printf("%I64d\n",aa);
}
return ;
}

2016"百度之星" - 初赛(Astar Round2A)的更多相关文章

  1. 2016百度之星-初赛(Astar Round2A)AII X

    Problem Description F(x,m) 代表一个全是由数字x组成的m位数字.请计算,以下式子是否成立: F(x,m) mod k ≡ c Input 第一行一个整数T,表示T组数据. 每 ...

  2. 2016"百度之星" - 初赛(Astar Round2B) 1006 中位数计数

    思路:统计当前数左边比它小|大 i个人,相应右边就应该是比它大|小i个人 l数组表示左边i个人的方案 r表示右边i个人的方案 数组下标不可能是负数所以要加n //#pragma comment(lin ...

  3. 2016"百度之星" - 初赛(Astar Round2A)HDU 5695 拓扑排序+优先队列

    Gym Class Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  4. 2016百度之星 初赛2A ABEF

    只做了1001 1002 1005 1006.剩下2题可能以后补? http://acm.hdu.edu.cn/search.php?field=problem&key=2016%22%B0% ...

  5. HDU 5690:2016"百度之星" - 初赛 All X

    原文链接:https://www.dreamwings.cn/hdu5690/2657.html All X Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  6. 2016"百度之星" - 初赛(Astar Round2A)Gym Class(拓扑排序)

    Gym Class  Accepts: 849  Submissions: 4247  Time Limit: 6000/1000 MS (Java/Others)  Memory Limit: 65 ...

  7. 2016"百度之星" - 初赛(Astar Round2A)All X(数学 矩阵)

    All X  Accepts: 1281  Submissions: 7580  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536 ...

  8. 2016"百度之星" - 初赛(Astar Round2A) 1004 D Game 区间DP

    D Game Problem Description   众所周知,度度熊喜欢的字符只有两个:B 和D. 今天,它发明了一个游戏:D游戏. 度度熊的英文并不是很高明,所以这里的D,没什么高深的含义,只 ...

  9. 2016"百度之星" - 初赛(Astar Round2A)1002 / HDU 5691 状态压缩DP

    Sitting in Line Problem Description   度度熊是他同时代中最伟大的数学家,一切数字都要听命于他.现在,又到了度度熊和他的数字仆人们玩排排坐游戏的时候了.游戏的规则十 ...

随机推荐

  1. Ueditor配置及在项目中的使用

    引言 上篇中简单介绍了Ueditor的两种定制方式,想了解的请戳这里:Ueditor的两种定制方式.在项目中,Ueditor该怎么使用更方便呢?很容易让人想到将ueditor放入用户控件页,可以拖到需 ...

  2. centos安装软件Error: Cannot find a valid baseurl for repo: base

    今天使用yum安装软件,出现下面的提示: Loaded plugins: fastestmirror, refresh-packagekit, securityLoading mirror speed ...

  3. 构建seajs业务模块之grunt VS spm build

    在最开始,我并不知道grunt可以构建CMD模块.(以下spm指代spm build) 当时正困惑于如何用spm方便的构建业务模块,后来看到@twinstony (感谢@twinstony的分享)使用 ...

  4. 读<你必须知道的.NET>IL指令笔记

    IL指令笔记: 1.newObj和initObj MSDN解释:newObj用于分配和初始化对象,而initObj用户初始化值类型 newObj解释: (1):从托管堆分配指定类型所需要的全部内存空间 ...

  5. easyui 删除行bug

    easyui删除行,出现了bug.(经常使用这个框架的人几乎都会遇到) 我也非常纳闷,今天特地试了很久. 最后发现,如果是自己datagrid('getRows') 然后迭代出来,最后算出行号,可以成 ...

  6. mac安装软件管家homebrew

    http://www.iwangzheng.com/ 1.简介 众所周知,Mac的操作系统是基于Unix的,在这个系统上大家可以安装一些专门为mac定制开发的软件.这就带来了一个问题,手工编译每个软件 ...

  7. CUDA 6.5 && VS2013 && Win7:创建CUDA项目

    运行环境: Win7+VS2013+CUDA6.5 1.创建win32空项目 2.右键项目解决方案-->生成项目依赖项-->生成自定义 3.右键项目解决方案-->属性-->配置 ...

  8. Android 中 设置TextView垂直滚动

    布局文件 android:scrollbars="vertical" android:singleLine="false" 代码文件 ctl_tv_conten ...

  9. python安装问题

    安装MinGW之后 出现.. 解决方案 ================

  10. 利用jQuery.validate异步验证用户名是否存在

    转:http://www.cnblogs.com/linzheng/archive/2010/10/14/1851781.html HTML头部引用: <script type="te ...