喵哈哈村的魔法考试 Round #7 (Div.2) 题解

喵哈哈村的魔法考试 Round #7 (Div.2)

注意!后四道题来自于周日的hihocoder offer收割赛第九场。

我建了个群:欢迎加入qscoj交流群,群号码:540667432

大概作为该oj的讨论吧,未来应该会上线一个bbs的。

喵哈哈村的七十六

签到题,直接for一遍判断就好了嘛

#include<bits/stdc++.h>
using namespace std;
int n,a;
int main(){
    while(cin>>n>>a){
        int ans = 0;
        for(int i=0;i<n;i++){
            int b,c;
            cin>>b>>c;
            if(a>=b)ans=max(ans,c);
        }
        cout<<ans<<endl;
    }
}

喵哈哈村的麦克雷

对于这道题,你需要知道bfs。

把0压进队列里面,然后BFS就行了。

bfs找到的,就一定是最近的路。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 805;
string s[maxn];
int mp[maxn][maxn];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int n,m;
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        memset(mp,-1,sizeof(mp));
        for(int i=0;i<n;i++)
            cin>>s[i];
        queue<int> QX,QY;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(s[i][j]=='0'){
                    mp[i][j]=0;
                    QX.push(i);
                    QY.push(j);
                }
            }
        }
        while(!QX.empty()){
            int nx = QX.front();
            int ny = QY.front();
            QX.pop();
            QY.pop();
            for(int i=0;i<4;i++){
                int nex=nx+dx[i];
                int ney=ny+dy[i];
                if(nex<0||nex>=n)continue;
                if(ney<0||ney>=m)continue;
                if(mp[nex][ney]!=-1)continue;
                mp[nex][ney]=mp[nx][ny]+1;
                QX.push(nex);
                QY.push(ney);
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cout<<mp[i][j]<<" ";
            }
            cout<<endl;
        }
    }
}

喵哈哈村的Dva

这道题实际上是模拟题,考虑的情况有点多。

我这里给一个我的做法:

手写一个cal()函数,表示1970年到这一天需要多少秒,然后两者相减,就是中间的答案。

然后就是讨论+特判了……

#include<bits/stdc++.h>
using namespace std;

string s1,s2,s3,s4;
int YYYY1,m1,d1,h1,f1,mi1,y2,m2,d2,h2,f2,mi2;
int V1[50]={1972,1973,1974,1975,1976,1977,1978,1979,1987,1989,1990,1995,1998,2005,2008,2016};//16
int V2[50]={1972,1981,1982,1983,1985,1992,1993,1994,1997,2012,2015};//11
int M[12]={31,28,31,30,31,30,31,31,30,31,30,31};
void gettime(){
    YYYY1=0,m1=0,d1=0,h1=0,f1=0,mi1=0,y2=0,m2=0,d2=0,h2=0,f2=0,mi2=0;
    for(int i=0;i<4;i++){
        YYYY1=YYYY1*10+(s1[i]-'0');
        y2=y2*10+(s3[i]-'0');
    }
    for(int i=5;i<7;i++){
        m1=m1*10+(s1[i]-'0');
        m2=m2*10+(s3[i]-'0');
    }
    for(int i=8;i<10;i++){
        d1=d1*10+(s1[i]-'0');
        d2=d2*10+(s3[i]-'0');
    }
    for(int i=0;i<2;i++){
        h1=h1*10+(s2[i]-'0');
        h2=h2*10+(s4[i]-'0');
    }
    for(int i=3;i<5;i++){
        f1=f1*10+(s2[i]-'0');
        f2=f2*10+(s4[i]-'0');
    }
    for(int i=6;i<8;i++){
        mi1=mi1*10+(s2[i]-'0');
        mi2=mi2*10+(s4[i]-'0');
    }
}
bool check(int p)
{
    if(p%400==0)return 1;
    if(p%4==0&&p%100!=0)return 1;
    return 0;
}
int check2(int y){
    int tmp = 0;
    for(int i=0;i<16;i++)
        if(V1[i]==y)tmp++;
    for(int i=0;i<11;i++)
        if(V2[i]==y)tmp++;
    return tmp;
}
int check6(int y){
    for(int i=0;i<11;i++)
        if(V2[i]==y)return 1;
    return 0;
}
int check12(int y){
    for(int i=0;i<16;i++)
        if(V1[i]==y)return 1;
    return 0;
}
long long solve(int y,int m,int d,int h,int f,int mi){
    long long tmp = 0;
    for(int i=1970;i<y;i++){
        if(check(i))tmp+=1ll*366*24*60*60;
        else tmp+=1ll*365*24*60*60;
        tmp+=check2(i);
    }
    for(int j=0;j<m-1;j++){
        if(j==1&&check(y))tmp+=1ll*29*24*60*60;
        else tmp+=1ll*M[j]*24*60*60;
        if(j==5&&check6(y))tmp++;
    }
    for(int k=0;k<d-1;k++){
        tmp+=24*60*60;
    }
    for(int i=0;i<h;i++){
        tmp+=60*60;
    }
    for(int i=0;i<f;i++)
        tmp+=60;
    tmp+=mi;
    return tmp;
}
int main(){
    while(cin>>s1>>s2>>s3>>s4){
        gettime();
        long long ans=solve(y2,m2,d2,h2,f2,mi2)-solve(YYYY1,m1,d1,h1,f1,mi1);
        cout<<ans<<endl;
    }
}

