BestCoder Round #69 (div.2)

A.geometry

#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

int main(){
    //freopen("in.txt","r",stdin);
    int T;scanf("%d",&T);
    while(T--){
        int x,y;
        scanf("%d%d",&x,&y);
        cout<<abs(2*x*y)<<endl;
    }
    return 0;
}

B.tree

思路:可知距离每个点最近的点必然是它本身,距离唯一。所以只需要把所有相互之间距离为0的点通过并查集缩团就好。一开始想写dfs超时了,然后换了并查集,因为脑残原因WA了几发。

#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

int par[100100];
int cnt[100100];
int find(int x){
    return par[x]=(x==par[x]?x:find(par[x]));
}

int main(){
    //freopen("in.txt","r",stdin);
    int T;scanf("%d",&T);
    while(T--){
        int n;scanf("%d",&n);
        for(int i=0;i<=n;i++){
            par[i]=i;
            cnt[i]=1;
        }
        for(int i=0;i<n-1;i++){
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            if(!w){
                par[v]=find(u);
                cnt[par[v]]+=cnt[v];
            }
        }
        for(int i=1;i<=n;i++)
            cnt[i]=cnt[par[find(i)]];
        int ans=cnt[1];
        for(int i=2;i<=n;i++)
            ans^=cnt[i];
        printf("%d\n",ans);
    }
    return 0;
}

C.graph

思路:之前做个一个类似的,随机选择图上一点出发,等概率去往与他相邻的点,求k步之后他在每个点的概率。这个题的概率转移方程跟之前那题一直,只是因为点小于20个,所以变成矩阵的形式,然后再通过矩阵快速幂计算就好了。值得一提的是快速幂的模板有问题,导致WA。还有不知道数论的一个定理。谢谢bc群里的菊苣们。

#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;
typedef long long ll;

const ll MOD=1e9+7;
const int MAXN=60; //矩阵阶数
bool G[60][60];
int tot[60];
struct Matrix{
    ll m[MAXN][MAXN];
    Matrix()
    {
        memset(m,0,sizeof(m));
        for(int i=0;i<MAXN;i++)
            m[i][i]=1;
    }
    void clear(){
        memset(m,0,sizeof(m));
    }
    void show(){
        for(int i=0;i<MAXN;i++){
            for(int j=0;j<MAXN;j++)
                cout<<m[i][j]<<" ";
            cout<<endl;
        }
    }
};

Matrix mtMul(Matrix A,Matrix B)
{
    int i,j,k,tmp;
    Matrix C;
    C.clear();
    for(int i=0;i<MAXN;i++)
        for(int j=0;j<MAXN;j++)
            for(int k=0;k<MAXN;k++)
                C.m[i][j]=(C.m[i][j]%MOD+(A.m[i][k]%MOD)*(B.m[k][j]%MOD))%MOD;
    return C;
}
Matrix mtPow(Matrix A,int k)
{
    k--;
    Matrix ans=A;
    while(k){
        if(k&1) ans=mtMul(ans,A);
        A=mtMul(A,A);
        k>>=1;
    }
    return ans;
}

ll Quick_pow(ll x,int m){
    ll sum=1;
    while(m){
        if(m&1) sum=((sum%MOD)*(x%MOD))%MOD;
        x=((x%MOD)*(x%MOD))%MOD;
        m>>=1;
    }
    return sum;
}

int main(){
    //freopen("in.txt","r",stdin);
    int n,m;
    while(cin>>n>>m){
        int u,v;
        Matrix M;
        M.clear();
        memset(G,0,sizeof(G));
        memset(tot,0,sizeof(tot));
        while(m--){
            cin>>u>>v;
            tot[u-1]++;
            G[v-1][u-1]=true;
        }
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                if(G[i][j]){
                    M.m[i][j]=((M.m[i][j])%MOD+Quick_pow(tot[j],1e9+5)%MOD)%MOD;
                }
        int Q;
        cin>>Q;

        while(Q--){
            int u,k;
            cin>>u>>k;
            Matrix ans=mtPow(M,k);
            //ans.show();
            for(int i=0;i<n;i++)
                cout<<ans.m[i][u-1]<<" ";
            cout<<endl;
        }
    }
    return 0;
}

时间: 2024-10-08 02:35:37

BestCoder Round #69 (div.2)的相关文章

BestCoder Round #69 (div.2) Baby Ming and Weight lifting(hdu 5610)

Baby Ming and Weight lifting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 681    Accepted Submission(s): 280 Problem Description Baby Ming is fond of weight lifting. He has a barbell pole(the

BestCoder Round #69 (div.2)(hdu5611)

Baby Ming and phone number Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1501    Accepted Submission(s): 399 Problem Description Baby Ming collected lots of cell phone numbers, and he wants to

计算几何(水)BestCoder Round #50 (div.2) 1002 Run

题目传送门 1 /* 2 好吧,我不是地球人,这题只要判断正方形就行了,正三角形和正五边形和正六边形都不可能(点是整数). 3 但是,如果不是整数,那么该怎么做呢?是否就此开启计算几何专题了呢 4 */ 5 /************************************************ 6 * Author :Running_Time 7 * Created Time :2015-8-8 19:54:14 8 * File Name :B.cpp 9 ************

hdu5418 BestCoder Round #52 (div.2) Victor and World ( floyd+状压dp)

Problem Description After trying hard for many years, Victor has finally received a pilot license. To have a celebration, he intends to buy himself an airplane and fly around the world. There are n countries on the earth, which are numbered from 1 to

BestCoder Round #11 (Div. 2) 前三题题解

题目链接: huangjing hdu5054 Alice and Bob 思路: 就是(x,y)在两个參考系中的表示演全然一样.那么仅仅可能在这个矩形的中点.. 题目: Alice and Bob Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 216    Accepted Submission(s): 166 Problem De

BestCoder Round #11 (Div. 2) 题解

HDOJ5054 Alice and Bob Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 302    Accepted Submission(s): 229 Problem Description Bob and Alice got separated in the Square, they agreed that if they

HDU 5651 xiaoxin juju needs help(BestCoder Round #77 (div.1)1001)

传送门 xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 861    Accepted Submission(s): 243 Problem Description As we all known, xiaoxin is a brilliant coder. He knew **palin

BestCoder Round #50 (div.2)

题目传送:BestCoder Round #50 (div.2) BC感觉越做越无语了 1001.Distribution money AC代码: #include <map> #include <set> #include <list> #include <cmath> #include <deque> #include <queue> #include <stack> #include <bitset> #

HDU 5671 Matrix (BestCoder Round #81 (div.2) 1002)

传送门 Matrix Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 311    Accepted Submission(s): 142 Problem Description There is a matrix M that has n rows and m columns (1≤n≤1000,1≤m≤1000).Then we