BZOJ4602: [Sdoi2016] Gear DFS Inverse

Source: Internet
Author: User

This problem is a DFS, there is a dairy problem almost the same. But the accuracy of this problem card.

There is a problem with another piece of the puzzle online. This method of taking the logarithm can be easily jammed. For example, 1e18 and (1e9-1) * (1e9+1) to take the logarithm is not guaranteed to not be card accuracy. So we need to change a method.

We take a large prime number, which is calculated in the modal sense of this prime number: multiplication is multiplication, division becomes multiply inverse, negative number plus a prime number. This approach may conflict, but is not data-independent.

#include <cstdio>using namespacestd;structEdge {intp; intA; intb; Edge*Next;} ; Const intp =10079 ;intPOW (intAintN) {intAns =1 ;  while(n) {if(N &1) ans *= A, ans%=p; A*= A; A%=p; N/=2 ; }    returnans;}intInt JConst inta) {returnPow (A, p-2 ) ; } Const intMAXN =1234 ; Const intMAXM =12345 ; intN, M; Edge E [Maxm*2 ] ; Edge*V [MAXN];BOOLvis [MAXN];intdis [MAXN];voidRead () {scanf ("%d%d", & N, &M);  for(inti =1; I <= N; + + i) vis [i] = V [i] =0 ;  while(M-- ) {        intS, T, a, B; scanf ("%d%d%d%d", & S, & T, & A, &b); Edge*ConstT1= & E [M *2 ] ; T1p =T; T1-A =A; T1B = B <0? B +p:b; T1Next =V [s]; V [s]=T1; Edge*Constf = & E [M *2+1 ] ; Fp =s; FA = b <0? B +p:b; Fb =A; FNext =V [t]; V [T]=F; }}BOOLDFS (Const into) {Vis [O]=true ;  for(Edge * v = v [o]; v; v = Vnext)if(! Vis [v-p]) {dis [vP] = dis [o] * v-a% P * INV (v-B)%p; if(Dfs (v-P))return false ; } Else     if(Dis [v-p]! = DIS [o] * v-a% P * INV (v-B)%p)return false ; return true ;}BOOLsolve () { for(inti =1; I <= N; ++i)if(! Vis [i] && (dis [i] =1, ! DFS (i)))return false ; return true ;}intMain () {intT; scanf ("%d", &T);  for(inti =1; I <= T; ++i) {read (); printf ("Case #%d:", i); Puts (solve ()?"Yes":"No" ) ; }    return 0 ; }

BZOJ4602: [Sdoi2016] Gear DFS Inverse

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.