喵哈哈村的卢西奥

两种情况:

第一种情况,有两个是子树,这个东西用启发式合并/dp去维护一下当前sz[x]=all/3的个数就好了。

第二种情况,只有一个是子树,那么第二段显然是包含了第一段的,那么就存在一个的size=2all/3,一个sz=all/3的情况,统计一下就好了。

dfs处理一下就好了,具体看代码:

#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <string>
#include <cstdio>
#include <vector>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

#include <string>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <fstream>
// #include <unordered_set>

using namespace std;

#define FF first
#define SS second
#define MP make_pair
#define PB push_back
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define FOR(i, n, m) for(int i = n; i <= m; i++)
#define REP(i, n, m) for(int i = n; i >= m; i--)
#define ll long long

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;

/****************************define***************************************/
const int mod = 1e9 + 7;
const int maxn = 100010;


int n;
int v[maxn], sz[maxn], p[maxn];
vector<int> nd[maxn];
int s;
int cn = 0, cn2 = 0;
ll ans = 0;

void Dfs_sz(int u){
        sz[u] += v[u];
        int t = nd[u].size();
        FOR(i, 0, t-1) {
            Dfs_sz(nd[u][i]);
            sz[u] += sz[nd[u][i]];
            }
        }
void Dfs(int u){
        //printf("node %d. ans %d. cn %d. cn2 %d. sz %d\n", u, ans, cn, cn2, sz[u]);
        if(sz[u] == s/3) {
            ans += cn + cn2;
            //printf("node %d. ans %d\n", u, ans);
            }
        if(p[u] != 0 && sz[u] == s*2/3) cn2++;
        int t = nd[u].size();
        FOR(i, 0, t-1){
            Dfs(nd[u][i]);
            }
        if(sz[u] == s/3) cn++;
        if(p[u] != 0 && sz[u] == s*2/3) cn2--;
        }

int main(){
        int t;
        cin >> t;
        while(t--){
            cn = 0, cn2 = 0;
            s = 0, ans = 0;
            FOR(i, 0, maxn-1) nd[i].clear();
            memset(sz, 0, sizeof(sz));

            int n, root=-1;
            cin >> n;
            FOR(i, 1, n) {
                cin >> v[i] >> p[i];
                s += v[i];
                nd[p[i]].PB(i);
                if(p[i] == 0) root=i;
                }
            if(s % 3 != 0) cout << 0 << endl;
            else{
                Dfs_sz(root);
                Dfs(root);
                printf("%lld\n", ans);
                }
            }
        return 0;
        }

喵哈哈村的小美

数学题,杨氏矩阵,百度钩子公式有惊喜。

如果不会的话,其实暴力打表+oeis也能做出来,一维一维的去查就好了。

大力推荐一个网站 oeis.org!!!!!!!!!

如果不知道的话,你就out了。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
const int mod = 1e9+7;
const int Maxn = 1e6+6;
int mp[maxn][maxn];
bool check(int n,int m){
    int flag = 1;
    for(int i=0;i<n;i++){
        for(int j=1;j<m;j++){
            if(mp[i][j]<mp[i][j-1])flag=0;
        }
    }
    for(int i=1;i<n;i++){
        for(int j=0;j<m;j++){
            if(mp[i][j]<mp[i-1][j])flag=0;
        }
    }
    return flag;
}
int gcd(int a, int b, int& x, int& y) {
    if (!a) {
        x = 0, y = 1;
        return b;
    }
    int xx, yy, g = gcd(b % a, a, xx, yy);
    x = yy - b / a * xx;
    y = xx;
    return g;
}

inline int normal(int n) {
    n %= mod;
    (n < 0) && (n += mod);
    return n;
}

inline int inv(int a) {
    int x, y;
    assert(gcd(a, mod, x, y) == 1);
    return normal(x);
}

inline int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
inline int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; }
inline int mul(int a, int b) { return int(a * 1ll * b % mod); }
inline int _div(int a, int b) { return mul(a, inv(b)); }

long long p[Maxn];
long long solve2(long long x){
    //(2n)!/(n!(n+1)!)
    return _div(p[2*x],mul(p[x],p[x+1]));
}
long long solve3(long long x){
    //2*(3*n)!/(n!*(n+1)!*(n+2)!);
    return _div(mul(2,p[3*x]),mul(p[x],mul(p[x+1],p[x+2])));
}

int main(){
    p[0]=1;
    for(int i=1;i<Maxn;i++){
        p[i]=p[i-1]*i%mod;
    }
    int n,m;
    while(cin>>n>>m){
    if(n==1){
        cout<<"1"<<endl;
    }
    else if(n==2){
        cout<<solve2(m)<<endl;
    }else{
        cout<<solve3(m)<<endl;
    }
    }
    /*
    vector<int> V;
    for(int i=0;i<n*m;i++)
        V.push_back(i);
    int ans = 0;
    do{
        int tot = 0;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                mp[i][j]=V[tot++];
            }
        }
        if(check(n,m))ans++;
    }while(next_permutation(V.begin(),V.end()));
    cout<<solve3(m)<<endl;
    cout<<ans<<endl;
    */
}
posted @ 2017-03-14 16:52  qscqesze  阅读(448)  评论(0编辑  收藏  举报