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

Codeforces Round #362 (Div. 2) ABCDE

时间:2016-08-07 13:47:10      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

A. Pineapple Incident

题解:

水题。。。注意没有t+1这种情形

代码:

#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define se second
#define fs first
#define ll long long
using namespace std;
const int INF=1e9+10;
const int maxn=1000000+5;

ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
    while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
    return x*f;
}
//-----------------------------------------------------------------------------

int main()
{
    int t,s,x;
    cin>>t>>s>>x;
    if(x<t) cout<<"NO"<<endl;
    else
    {
        int tmp=x-t,tmp1=x-t-1;
        if(tmp==0||tmp%s==0||(tmp1%s==0&&tmp1!=0)) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
}

 

B. Barnicle

题解:

将科学进制转换为10进制。。。。模拟题意就行。。注意有一个坑点

代码:

#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define se second
#define fs first
#define ll long long
using namespace std;
const int INF=1e9+10;
const int maxn=1000000+5;

ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
    while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
    return x*f;
}
//-----------------------------------------------------------------------------

int main()
{
    string num;
    cin>>num;
    int pos1,pos2,d,b;
    for(int i=0;i<num.length();i++)
    {
        if(num[i]==.) pos1=i;
        if(num[i]==e) pos2=i;
    }
    b=0;
    for(int i=pos2+1;i<num.length();i++) b=b*10+num[i]-0;
    d=pos2-pos1-1;
    if(b>=d)
    {
        int k=b-d,flag=0;
        for(int i=0;i<pos2;i++)
        {
            if(i==pos1) continue;
            if(num[i]==0&&flag==0) continue;
            flag=1;
            cout<<num[i];
        }
        for(int i=0;i<k;i++) cout<<0;
    }
    else
    {
        if(b==0)
        {
            if(d==1&&num[pos1+1]-0==0)    for(int i=0;i<pos1;i++) cout<<num[i];
            else  for(int i=0;i<pos2;i++) cout<<num[i];
        }
        else
        {
            for(int i=0;i<=pos1+b;i++)
            {
                if(i==pos1) continue;
                cout<<num[i];
            }
            cout<<num[pos1];
            for(int i=pos1+b+1;i<pos2;i++) cout<<num[i];
        }
    }
    return 0; 
}

 

C. Lorenzo Von Matterhorn

题解:

使用map来保存,然后更新时从树向父节点更新。。。直到相等即可。求和也是如此

代码:

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define se second
#define fs first
#define ll long long
const int INF=1e9+10;
const int maxn=1000000+5;

ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
    while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
    return x*f;
}
//-----------------------------------------------------------------------------

map<pair<ll,ll>,ll> Edge;

void Change(ll a,ll b,ll c)
{
    ll Max=max(a,b),Min=min(a,b);
    while(Max!=Min)
    {
        while(Max>Min)
        {
            ll k=Max>>1;
            Edge[make_pair(k,Max)]+=c;
            Edge[make_pair(Max,k)]+=c;
            Max=k;
        }
        while(Min>Max)
        {
            ll k=Min>>1;
            Edge[make_pair(k,Min)]+=c;
            Edge[make_pair(Min,k)]+=c;
            Min=k;
        }
    }
}

ll solve(ll a,ll b)
{
    ll sum=0;
    ll Max=max(a,b),Min=min(a,b);
    while(Max!=Min)
    {
        while(Max>Min)
        {
            ll k=Max>>1;
            sum+=Edge[make_pair(Max,k)];
            Max=k;
        }
        while(Min>Max)
        {
            ll k=Min>>1;
            sum+=Edge[make_pair(Min,k)];
            Min=k;
        }
    }
    return sum;
}

int main()
{
   int n;
   ll d,a,b,c;
   n=read();
   for(int i=1;i<=n;i++)
   {
         d=read();
         if(d==1)
         {
              a=read();b=read();c=read();
              Change(a,b,c);
         }
         else
         {
             a=read();b=read();
             cout<<solve(a,b)<<endl;
         }
   }
}

 

D. Puzzles

题解:

在dfs过程中。从父节点到子节点的过程中,每个子节点被访问的概率是相同的,求每个节点被访问的时间的期望

每个子节点被访问的概率是相同的等价于每个子节点都有50%的概率在其他的子节点之前被访问到,那么该节点的期望就等于父节点的期望+其余子节点的和/2;

先第一次求每个节点有多少节点。然后再求期望.两次dfs即可

代码:

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define se second
#define fs first
#define ll long long
#define CLR(x) memset(x,0,sizeof x)
#define SZ(x) ((int)(x).size())
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
typedef pair<int,int> P;
const double eps=1e-9;
const int maxn=100100;

ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
    while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
    return x*f;
}
//-----------------------------------------------------------------------------

vector<int> G[maxn];
double ans[maxn];
int sz[maxn];
double st[maxn],ct;

void dfs1(int s)
{
    sz[s]=0;
    for(int i=0;i<G[s].size();i++)
    {
        dfs1(G[s][i]);
        sz[s]+=sz[G[s][i]];
    }
    sz[s]++;
}

void dfs2(int s)
{
    for(int i=0;i<G[s].size();i++)
    {
        double sum=sz[s]-1-sz[G[s][i]];
        st[G[s][i]]=st[s]+sum/2+1;
        dfs2(G[s][i]);
    }
}

