BZOJ4766: Literary computing Ji, bzoj4766 literary computing Ji

Source: Internet
Author: User

BZOJ4766: Literary computing Ji, bzoj4766 literary computing Ji

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 737 Solved: 402
[Submit] [Status] [Discuss] Description "three weeks of hard work, creating a computer ". In response to the call, Mr. W spent three weeks building a computing platform for literature and art. Art computing has more artistic cells than ordinary computers. Ordinary computers can calculate the number of spanning trees for a complete graph with a number, while literary computing Ji can calculate the number of spanning trees for a fully Binary Graph with a number. More specifically, the number of points on one side is given as n, and the number of points on the other side is m. A total of n * m edges have a complete bipartite graph K _ {n, m }, computing Ji can quickly calculate the number of trees generated. John doesn't know if Ji computing is correct. Can you help him?

 

Input only contains three integers (n, m, p) in a row, indicating the given fully bipartite graph K _ {n, m} 1 <= n, m, p <= 10 ^ 18 ).

 

Output is an integer of only one row, indicating the number of spanning trees in the fully bipartite graph K _ {n, m}. The answer is p.

 

Sample Input2 3 7 Sample Output5HINT Source

 

Algorithm 1

Combine MatrixTree theorem to form a table or inductive proof

Algorithm 2

According to prufe

R because the prufer sequence corresponds to a unique tree, the problem is to calculate the number of valid prufer sequences.
"One way to generate a Prufer sequence is to iteratively Delete points until there are only two points left in the source image ." According to the properties of the prufer sequence, the remaining two points must have an edge. In the bipartite graph, 2.1 of the connected edges is in different sets. During deletion, "the vertex with the minimum number of the vertex and the connected edge will be removed from all the leaf nodes (vertex with a degree of 1), and the vertex numbers adjacent to it will be added to the Prufer sequence ", each time A vertex is deleted, the vertex of the edge connected to it needs to be added to the prufer sequence. The vertex at both ends of the edge in the bipartite graph must belong to different sets, so there are n-1 vertices in the set to be deleted, that is to say, the number in the B set needs to be added to n-1 times. There are a total of $ m ^ n − 1 $ possibilities. Similarly, in the B set, the number of m points is deleted, that is to say, the number in the set needs to be added to the 1-1 time. There are A total of $ n ^ m-1 $ possibilities.
If the two conditions are multiplied, the answer is $ n ^ {m-1} Then m ^ {n-1} $.

 

 

#include<cstdio>#include<cstring>#include<algorithm>#define int long long using namespace std;const int MAXN=1e6+10,INF=1e4+10;inline int read(){    char c=getchar();int x=0,f=1;    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}    return x*f;}int fastmul(int a,int p,int mod){    int base=0;    while(p)    {        if(p&1) base=(base+a)%mod;        a=(a+a)%mod;        p>>=1;    }    return base;}int fastpow(int a,int p,int mod){    int base=1;    while(p)    {        if(p&1) base=fastmul(base,a,mod)%mod;        a=fastmul(a,a,mod)%mod;        p>>=1;    }    return base;}main(){    int N=read(),M=read(),P=read();    printf("%lld",fastmul(fastpow(N,M-1,P),fastpow(M,N-1,P),P)%P);     return 0;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.