2017 Multi-University Training Contest - Team 1 (5/12)

官方题解

1001. Add More Zero

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-8
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=1e5+10,M=1e6+10,inf=1e9+10;
const LL INF=1e18+10,mod=1e9+7;

int main()
{
    int n,cas=1;
    while(~scanf("%d",&n))
    {
        double x=1.0*log(2)/log(10)*n;
        int ans=int(x);
        printf("Case #%d: %d\n",cas++,ans);
    }
    return 0;
}
1001

 

1002. Balala Power!

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-8
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=1e5+1000,M=1e6+10,inf=1e9+10;
const LL INF=1e18+10,mod=1e9+7;

char a[N];
int num[N][27],flag[30];
struct is
{
    string x;
    int pos;
    bool operator <(const is &p)const
    {
        if(x.size()==p.x.size())
        return x<p.x;
        return x.size()<p.x.size();
    }
}c[30];

int main()
{
    int n,cas=1;
    while(~scanf("%d",&n))
    {
        memset(num,0,sizeof(num));
        memset(flag,0,sizeof(flag));
        for(int i=1;i<=n;i++)
        {
            scanf("%s",a);
            int len=strlen(a);
            if(len>1)flag[a[0]-'a']=1;
            for(int j=len-1,k=0;j>=0;j--,k++)
            {
                num[k][a[j]-'a']++;
            }
        }
        for(int j=0;j<26;j++)
        for(int i=0;i<=100100;i++)
        {
            if(num[i][j]>=26)
            {
                num[i+1][j]+=num[i][j]/26;
                num[i][j]%=26;
            }
        }
        for(int i=0;i<26;i++)
        {
            c[i].x="";
            c[i].pos=i;
            for(int j=100100;j>=0;j--)
            {
                if(num[j][i]!=0)
                {
                    for(int k=j;k>=0;k--)
                        c[i].x+=(char)(num[k][i]+'0');
                    break;
                }
            }
        }
        sort(c,c+26);
        LL fuck=1;
        LL ans=0;
        int now=-1;
        for(int i=0;i<=25;i++)
        if(flag[c[i].pos]==0)
        {
            now=i;
            break;
        }
        for(int i=0;i<=25;i++,fuck++)
        {
            if(i==now){
                    fuck--;
                    continue;
            }
            LL base=1,out=0;
            for(int j=0;j<=100101;j++)
            {
                out=(out+(1LL*num[j][c[i].pos])*base)%mod;
                base=(1LL*base*26)%mod;
            }
            ans=(ans+out*fuck)%mod;
        }
        printf("Case #%d: %lld\n",cas++,ans);
    }
    return 0;
}
View Code

 

1003. Colorful Tree

树分治写法

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
typedef unsigned long long ULL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 2e5+10, M = 1e3+20,inf = 2e9,mod = 1000000007;

int head[N],vis[N],f[N],siz[N],n,t = 1,allnode,root;
struct edge{int to,next;}e[N * 4];
void add(int u,int v) {e[t].next=head[u];e[t].to=v;head[u]=t++;}
void getroot(int u,int fa) {
        f[u] = 0;
        siz[u] =  1;
        for(int i = head[u]; i; i = e[i].next) {
            int to = e[i].to;
            if(vis[to] || to == fa) continue;
            getroot(to,u);
            siz[u] += siz[to];
            f[u] = max(f[u],siz[to]);
        }
        f[u] = max(f[u], allnode - siz[u]);
        if(f[u] < f[root]) root = u;
}
int cnt[N],c[N],best[N],dep[N];
int vis2[N],fuck,vis3[N],cnt3[N];
LL ans = 0;
inline void dfs3(int u,int f) {
    vis2[c[u]] = 0;
    vis3[c[u]] = 0;
    cnt3[c[u]] = 0;
    for(int i = head[u]; i; i = e[i].next) {
        int to = e[i].to;
        if(vis[to] || to == f) continue;
        dfs3(to,u);
    }
}
inline LL dfs2(int u,int f,int now) {
    LL ret  = 0;
    if((!vis2[c[u]]) && fuck!=c[u])
        ret += 1LL*siz[u] * (allnode - now - cnt[c[u]]),
        cnt3[c[u]] += siz[u];
    vis2[c[u]]++;
    for(int i = head[u]; i; i = e[i].next) {
        int to = e[i].to;
        if(vis[to] || to == f) continue;
        ret += dfs2(to,u,now);
    }
    vis2[c[u]]--;
    return ret;
}
inline void make_fail(int u,int f) {
    if(!vis3[c[u]])
        cnt[c[u]] += cnt3[c[u]],
        vis3[c[u]] = 1;
    for(int i = head[u]; i; i = e[i].next) {
        int to = e[i].to;
        if(vis[to] || to == f) continue;
        make_fail(to,u);
    }
}
inline LL cal(int u) {
    LL ret = 0;
    LL ahv = 0;
    fuck = c[u];
    dep[u] = 0;
    for(int i = head[u]; i; i = e[i].next) {
        int to = e[i].to;
        if(vis[to]) continue;
        dfs3(to,u);
        ret += dfs2(to,u,siz[to]);
        make_fail(to,u);
        ret += 1LL*(allnode - ahv - siz[to])*siz[to];
        ahv += siz[to];
    }
    return ret;
}
void getdeep(int u,int f) {
        siz[u] =  1;
        cnt[c[u]] = 0;
        dep[u] = dep[f] + 1;
        for(int i = head[u]; i; i = e[i].next) {
            int to = e[i].to;
            if(vis[to] || to == f) continue;
            getdeep(to,u);
            siz[u] += siz[to];
        }
}
void work(int u) {
    vis[u] = 1;
    getdeep(u,0);
    ans += cal(u);
    for(int i = head[u]; i; i = e[i].next) {
        int to = e[i].to;
        if(vis[to]) continue;
        allnode = siz[to];
        root = 0;
        getroot(to,0);
        work(root);
    }
}
void init() {
    t = 1;
    for(int i = 0; i <= n; ++i)
        head[i] = 0,vis[i] = 0,cnt[i] = 0;
}
int main() {
    int cas = 1;
    while(scanf("%d",&n)!=EOF) {
        init();
        for(int i = 1; i <= n; ++i) scanf("%d",&c[i]);
        for(int i = 1; i < n; ++i) {
            int x,y;
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
        }
        ans = 0;
        f[0] = inf;
        allnode = n,root = 0;
        getroot(1,0);
        work(root);
        printf("Case #%d: %lld\n",cas++,ans);
    }
    return 0;
}
View Code

 