int main()
{
    int n,p;
    cin>>n;
    for(int i=2;i<=n;i++)
    {
        cin>>p;
        G[p].pb(i);
    }
    ct=0;
    st[1]=1;
    dfs1(1);
    dfs2(1);
    for(int i=1;i<=n;i++) cout<<setprecision(10)<<st[i]<<" ";
    return 0;
}

 

E. PLEASE

题解:

不怎么会做啊。。。数学方面的。

看的snowy_smile的博客(orz)

博客地址http://blog.csdn.net/snowy_smile/article/details/52052161

代码:

快速幂

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define se second
#define fs first
#define ll long long
#define CLR(x) memset(x,0,sizeof x)
#define SZ(x) ((int)(x).size())
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 
#define INF 2097152
typedef pair<int,int> P;
const double eps=1e-9;
const int maxn=1000100;
const int mod=1e9+7;

ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
    while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
    return x*f;
}
//-----------------------------------------------------------------------------

int n;

ll mul(ll x,int p)
{
    ll y=1;
    while(p)
    {
        if(p&1) y=y*x%mod;
        x=x*x%mod;
        p>>=1;
    }
    return y;
}

ll inv(ll a)
{
    return mul(a,mod-2);
}

void fast(int tim)
{
    ll top=mul(2,tim);
    if(tim&1)
    {
        top+=1;
        if(top>=mod) top-=mod;
    }
    else
    {
        top-=1;
        if(top<0) top+=mod;
    }
    top=top*inv(3)%mod;
    cout<<top;
}

int main()
{
    cin>>n;
    ll tim=1;
    for(int i=1;i<=n;i++)
    {
        ll x;cin>>x;
        x%=(mod-1);
        tim*=x;
        tim%=(mod-1);
    }
    tim=(tim+mod-2)%(mod-1);
    fast(tim);
    cout<<"/";
    cout<<mul(2,tim)<<endl;
    return 0;
}

矩阵快速幂

/*
求4*n+1; 
构造矩阵 
4 0
1 1 
初始矩阵
0 1
0 0 
*/

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define se second
#define fs first
#define ll long long
#define CLR(x) memset(x,0,sizeof x)
#define MC(x,y) memcpy(x,y,sizeof(x))  
#define SZ(x) ((int)(x).size())
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 
#define INF 2097152
typedef pair<int,int> P;
const double eps=1e-9;
const int maxn=1000100;
const int mod=1e9+7;

ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
    while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
    return x*f;
}
//-----------------------------------------------------------------------------

int n;
const int G=2;

struct MX
{
    int v[G][G];
    void O(){CLR(v);}
    void E(){CLR(v);for(int i=0;i<G;i++) v[i][i]=1;}
    MX operator*(const MX &b) const
    {
        MX c;c.O();
        for(int i=0;i<G;i++)
        for(int j=0;j<G;j++)
        for(int k=0;k<G;k++)
        c.v[i][j]=(c.v[i][j]+(ll)v[i][k]*b.v[k][j])%mod;
        return c;
    }
    MX operator+(const MX &b) const
    {
        MX c;c.O();
        for(int i=0;i<G;i++)
        for(int j=0;j<G;j++) c.v[i][j]=(v[i][j]+b.v[i][j])%mod;
        return c;
    }
    MX operator^(ll p) const
    {
        MX y;y.E();
        MX x;MC(x.v,v);
        while(p)
        {
            if(p&1) y=y*x;
            x=x*x;
            p>>=1;
        }
        return y;
    }
}a,b,c;

void tryit(int p)
{
    b.O();
    b.v[0][0]=4;
    b.v[1][0]=b.v[1][1]=1;
    a.O();
    a.v[0][0]=0;
    a.v[0][1]=1;
    c=a*(b^(p/2));
    ll ans=c.v[0][0];
    if(p&1) ans=(ans*2+1)%mod;
    cout<<ans;
}

ll mul(ll x,int p)
{
    ll y=1;
    while(p)
    {
        if(p&1) y=y*x%mod;
        x=x*x%mod;
        p>>=1;
    }
    return y;
}

int main()
{
    cin>>n;
    ll tim=1;
    for(int i=1;i<=n;i++)
    {
        ll x;cin>>x;
        x%=(mod-1);
        tim*=x;
        tim%=(mod-1);//这里用费马小定理进行加速,具体是这样的,要求2^n%mod,mod与2互质,那么2^(mod-1)%mod=1,求tim有多少个2即可 
    }
    tim=(tim+mod-2)%(mod-1);//这里,因为在计算时最后用的是tim-1,这么写是为了避免tim=0时,减1变成负 
    tryit(tim);
    cout<<"/";
    cout<<mul(2,tim)<<endl;
    return 0;
}

 

关于逆元的学习

http://www.cnblogs.com/linyujun/p/5194184.html

http://blog.csdn.net/hlyfalsy/article/details/38067021

http://blog.csdn.net/acdreamers/article/details/8220787

Codeforces Round #362 (Div. 2) ABCDE

标签:

原文地址:http://www.cnblogs.com/byene/p/5745974.html

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