Day2 training for National Day

Keywords: C++ less

T1 Continuous Storm

subject

[Title Description]

Given M colours of beads, the number of beads in each colour is unlimited. These beads are made into a necklace of length N.

Ask how many non-repetitive necklaces you can make. The two necklaces are the same, if and only if the two necklaces can be overlapped by rotation or turnover, and the corresponding beads have the same color.

[Input Format]

One line and two integers represent M and N, respectively.

[Output Format]

An integer in a row represents the answer.

[Input sample]

2 5

[Output sample]

8 

[Data Scale]

For 30% of the data, n,m < 4;

For 60% of the data, n,m < 5;

For 100% of the data, the nanometer is less than 32.

analysis

When the nanometer is less than 32, AC can be directly violated.

Positive solutions seem to be Polya's theorem.

Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;
int m,n,ans;
int gcd(int x,int y)
{
    if(y==0) return x;
    return gcd(y,x%y);
}
int main()
{
    //freopen("necklace.in","r",stdin);
    //freopen("necklace.out","w",stdout);
    cin>>m>>n;
    for(int i=1;i<=n;i++) ans+=pow(m,gcd(n,i));
    if(n&1) ans+=n*pow(m,(n+1)/2);
    else ans+=(n/2)*pow(m,(n+2)/2)+(n/2)*pow(m,n/2);
    cout<<ans/(n*2);
    return 0;
}

 

 

 

 

 

T2 planting trees

subject

[Title Description]

Fanvree is smart, and he always simplifies problems when solving them. For example, he likes to convert diagrams to trees all day long. But he doesn't shrink, so how does he transform?

This is a graph with n points m two way edges. Fanvree will select a node, then delete the node and even the edge of the point. If it becomes a tree, then the node is feasible. What is a tree? Trees are undirected connected graphs without simple rings.

Tell Fanvree what the possible node is.

[Input Format]

The first line has two positive integers n,m, which means that there are n points m edges. Guarantee n (> 2).

Next, in line m, there are two integers v and u in each line, which means that there is an undirected edge between v and U which is 1 < v and U < n. Make sure there are no double edges and self-rings.

[Output Format]

The first line has a positive integer ns, which means that NS nodes are optional in this graph.

The next line contains ns integers, each representing the number of an optional node. Please output the numbers from small to large.

There is at least one optional node in the data assurance graph.

[Input sample]

6 6
1 2
1 3
2 4
2 5
4 6
5 6

[Output sample]

3
4 5 6

[Data Scale]

For 40% of the data, n,m < 1000;

There is another 10% data, m=n-1;

There is another 20% data, m=n.

For 100% of the data, n,m < 100000.

analysis

An optional point is not necessarily a cut point.

After selecting the point, if the graph is connected and the number of remaining edges is n-2, then the point is optional.

Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std;
const int N=100010;
int n,m,head[N],ver[N<<1],Next[N<<1],tot,deg[N],ans,dfn[N],low[N],cnt,anss[N],cut[N];
void add(int x,int y)
{
    ver[++tot]=y;
    Next[tot]=head[x],head[x]=tot;
}
void tarjan(int x)
{
    int child=0;
    dfn[x]=++cnt,low[x]=cnt;
    for(int i=head[x];i;i=Next[i])
    {
        int y=ver[i];
        if(!dfn[y])
        {
            tarjan(y);
            low[x]=min(low[x],low[y]);
            if(low[y]>=dfn[x]&&x!=1) cut[x]=1;
            if(x==1) child++;
        }
        low[x]=min(low[x],dfn[y]);
    }
    if(child>=2) cut[x]=1;
}
int main()
{
    cin>>n>>m; 
    for(int i=1;i<=m;i++)
    {
        int x,y;
        cin>>x>>y;
        add(x,y),add(y,x);
        deg[x]++,deg[y]++;
    }
    tarjan(1);
    for(int i=1;i<=n;i++)
        if(!cut[i]&&deg[i]==m-n+2)
        {
            ans++;
            anss[ans]=i;
        }
    if(ans==0)
    {
        cout<<-1;
        return 0;
    }
    cout<<ans<<endl;
    for(int i=1;i<=ans;i++) cout<<anss[i]<<" ";
    return 0;
}

 

 

 

 

 

T3 sequence

subject

[Title Description]

Fiugou wants to find three numbers at different positions in a sequence A of length N, and use these three numbers to form a triangle with three side lengths. But it wants to be as close as possible to the front of the three numbers under the satisfied conditions.

Specifically, let these three numbers Ai, Aj, Ak (i < J < k), Fiugou wants K to be as small as possible; when k is equal, satisfy J to be as small as possible; when K and j are equal, satisfy I to be as small as possible.

But the number in this sequence may change. So Fiugou gives M operations in the following form:

1 x y: Change Ax to y;
2: Query the optimal legal solution and give the three numbers (not the location) from small to large.

[Input Format]

The first line is an integer N, representing the length of the sequence.

