[Fill file] noip2019 training test events (xiv)

Problem A: Fibonacci(fib.pas/cpp)

Time Limit: 1000 ms Memory Limit: 128 MB

Description

Peas recently fell in love with the number of Fibonacci, then he began to study the product of the number of Fibonacci. Now he would like to ask you a number can not be broken down into the product of two Fibonacci numbers?

Definition of the Fibonacci: F0 = 0, F1 = 1, Fk = Fk-1 + Fk-2.

Input

The first line T represents an integer number of questions.

Next T lines, each line represents a digital A Peas ask for your number.

Output

For each question, if this number can be decomposed into the product of two Fibonacci numbers output "Yes", otherwise a "No".

Sample Input
5
5
4
12
11
10 
Sample Output
Yes
Yes
No
No
Yes 

HINT

For 50% of the data: A≤50;

To 100% of the data: T≤100,0≤A≤10 ^ 9.

Solution

Conclusion: in the int range, Fibonacci listed only 46

So every sentence directly on it.

#include<bits/stdc++.h>
using namespace std;
#define int long long
map<int,int>mp;
int f[201];
void pre(){
    f[1]=1;
    for(int i=2;i<=46;++i){
        f[i]=f[i-1]+f[i-2];
        if(i>2)mp[f[i]]=1;
    }
}
signed main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    pre();
    int T;
    scanf("%lld",&T);
    while(T--){
        int tmp;
        scanf("%lld",&tmp);
        if(tmp==0||tmp==1){puts("Yes");continue;}
        bool flag=false;
        for(int i=1;i<=46;++i){
            if(tmp%f[i]==0){
                if(mp[tmp/f[i]]){
                    flag=true;
                    break;
                }
            }
        }
        puts(flag?"Yes":"No");
    }
}

Problem B: as far as the (equal.pas / cpp)

Time Limit: 1000 ms Memory Limit: 128 MB

Description

Penguins urban structure of the country is a tree, there are N cities and N-1 Tiao undirected road, each road are the same length. Peas and bean paste ready to go to NOIP (National Olympiad in Informatics for Penguin), but they live in different places, Peas live in cities A, hummus live in the city B. They are looking for a distance as far as the A and B venue, so they want to know how many cities meet this requirement?

Because they attend so many times NOIP, so there are many questions.

Input

A first integer N represents the number of row city.

Next N-1 lines, each two numbers F and T, indicate there is a road between the city and the city F T.

The next line M represents an integer number of inquiries.

Next M lines of two numbers A and B, represent the inquiry of city A and B (A may be the same B).

Output

Output M rows, each row represents an integer number of A and B as far as the city.

Sample Input
4
1 2
2 3
2 4
2
1 2
1 3 
Sample Output
0
2 

HINT

For 30% of the data: N, M≤1000;

For another 10% of the data: A = B;

For another 30% of the data: random ensure tree form;

To 100% of the data: 1≤N, M≤100000.

Solution

Query set two points for the x, y, their most recent common ancestor is lca

So in four cases discussed:

1, x = y: in this case it may be whole tree.

2, dis (x, y) is odd: a point obviously not satisfied.

3, dis (x, y) is even but dis (x, lca) = dis (y, lca):! At this point find the middle point, the middle point of the sub-tree size is the answer.

4, dis (x, lca) = dis (y, lca): except subtree contains the x and y comprising the lca, all eligible.

Then just rush on it.