1006. Function

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
typedef unsigned long long ULL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 5e5+10, M = 1e3+20,inf = 2e9,mod = 1000000007;

int cnt,vis[N];
int n,m,a[N],b[N];
vector<int> G[N],E[N];
vector<LL > ret;
LL fi[N],ff[N];
void init() {
    for(int i = 0; i <= max(n,m); ++i) {
        fi[i] = 0;vis[i] = 0;
        ff[i] = 0;
    }
    for(int i = 0; i <= max(n,m); ++i) G[i].clear(),E[i].clear();
    ret.clear();
}
void dfs(int u) {
    cnt++;
    vis[u] = 1;
    for(int i = 0; i < G[u].size(); ++i) {
        if(!vis[G[u][i]])dfs(G[u][i]);
    }
}
void dfs2(int u) {
    cnt++;
    vis[u] = 1;
    for(int i = 0; i < E[u].size(); ++i) {
        if(!vis[E[u][i]]) dfs2(E[u][i]);
    }
}
int main() {
    int cas= 1;
    while(scanf("%d%d",&n,&m)!=EOF) {
        init();
        for(int i = 0; i < n; ++i) {
            scanf("%d",&a[i]);
            G[i].push_back(a[i]);
        }
        for(int i = 0; i < m; ++i)  {
            scanf("%d",&b[i]);
            E[i].push_back(b[i]);
        }
        for(int i = 0; i < n; ++i) {
            if(!vis[i]) {
                cnt = 0;
                dfs(i);
                ret.push_back(cnt);
            }
        }
        for(int i = 0; i < m; ++i) vis[i] = 0;
        for(int i = 0; i < m; ++i) {
             if(!vis[i]) {
                cnt = 0;
                dfs2(i);
                fi[cnt]++;
            }
        }
        for(int i = 2; i <= m; ++i) {
            for(int j = i; j <= n; j += i) {
                ff[j] += fi[i]*i;
            }
        }
        LL ans = 1;
        for(int i = 0; i < ret.size(); ++i) {
            if(ret[i] != 1)
                ans = (ans * (fi[1] + ff[ret[i]]))%mod;
            else ans = (ans * (fi[1]))%mod;
        }
        printf("Case #%d: %lld\n",cas++,ans);
    }
    return 0;
}
View Code

 

1011. KazaQ's Socks

找规律

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
typedef unsigned long long ULL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 2e5+10, M = 1e3+20,inf = 2e9;


LL k,n;
int main() {
    int cas = 1;
    while(scanf("%lld%lld",&n,&k)!=EOF) {
        printf("Case #%d: ",cas++);
        if(k <= n) {
            printf("%lld\n",k);
        }
        else {
            k -= n;
            LL tmp = k - k/(n-1)*(n-1);
            if(tmp == 0) tmp = n-1;
            if((k/(n-1))%2) {
                if(tmp <= n-2) printf("%lld\n",tmp);
                else printf("%lld\n",n-1);
            }
            else {
                if(tmp <= n-2) printf("%lld\n",tmp);
                else printf("%lld\n",n);
            }

        }
    }
    return 0;
}
View Code

 

posted @ 2017-07-25 19:30  meekyan  阅读(272)  评论(1编辑  收藏  举报