About mod& chain forward star-September 25, 2015

Source: Internet
Author: User

Long time no write, in fact, has been doing, but not the mood to write a summary of the article, so or later to do a little bit to write it.

Recently the state is very poor very bad, the water problem all can not pass, I am also drunk.

Redo last year's two Noip questions, d1t2 and D2T3, which included the MoD operation.

The MoD operation basically satisfies the arithmetic, so many expressions are equivalent in modulo p meaning, except for division.

However, it is important to note that the calculation needs to be done in a non-negative case, and when subtraction occurs, it must be added to the positive value.

In addition to pay attention to the problem of overflow, timely take the mold is also very important.

The noip2014d1t2 can be used as a chain forward-to-star chart.

That's about it:

struct edge{

int to,next,w;

};

Head[];

This is what happens when you cycle:

for (int i=head[u];i!=-1;i=e[i].next)

Which means head is initialized to-1.

There are also Addedge:

void Addedge (int u,int v,int W) {

E[etot].to=v;

E[etot].next=head[u];

head[u]=etot++;

}

Seems to be very convenient, for the sparse graph effect is very good, but also to avoid the use of static linked lists, the feeling of static linked list is a bit troublesome.

noip2014 Joint Weights:

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <string>5#include <algorithm>6 using namespacestd;7 Const intMAXN =200000*2+Ten;8 Const intMod=10007; 9 structedge{Ten     intTo,next; One }; A Edge E[MAXN]; - intetot=0; - intHEAD[MAXN]; the intW[MAXN]; - inttot=0, max_w=0; - intN; - voidAddedge (intUintv) { +e[etot].to=v; -e[etot].next=Head[u]; +head[u]=etot++; A } at voidread_in () { -memset (head,-1,sizeof(head)); -scanf"%d",&n); -      for(intI=1; i<n;i++){ -         intu,v; -scanf"%d%d",&u,&v); in Addedge (u,v); - Addedge (v,u); to     } +      for(intI=1; i<=n;i++) scanf ("%d", w+i); - } the voidCalint&x,int&y,intW) { *     if(w>=x) {y=x; x=w;return;} $     if(w>y) {y=w;return;}Panax Notoginseng } - voidsolve () { the      for(intI=1; i<=n;i++){ +         intsum=0; A         intmax_1=0, max_2=0; the          for(intj=head[i];j!=-1; j=E[j].next) { + Cal (Max_1,max_2,w[e[j].to]); -Sum= (sum+w[e[j].to])%MOD; $         } $         if(Max_1*max_2>max_w) max_w=max_1*max_2; -          for(intj=head[i];j!=-1; j=E[j].next) { -Tot= (tot+ ((sum-w[e[j].to]+mod)%mod) *w[e[j].to])%MOD; the         } -     }Wuyicout<<max_w<<' '<<tot%MOD<<Endl; the } - intMain () { Wu read_in (); - solve (); About     return 0; $}

Equation of noip2014 Solution

This problem is more advanced, but also the use of a lot of expressions in the meaning of the modulus of the equivalent of this property, so only a few prime p, the 1 to P generation equation, the final enumeration of the solution, mapping back to 1 to P judgment and so on is not equal to 0 is good. Complexity Max (P*n*pnum,m*pnum), so p can't be too big, almost 10000 or so.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <string>5#include <algorithm>6 using namespacestd;7 Const intMAXN = -+Ten;8 Const intmodnum=5;9 Const intMaxmod=21893+Ten;Ten Const Long Longmod[modnum]={10007,11261,14843,19997,21893}; One Long LongA[maxn][modnum]; A Long LongRes[maxmod][modnum]; - intANS[MAXN]; - intn,m; the voidChangeLong Long&x,stringSLong LongMoD) { -     intlen=s.size (); -x=0; -     if(s[0]=='-'){ +          for(intI=1; i<len;i++) x= (x*Ten+s[i]-'0')%MoD; -x=-x; +     } A     Else{ at          for(intI=0; i<len;i++) x= (x*Ten+s[i]-'0')%MoD; -     } - } - BOOLJud (intx) { -      for(intI=0; i<modnum;i++)if(res[x%mod[i]][i]!=0)return false; -     return true; in } - intMain () { toCin>>n>>m; +      for(intI=0; i<=n;i++){ -         stringx; theCin>>x; *          for(intj=0; j<modnum;j++) Change (a[i][j],x,mod[j]);  $     }Panax Notoginseng     inttot=0; -      for(intMod=0; mod<modnum; mod++){ the          for(intp=1;p <mod[mod];p + +){ +             // A             Long LongCurrent=0; the             Long Long Base=1; +             //Cal -              for(intI=0; i<=n;i++){ $Current= (current+Base*A[I][MOD])%Mod[mod]; $                 Base=Base*p%Mod[mod]; -             } -             //cal/ theres[p][mod]=Current ; -             //Wuyi         } the     } -      for(intI=1; i<=m;i++)if(Jud (i)) ans[tot++]=i; Wucout<<tot<<Endl; -      for(intI=0; i<tot;i++) cout<<ans[i]<<Endl; About     return 0; $}

About mod& chain forward star-September 25, 2015

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.