The second row has N integers representing the initial sequence.

The third line is an integer M, representing the number of operations.

Next, the M-line operation, two operation formats as described above.

[Output Format]

A total of M rows, three numbers per row, from small to large given. If not, output - 1.

[Input sample]

6
7 1 3 4 5 1
3
2
1 3 5
2

[Output sample]

3 5 7
4 5 7

[Data Scale]

For 10% data, N < 10, M < 5;

For 30% data, N < 100, M < 25;

For 50% data, N < 1000, M < 1000;

For 100% of the data, N is less than 100000, M is less than 1000, 1 is less than Ai is less than 2*109, 1 is less than x is less than N, 1 is less than y is less than 2*109.

analysis

Direct violence simulation is enough. Attention should be paid to the relationship judgment between triangle trilateral and i,j,k.

Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;
inline long long read()
{
    int num=0;
    char ch=getchar();
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9')
    {
        num=(num<<1)+(num<<3)+ch-'0';
        ch=getchar();
    }
    return num;
}
int n,m;
long long a[100100];
int main()
{
    n=read();
    for(int i=1;i<=n;i++) a[i]=read();
    m=read();
    for(int t=1;t<=m;t++)
    {
        int r=read();
        if(r==1)
        {
            int x=read();
            long long y=read();
            a[x]=y;
        }
        else
        {
            bool ok=false;
            for(int k=3;k<=n;k++)
                if(!ok)
                    for(int j=2;j<k;j++)
                        if(!ok)
                            for(int i=1;i<j;i++)
                                if(a[i]+a[j]>a[k]&&a[i]+a[k]>a[j]&&a[j]+a[k]>a[i])
                                {
                                    if(a[i]<a[j])
                                    {
                                        if(a[i]<a[k])
                                        {
                                            cout<<a[i]<<" ";
                                            if(a[j]<a[k]) cout<<a[j]<<" "<<a[k]<<endl;
                                            else cout<<a[k]<<" "<<a[j]<<endl;
                                        }
                                        else cout<<a[k]<<" "<<a[i]<<" "<<a[j]<<endl;
                                    }
                                    else
                                    {
                                        if(a[i]<a[k]) cout<<a[j]<<" "<<a[i]<<" "<<a[k]<<endl;
                                        else
                                        {
                                            if(a[j]<a[k]) cout<<a[j]<<" "<<a[k]<<" ";
                                            else cout<<a[k]<<" "<<a[j]<<" ";
                                            cout<<a[i]<<endl;
                                        }
                                    }
                                    ok=true;
                                    break;
                                }
            if(!ok) cout<<"-1"<<endl;
        }
    }
    return 0;
}

 

 

 

 

 

T4 gift

subject

[Title Description]

Summer's birthday is coming. As a formal boyfriend of Xiachuan, Jitang is going to buy some birthday gifts for Xiachuan.

There are N kinds of gifts in the shop. For every gift that Xia Chuan receives, he receives the corresponding joy value Wi (the joy value of each gift can not be repeated).

Each time, the clerk will take out the first kind of gift according to a certain probability Pi (or no gift).

Jitang always buys gifts from the shop assistants. No purchase, no reason to buy it, and a purchase.

Jitang hopes that the joy of Xiachuan will be as high as possible in the end.

Find out what is the ultimate maximum joy value of Xiachuan, and find out the expected number of purchases for Xiachuan to get this joy value and Jitang.

[Input Format]

The first line, an integer N, indicates that there are N kinds of gifts.

Next, in line N, there is a real Pi and a positive integer Wi for each line, indicating the probability that the gift of type i will be taken out and the value of joy that can be obtained.

[Output Format]

In the first line, an integer represents the maximum joy that can be obtained.

In the second line, a real number represents the expected number of purchases to obtain this joy value, leaving three decimal places.

[Input sample]

3
0.1 2
0.2 5
0.3 7

[Output sample]

14
12.167

[Data Scale]

For 10% of the data, N=1;

For 30% data, N < 5;

For 100% data, N < 20, 0 < Wi < 109, 0 < Pi < 1 and Pi < 1.

analysis

What is the expectation?

N is only 20, so we can consider DP:

Let fs denote the expected number of purchases when the set of purchased gifts is s, then the transfer equation is:

Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;
const int N=(1<<20)+5;
double p[N],f[N];
int n,w[N];
long long ans;
int main()
{
    cin>>n;
    for(int i=0;i<n;i++) cin>>p[i]>>w[i],ans+=w[i];
    int temp=1<<n;
    for(int i=1;i<temp;i++)
    {
        double pp=0,ff=1;
        for(int j=0;j<n;j++)
            if(1<<j&i) ff+=f[1<<j^i]*p[j],pp+=p[j];
        f[i]=ff/pp;
    }
    cout<<ans<<endl;
    printf("%.3f",f[temp-1]);
    return 0;
}

Posted by keyurjbhatt on Sat, 05 Oct 2019 08:54:02 -0700