Bestcoder Round #69 (Div.2)

Source: Internet
Author: User

A.geometry

#include <cstdio>#include<vector>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;intMain () {//freopen ("In.txt", "R", stdin);    intT;SCANF ("%d",&T);  while(t--){        intx, y; scanf ("%d%d",&x,&y); cout<<abs (2*x*y) <<Endl; }    return 0;}
View Code

B.tree

Idea: The closest point to each point is necessarily itself, the distance is unique. So you just have to pass all the points that are 0 from each other and check the set to shrink the group. Initially wanted to write a Dfs timeout, and then changed and check set, because the brain residue reasons WA a few hair.

#include <cstdio>#include<vector>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;intpar[100100];intcnt[100100];intFindintx) {    returnPar[x]= (X==par[x]?X:find (par[x]));}intMain () {//freopen ("In.txt", "R", stdin);    intT;SCANF ("%d",&T);  while(t--){        intN;SCANF ("%d",&N);  for(intI=0; i<=n;i++) {Par[i]=i; Cnt[i]=1; }         for(intI=0; i<n-1; i++){            intu,v,w; scanf ("%d%d%d",&u,&v,&W); if(!W) {Par[v]=find (U); CNT[PAR[V]]+=Cnt[v]; }        }         for(intI=1; i<=n;i++) Cnt[i]=Cnt[par[find (i)]; intans=cnt[1];  for(intI=2; i<=n;i++) ans^=Cnt[i]; printf ("%d\n", ans); }    return 0;}
View Code

C.graph

Idea: Before doing a similar, randomly select a point on the chart, and so on the probability to go to the point adjacent to him, seeking K step after his probability at each point. The probability transfer equation for this problem is the same as the previous one, just because the point is less than 20, so it becomes the form of the matrix, and then it is good to calculate by the matrix fast power. It is worth mentioning that the quick power template has problems that lead to WA. There is also a theorem that does not know number theory. Thanks for the chicory in the BC Group.

#include <cstdio>#include<vector>#include<cstring>#include<iostream>#include<algorithm>using namespaceStd;typedefLong Longll;Constll mod=1e9+7;Const intmaxn= -;//Order of matricesBOOLg[ -][ -];inttot[ -];structmatrix{ll M[MAXN][MAXN]; Matrix () {memset (M,0,sizeof(m));  for(intI=0; i<maxn;i++) M[i][i]=1; }    voidClear () {memset (M,0,sizeof(m)); }    voidShow () { for(intI=0; i<maxn;i++){             for(intj=0; j<maxn;j++) cout<<m[i][j]<<" "; cout<<Endl; }    }}; Matrix Mtmul (Matrix A,matrix B) {inti,j,k,tmp;    Matrix C;    C.clear ();  for(intI=0; i<maxn;i++)         for(intj=0; j<maxn;j++)             for(intk=0; k<maxn;k++) C.m[i][j]= (c.m[i][j]%mod+ (a.m[i][k]%mod) * (b.m[k][j]%mod))%MOD; returnC;} Matrix Mtpow (Matrix A,intk) {k--; Matrix ans=A;  while(k) {if(k&1) ans=Mtmul (ans,a); A=Mtmul (a,a); K>>=1; }    returnans;} ll Quick_pow (ll X,intm) {ll sum=1;  while(m) {if(m&1) sum= ((sum%mod) * (x%mod))%MOD; X= ((x%mod) * (x%mod))%MOD; M>>=1; }    returnsum;}intMain () {//freopen ("In.txt", "R", stdin);    intn,m;  while(cin>>n>>m) {        intu,v;        Matrix M;        M.clear (); memset (G,0,sizeof(G)); memset (Tot,0,sizeof(tot));  while(m--) {cin>>u>>v; Tot[u-1]++; G[v-1][u-1]=true; }         for(intI=0; i<n;i++)             for(intj=0; j<n;j++)                if(G[i][j]) {M.m[i][j]= ((M.m[i][j])%mod+quick_pow (tot[j],1e9+5)%mod)%MOD; }        intQ; CIN>>p;  while(q--){            intu,k; CIN>>u>>K; Matrix ans=Mtpow (m,k); //ans.show ();             for(intI=0; i<n;i++) cout<<ans.m[i][u-1]<<" "; cout<<Endl; }    }    return 0;}
View Code

Bestcoder Round #69 (Div.2)

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.