Codeforces Round #362 (Div. 2) ABCDE

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

时间: 2024-10-08 17:24:11

Codeforces Round #362 (Div. 2) ABCDE的相关文章

Codeforces Round #261 (Div. 2)[ABCDE]

Codeforces Round #261 (Div. 2)[ABCDE] ACM 题目地址:Codeforces Round #261 (Div. 2) A - Pashmak and Garden 题意: 一个正方形,它的边平行于坐标轴,给出这个正方形的两个点,求出另外两个点. 分析: 推断下是否平行X轴或平行Y轴,各种if. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * File: A.cpp * Create Date: 2014-0

Codeforces Round #264 (Div. 2)[ABCDE]

Codeforces Round #264 (Div. 2)[ABCDE] ACM 题目地址: Codeforces Round #264 (Div. 2) 这场只出了两题TAT,C由于cin给fst了,D想到正解快敲完了却game over了... 掉rating掉的厉害QvQ... A - Caisa and Sugar[模拟] 题意: Caisa拿s美元去超市买sugar,有n种sugar,每种为xi美元yi美分,超市找钱时不会找美分,而是用sweet代替,当然能用美元找就尽量用美元找.他

Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn LCA(最近公共祖先)

C. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. Ther

Codeforces Round #260 (Div. 2) ABCDE

A题逗比了,没有看到All ai are distinct. All bi are distinct. 其实很水的.. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 #define mnx 100002 8 9 10 struct latop{ 11 int p, q; 12 bo

Codeforces Round #531 (Div. 3) ABCDE题解

Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividing 题意: 给一个数n,然后要求你把1,2.....n分为两个集合,使得两个集合里面元素的和的差的绝对值最小. 题解: 分析可以发现,当n%4==0 或者 n%3==0,答案为0:其余答案为1.之后输出一下就好了. 代码如下: #include <bits/stdc++.h> using name

Codeforces Round #200 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/344 A. Magnets time limit per test:1 second memory limit per test:256 megabytes Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets inst

Codeforces Round #105 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/148 比较简单的一场,最长的一题也才写了30行多一点 A. Insomnia cure time limit per test:2 seconds memory limit per test:256 megabytes ?One dragon. Two dragon. Three dragon?, - the princess was counting. She had trouble falling asleep, and

Codeforces Round #362 (Div. 1) B. Puzzles 树形dp,概率

题目链接: http://codeforces.com/problemset/problem/696/B 题意: 一个树,dfs遍历子树的顺序是随机的.所对应的子树的dfs序也会不同.输出每个节点的dfs序的期望 思路: http://www.cnblogs.com/01world/p/5795498.html 假设四个子节点为A,B,C,D,因为排列等可能,所以A在B前面的概率跟A在B后面的概率相等,C和D对于A而言一样.所以遍历A的时间期望就是( t(B) + t(C) + t(D) )/2

Codeforces Round #186 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/313 A. Ilya and Bank Account time limit per test:2 seconds memory limit per test:256 megabytes Ilya is a very clever lion, he lives in an unusual city ZooVille. In this city all the animals have their rights and obl