https://vjudge.net/contest/321565#problem/C 超时代码

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#define inf 2147483647
#define N 10100
#define p(a) putchar(a)
#define For(i,a,b) for(register long long i=a;i<=b;++i)
//by war
//2019.8.22
using namespace std;
long long T,n,x,y,cnt,tot;
long long prime[N],mu[N],ans[55];
bool vis[N];

struct dian{
    long long l,r,t;
}a[N];

struct node{
    long long n;
    node *next;
}*e[N];

inline void in(long long &x){
    long long y=1;char c=getchar();x=0;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    x*=y;
}
inline void o(long long x){
    if(x<0){p('-');x=-x;}
    if(x>9)o(x/10);
    p(x%10+'0');
}

inline void push(long long x,long long y){
    node *p;
    p=new node();
    p->n=y;
    if(e[x]==0)
        e[x]=p;
    else{
        p->next=e[x]->next;
        e[x]->next=p;
    }
}

void Euler(){
    mu[1]=1;
    For(i,2,50){
        if(!vis[i]) prime[++cnt]=i,mu[i]=-1;
        for(register long long j=1;j<=cnt&&i*prime[j]<=50;j++){
            vis[i*prime[j]]=1;
            if(i%prime[j]==0){
                mu[i*prime[j]]=0;
                break;
            }
            mu[i*prime[j]]=-mu[i];
        }
    }
}

long long dfs(long long x,long long fa,long long w){
    long long res=0;
    for(node *i=e[x];i;i=i->next)
        if(i->n!=fa)
            res+=dfs(i->n,x,w*a[i->n].t);
    return res+w;
}

inline long long F(register long long d){
    long long res=0,kk=0;
    For(i,1,n){
        a[i].t=a[i].r/d-a[i].l/d;
        if(a[i].l%d==0)
            a[i].t++;
    }
    For(i,1,n){
        res+=dfs(i,i,a[i].t);
        res-=a[i].t;
        kk+=a[i].t;
    }
    return res/2+kk;
}

inline void clear(){
    For(i,1,50)
        e[i]=0;
    memset(ans,0,sizeof(ans));
}

signed main(){
    in(T);
    Euler();
    while(T--){
        clear();
        in(n);
        For(i,1,n-1){
            in(x);in(y);
            push(x,y);
            push(y,x);
        }
        For(i,1,n) in(a[i].l);
        For(i,1,n) in(a[i].r);
        For(i,1,50)
            for(register long long d=i;d<=50;d+=i)
                ans[i]+=mu[d/i]*F(d);
        printf("Case %lld:\n",++tot);
        For(i,1,50){
            o(i);p(':');p(' ');
            o(ans[i]);p('\n');
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/war1111/p/11397482.html