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

2016"百度之星" - 初赛(Astar Round2A)

时间:2016-05-21 18:56:31      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

题目链接 :http://acm.hdu.edu.cn/search.php?field=problem&key=2016%26quot%3B%B0%D9%B6%C8%D6%AE%D0%C7%26quot%3B+-+%B3%F5%C8%FC%A3%A8Astar+Round2A%A3%A9&source=1&searchmode=source

1001 :

 矩阵快速幂

技术分享
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include<queue>
#include<vector>
using namespace std;
const int N = 5e5+20, M = 1e2+10, mod = 1000000007,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,0,sizeof(ans.arr));
    for(int i=1;i<=hang;i++) {
      for(int t=1;t<=lie;t++)
        for(int j=1;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&1) ans=mul(ans,a,2,2);
        a=mul(a,a,2,2);
        x/=2;
    }
    return ans;
}
int main(){
    ll x,y,z;
    int T,cas=1;
    scanf("%d",&T);
    while(T--) {
        scanf("%I64d%I64d%I64d%I64d",&x,&m,&k,&c);
        Matix fir,sec;
        Matix now;
        now.arr[1][1]=x;
        now.arr[1][2]=x;
        memset(fir.arr,0,sizeof(fir.arr));
        sec.arr[1][1]=10;sec.arr[1][2]=0;
        sec.arr[2][1]=1;sec.arr[2][2]=1;
        fir.arr[1][1]=1;
        fir.arr[2][2]=1;
        fir=pow(fir,sec,m-1);fir=mul(now,fir,2,2);
        printf("Case #%d:\n",cas++);
        if(fir.arr[1][1]==c)puts("Yes");
        else puts("No");
    }
    return 0;
}
View Code

1005:

 预处理+dfs

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

 再对对应长度dfs求出答案

技术分享
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include<queue>
#include<vector>
using namespace std;
const int N = 1e3+20, M = 1e6+10, mod = 1000000007,inf = 1e9;
typedef long long ll;

ll len[N],dp[N],L,R;
ll dfs(ll x){
    if(x<=0) return 0;
    if(x<=2) return x;
    if(x==3) return 2;
    ll ans=0;
    int pos = upper_bound(len+1,len+62,x) - len - 1;
    if(len[pos]==x) return dp[pos];
    if(x-len[pos]==1) return dp[pos]+1;
    ll ret = dp[pos]+1;
    ll shen = x-len[pos]-1;
    return ret+shen - dp[pos] + dfs(len[pos] - shen);
}
int main() {
    dp[1] = 1;
    len[1] = 1;
    for(int i=2;i<=61;i++) {
        len[i] = len[i-1]*2+1;
    }
    for(int i=2;i<=61;i++) {
        dp[i] = dp[i-1] + 1 + len[i-1] - dp[i-1];
    }
    int T;
    scanf("%d",&T);
    while(T--) {
        scanf("%I64d%I64d",&L,&R);
        printf("%I64d\n",dfs(R)-dfs(L-1));
    }
    return 0;
}
View Code

1006

 优先队列

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

技术分享
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include<queue>
#include<vector>
using namespace std;
const int N = 5e5+20, M = 1e6+10, mod = 1000000007,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 = 1;
    scanf("%d",&T);
    while(T--) {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) G[i].clear();
        for(int i=1;i<=n;i++) vis[i] = 0,d[i] =  0;
        for(int i=1;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=1;i<=n;i++) {
            if(d[i]==0) q.push(i);
        }
        int cnt =0 ;
        while(!q.empty()) {
            int k  = q.top();
            ans[++cnt] = k;
          //  cout<<k<<endl;
            q.pop();
            for(int i=0;i<G[k].size();i++) {
                d[G[k][i]]--;
                if(d[G[k][i]]==0&&!vis[G[k][i]]) {
                    q.push(G[k][i]);

                    vis[G[k][i]] = 1;
                }
            }
        }
        int mi = 1e9;
        ll aa = 0;
        for(int i=1;i<=cnt;i++) {
            mi = min(ans[i],mi);
            aa  = aa + mi;
        }
        printf("%I64d\n",aa);
    }
    return 0;
}
View Code

 

2016"百度之星" - 初赛(Astar Round2A)

标签:

原文地址:http://www.cnblogs.com/zxhl/p/5515232.html

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