#include<bits/stdc++.h>
using namespace std;
struct qwq{
    int v;
    int nxt;
}edge[400001];
int cnt=-1;
int head[400001];
void add(int u,int v){
    edge[++cnt].nxt=head[u];
    edge[cnt].v=v;
    head[u]=cnt;
}
int f[400001][21];
int dep[400001];
int siz[400001];
void dfs(int u){
    siz[u]=1;
    for(int i=1;i<=20;++i){
        f[u][i]=f[f[u][i-1]][i-1];
    }
    for(int i=head[u];~i;i=edge[i].nxt){
        int v=edge[i].v;
        if(v==f[u][0])continue;
        dep[v]=dep[u]+1;
        f[v][0]=u;
        dfs(v);
        siz[u]+=siz[v];
    }
}
int LCA(int x,int y){
    if(dep[x]<dep[y])swap(x,y);
    int d=dep[x]-dep[y];
    for(int i=0;i<=20;++i){
        if(d&(1<<i)){
            x=f[x][i];
        }
    }
    if(x==y){
        return x;
    }
    for(int i=20;i>=0;--i){
        if(f[x][i]==f[y][i])continue;
        x=f[x][i],y=f[y][i];
    }
    return f[x][0];
}
int main(){
    //freopen("in.txt","r",stdin);
    //freopen("ans.txt","w",stdout);
    memset(head,-1,sizeof(head));
    int n;
    scanf("%d",&n);
    for(int i=2;i<=n;++i){
        int u,v;
        scanf("%d%d",&u,&v);
        add(u,v),add(v,u);
    }
    dfs(1);
    int m;
    scanf("%d",&m);
    for(int i=1;i<=m;++i){
        int x,y;
        scanf("%d%d",&x,&y);
        if(x==y){
            printf("%d\n",n);
            continue;
        }
        int lca=LCA(x,y);
        if(dep[x]-dep[lca]==dep[y]-dep[lca]){
            int mid=dep[x]-dep[lca]-1;
            for(int j=0;j<=20;++j){
                if(mid&(1<<j)){
                    x=f[x][j];y=f[y][j];
                }
            }
            printf("%d\n",n-siz[x]-siz[y]);
            continue;
        }
        if((dep[x]+dep[y])%2==1){
            puts("0");
            continue;
        }
        int mid=(dep[x]-dep[lca]+dep[y]-dep[lca])/2;
        int mid1=mid-1;
        if(dep[x]<dep[y])swap(x,y);
        int xx=x;
        for(int j=0;j<=20;++j){
            if(mid&(1<<j)){
                x=f[x][j];
            }
            if(mid1&(1<<j)){
                xx=f[xx][j];
            } 
        }
        //cout<<x<<endl; 
        printf("%d\n",siz[x]-siz[xx]);
    }
}

Problem C: split cable (tree.pas / cpp)

Time Limit: 1000 ms Memory Limit: 128 MB

Description

Penguin Internet between countries are interconnected by a cable network, a tree structure is formed. Now that the winter, the heating sector the lack of fuel, so they decided to dismantle some of the network cable to do the fuel. But now there are K penguins online games to the Internet and others, so they need to put K penguins arrange a different room (two penguins in the same room will fight), then removed some of the cable, but the need to ensure that each penguin can at least stay through the cable and at least one other penguin online game.

So they want to know the minimum number of network cable need to be retained?

Input

A first line integer T, denotes the number of data sets;

The first line of each set of two integers N, K, represents the room number and the total number of penguins.

The second row of N-1 integers, Ai represents i-th room integer i + 1 and Ai room has a cable connection (1≤Ai≤i).

Output

Each output data represents an integer minimum number of cable retained.

Sample Input
2
4 4
1 2 3
4 3
1 1 1 
Sample Output
2
2 

HINT

For 30% of the data: N≤15;

For 50% of the data: N≤300;

For 70% of the data: N≤2000;

To 100% of the data: 2≤K≤N≤100000, T≤10.

Solution

First of all, we are the best option should be selected every one side an edge in order to minimize the use of the number of sides.

So greedy again to find the number of sides of the above programs, if these sides have been enough to put down all the penguins opinions output directly, or add extra penguin (ie separate even out side) can be.

#include<bits/stdc++.h>
using namespace std;
struct qwq{
    int v;
    int nxt;
}edge[200001];
int cnt=-1;
int head[200001];
void add(int u,int v){
    edge[++cnt].nxt=head[u];
    edge[cnt].v=v;
    head[u]=cnt;
}
int ans;
bool dfs(int u,int fa){
    bool flag=false;
    for(int i=head[u];~i;i=edge[i].nxt){
        int v=edge[i].v;
        if(v==fa)continue;
        bool tmp=dfs(v,u);
        if(!flag&&tmp){
            ans++;
            flag=true;
            //cout<<u<<" "<<v<<endl;
        }
    }
    return flag?false:true;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        ans=0;
        cnt=-1;
        memset(head,-1,sizeof(head));
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=2;i<=n;++i){
            int tmp;
            scanf("%d",&tmp);
            add(i,tmp),add(tmp,i);
        }
        dfs(1,-1);
        //cout<<ans<<endl;
        if(ans*2>=m){
            if(m&1){
                printf("%d\n",m/2+1); 
            }
            else printf("%d\n",m/2);
        }
        else printf("%d\n",ans+(m-(ans*2)));
    }
}

Guess you like

Origin www.cnblogs.com/youddjxd/p/11423559.html