Changle National training Day2

T1  Pente storm

topic

Description [title]

A given bead M colors, the number of each color are not limited to beads, these beads were made necklaces length N.

Q. How many kinds of unique necklace can be made. The same two necklaces, if and only if two or flipped by the rotation of the necklace can be superposed together, and the color corresponding to the same beads.

[Input Format]

Line represent two integers M, N.

[Output format]

A row of integer answer.

[Sample input]

2 5

[Sample Output]

8 

[Data] scale

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

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

To 100% of the data, nm≤32.

Resolve

nm≤32, you can direct violence AC.

Positive solution looks like Polya 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;
}
View Code

 

 

 

 

 

T2 trees

topic

Description [title]

Fanvree very smart, when he would solve the problem of simplifying the issue. For example, on the day he likes to map into a tree. But he would not shrink ring, how he transformed it?

This is a two-way side of the point n m bar chart, Fanvree will select a node, then delete this node and even point out this side, if that becomes a tree, then the node is feasible, What tree is it? I.e. no simple tree rings undirected FIG.

What does that tell Fanvree possible node Yes.

[Input Format]

The first line of two positive integers n, m, m n points expressed edges. Ensure n≥2.

Next m lines of two integers v, u, v and u expressed between an undirected edges 1≤v, u≤n. Side and to ensure that no heavy loopback.

[Output format]

The first line of a positive integer ns, ns denotes the figure, there are optional nodes.

The next line, total ns integers, each integer number represents an optional node. Please ascending order of output by number.

Data guarantee the existence of at least one optional node in FIG.

[Sample input]

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

[Sample Output]

3
4 5 6

[Data] scale

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

Another 10% of the data is present, m = n-1;

Another 20% of the data is present, m = n;

To 100% of the data, n, m≤100000.

Resolve

Optional cut point is not necessarily the point.

After the selected point, if the graph is connected, just as the number of sides and the remaining n-2, then this 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;
}
View Code

 

 

 

 

 

T3 sequence

topic

Description [title]

Fiugou want to find the number of three different positions in the sequence A N a length to three atoms long trilateral to form a triangle. But it wants under the condition, the location of these three numbers far forward.

Specifically, it is assumed that the number of three A I , A j , A k (I <j <k), k Fiugou desirable as small as possible; when equal to k, j as small as possible to meet; when k, j are equal, i meet as small as possible.

But this number in the sequence may vary. Therefore Fiugou M operations given the following form:

1 xy: The A X to Y;
2: Query legitimate optimal solution, from small to large is given among these three (instead of the position).

[Input Format]

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

The second line has N integers, representing the original sequence.

A third line integer M, the number of the representative operation.

Next, operation of M rows, two operation formats described above.

[Output format]

A total of M rows, each row of three numbers, from small to large are given. If not, the output 1.

[Sample input]

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

[Sample Output]

3 5 7
4 5 7

[Data] scale

For 10% of the data, N≤10, M≤5;

For 30% of the data, N≤100, M≤25;

For 50% of the data, N≤1000, M≤1000;

To 100% of the data, N≤100000, M≤1000,1≤A I ≤2 × 10 . 9 , 1≤x≤N, 1 ≦ y ≦ 10 × . 9 .

Resolve

Analog to direct violence, attention to the relationship with the sides of a triangle is determined relationship judgment i, j, k's.

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;
}
View Code

 

 

 

 

 

T4 gift

topic

Description [title]

Natsukawa's birthday is coming up. As Natsukawa formal boyfriend, Ji Tong Natsukawa going to buy some birthday present.

Store in a total of N kinds of gifts. Each Natsukawa obtain a gift, you will get the value of the joy corresponding W is I (the value of the joy of each gift can not be repeatedly obtained).

Each time, the staff will follow a certain probability P i (or do not come up with a gift), the i-th gifts out.

Season Church every time the clerk out to buy a gift. What did not come up to think they can not buy, can be considered a purchase.

The most hope Houxia Chuan Tong season of joy values ​​as high as possible.

Finally, the joy of seeking maximum value Natsukawa is how much, and find the joy Natsukawa get this value, the Church of the season expected purchases.

[Input Format]

The first line, an integer N, expressed N kinds of gifts.

Next N rows, each row a real number P i , and W is a positive integer i , the i represents the probability gift taken out and delight value can be obtained.

[Output format]

The first line, a joy integer representing the maximum value that can be obtained.

The second line, a real joy to get this number represents the value of expected purchases, three decimal places.

[Sample input]

3
0.1 2
0.2 5
0.3 7

[Sample Output]

14
12.167

[Data] scale

Data for 10%, N = 1;

For 30% of the data, N≤5;

To 100% of the data, N ≤ 20,0 <W is I ≦ 10 . 9 , 0 <P I ≦ 1 and [sigma] p I ≦ 1.

Resolve

Expectations What the hell?

Only 20 N, can be considered like a pressure DP:

Set f S said that it has purchased a collection of gift purchases to expect when s, 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;
}
View Code

 

Guess you like

Origin www.cnblogs.com/I-Love-You-520/p/11617054.html