Codefroces 852b-neural Network Country

Source: Internet
Author: User
Tags mul


Test instructions: There is a direction graph, except the source point and the meeting point has the L layer, each layer n points. Each point in the i+1 layer has an edge at each point of the i+2 layer, and the weight of the edge is the weighted value of the end of the edge. The path length of the source point to the meeting point can be divisible by M.

Solving: fast power. A[i] Indicates the total path length from layer 1th to layer A is the number of I (i% m), B[j] represents the total path length from the a+1 level to the a+1 layer (that is, their own layer) is the number of J (j% m), then the a+2 layer a[(i+j)%m] = A[i]*b [j].

Violent practices, starting from the first floor, on a layer-by-layer, will obviously time out.

Take a closer look, from the 2nd floor to the L-1 layer, each time the operation is the same, you can use a fast power to first the 2nd layer to the L-1 layer up.

#include <iostream>#include<cstring>#include<algorithm>#include<cstdio>#defineMoD 1000000007using namespacestd;Const intMAXN =100000+Ten;inta[1000010];intN, L, M;structnode{Long Longnum[ the]; Node () {memset (num,0x0000,sizeof(num)); }};node Begin, End, Mid;node mul (node la, node lb) {node AA=node ();  for(inti =0; I < m; i++)    {         for(intj =0; J < M; J + +)        {            intK = (i+j)%m; AA.NUM[K]+ = la.num[i] * Lb.num[j]%MoD; AA.NUM[K]%=MoD; }    }    returnAA;} Node Fast (node nod,intk) {Node sum=nod; K--;  while(k) {if(k&1) {sum=mul (sum, nod); } k>>=1; Nod=Mul (nod, nod); }    returnsum;}intMain (void) {Ios::sync_with_stdio (false); Begin=node (); End=node (); Mid=node (); CIN>> n >> L >>m;  for(inti =1; I <= N; i++)    {        intX CIN >>x; Begin.num[x%m]++; }     for(inti =1; I <= N; i++)    {        intX CIN >>x; A[i]=x; Mid.num[x%m]++; }     for(inti =1; I <= N; i++)    {        intX CIN >>x; end.num[(x+a[i])%m]++;    } node nod; if(L-2>0) {nod= Fast (Mid, L-2); Nod=Mul (nod, Begin); Nod=Mul (nod, End); }    Else{nod=Mul (Begin, End); }        Long LongAns =0;  for(intj =0; J <= -; J + +)    {        if(j%m==0) {ans+=Nod.num[j]; Ans%=MoD; }} cout<<ans;}

Codefroces 852b-neural Network Country

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: 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.