# The third question of test20181016 b

Source: Internet
Author: User
Tags modulus

Test Instructions B's third question (Haskell) title description

University for four years, why, why, why not study hard, did not find the same job as you.

B June one day to see such a problem, remind of endless memories.
Input \ (n, k\) and a tree \ (n\) points, there is a right, not a bit of power. The distance between two points \ (i, j\) \ (D (i, j) \) is defined as the Benquan and on the path. Please
$\sum_{1 \leq i < j \leq N} \left\lceil \frac{d (i,j)}{k} \right\rceil$
In other words, enumerate the unordered two points and find out the sum of the distance divided by \ (k\) rounding.

Input format

Enter the first line containing two integers \ (n, k\).
Next \ (n-1\) line, three integers per line (x, y, z\), indicates that there is an edge between \ (x\) and \ (y\) , and that the edge is \ (z\).

Output format

Outputs an integer that represents the answer.

Sample input

4 6
1 2 2
1 3 3
1 4 4

Sample output

7

Data size and conventions

For 100% of data, meet \ (1 \leq n \leq 100000, 1 \leq k \leq 10\).
For 100% of data, satisfy \ (1 \leq x, y \leq N, 1 \leq z \leq 10\).
For 30% of data, satisfy \ (n \leq 1000\).
For another 20% of the data, satisfy \ (k = 1\).
For another 20% of the data, satisfy \ (k = 2\).

Analysis

It is a classic question to consider the situation without rounding first.
Then consider how many paths modulus K remainder 1, 2, ....
This is a good question NOI2011 road construction and BZOJ2152 Cong cocoa adaptation.

$\lceil \frac{x}{k} \rceil = \frac{x + k-x \mod{k}}{k}$
So we need to find out how many path modulus K equals,.. K-1.

Consider DP.

Using \ (f (i,j) \) indicates how many points to I in the subtree of i mod k = j,\ (c (i) \) indicates how many paths to the global mod k = i.

F, C can be found at the time of Dfs, attention to order, can not use subtree on their own scheme.

Code
#include <cstdlib> #include <cstdio> #include <cmath> #include <cstring> #include <iostream > #include <string> #include <vector> #include <list> #include <deque> #include <stack> #include <queue> #include <map> #include <set> #include <bitset> #include <algorithm># include<complex> #define RG Register#define il inline#define co const#pragma GCC optimize ("O0") using namespace std;    Template<class t> il t read (RG t&x) {RG T data=0;    RG int w=1;    RG Char Ch=getchar ();        while (!isdigit (CH)) {if (ch== '-') w=-1;    Ch=getchar ();    } while (IsDigit (CH)) data=10*data+ch-' 0 ', Ch=getchar (); return x=data*w;} typedef long LONG ll;const int inf=0x7fffffff;const int maxn=1e5+7,maxk=11;int n,k;struct edge{int nx,to,w;}    E[maxn<<1];int head[maxn],ecnt;void Addedge (RG int X,RG int Y,RG int w) {e[++ecnt].to=y,e[ecnt].w=w; e[ecnt].nx=head[x],head[x]=ecnt;} int Siz[maxn];int F[MAXN]    [Maxk];ll c[maxn];ll Ans;il void dfs (RG int X,RG int fa) {siz[x]=1;    F[x][0]=1;        for (RG int i=head[x];i;i=e[i].nx) {RG int y=e[i].to,w=e[i].w;        if (Y==FA) continue;        DFS (Y,X);        Siz[x]+=siz[y];        Ans + = (ll) (N-siz[y]) * Siz[y] * W;        for (RG int p=0;p<k;++p) for (RG int q=0;q<k;++q) c[(p + q + W)% K] + = f[x][p] * F[y][q];    for (RG int j=0;j<k;++j) f[x][(j + W)% K] + = f[y][j];  }}int Main () {freopen ("haskell.in", "R", stdin);    Freopen ("Haskell.out", "w", stdout);    Read (n); read (k);        for (RG int i=1;i<n;++i) {RG int x,y,w;        Read (x); Read (y); Read (w);        Addedge (X,Y,W);    Addedge (Y,X,W);    } dfs (1,0);    for (RG int i=1;i<k;++i) {ans + = (k-i) * C[i];    } printf ("%lld\n", ans/k);//fclose (stdin);//fclose (stdout); return 0;}

Test20181016 B's third question

Related Keywords:

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.