Codeforces D. Polandball and Polygon

Source: Internet
Author: User
Tags gcd greatest common divisor

D. Polandball and PolygonTime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Polandball have such a convex polygon with n veritces that no three of it diagonals intersect at the same POI NT. Polandball decided to improve it and draw some red segments.

He chose a number  k  such that  GCD ( n , k ) = 1. Vertices of the polygon is numbered From 1 to  N  in a clockwise the. Polandball repeats the following process  N  times, starting from the vertex& nbsp 1:

Assume you ' ve ended last operation in vertex x (consider x = 1 If it's the first operation). Draw a new segment from vertex x to k-th next vertex in clockwise direction. This was a vertexx + k or x + k - n depending on which of t Hese is a valid index of polygon ' s vertex.

Your task is to calculate number of polygon ' s sections after each drawing. A section is a clear area inside the polygon bounded with drawn diagonals or the polygon ' s sides.

Input

There is numbers in the input: n and k (5≤ n ≤106, 2≤ k ≤ n -2, gcd(n, k) = 1).

Output

You should print n values separated by spaces. The i-th value should represent number of polygon ' s sections after drawing first i lines.< /c18>

Examplesinput
5 2
Output
Input
10 3
Output
Note

The greatest common divisor (GCD) of the integers a and b are the largest positive integer that D Ivides both a and b without a remainder.

For the first sample testcase, you should output "2 3 5 8 11". Pictures below correspond to situations after drawing lines.

The main topic: an N-sided shape, starting from point 1th, each walk to the (x+k-1)%n+1 position, ask each left the path to divide the polygon into several parts. 2 ≤ kn -2, gcd( n, k) = 1, it can be found that each point must pass one time, the corresponding point when inserting the edge corresponds to an interval, the two lines do not intersect ( Contribute to the answer) when and only if their interval is not intersection, because a point only corresponds to one interval (only consider the one that goes out), with a tree-like array maintenance can be.

There is a detail: if 2k>n, then the K conversion to n-k on it. (thought this detail is obvious, then did not go to the hack, the result originally 10 in the room is right then only then 3 not fst,mdzz)

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <vector>5#include <cstdlib>6#include <cmath>7#include <cstring>8 using namespacestd;9 #defineMAXN 1000010Ten #defineLLG Long Long One #defineYyj (a) freopen (a ".", "R", stdin), Freopen (a ". Out", "w", stdout); A Llg N,M,CS[MAXN],K,X,Y,ANS,C[MAXN]; -  -Llg lowbit (Llg x) {returnx&-x;} the voidAdd (llg w,llg v) - { -      while(w<=N) -     { +C[w]+=v; w+=Lowbit (w); -     } + } A llg sum (llg w) at { -LLG ans=0; -      while(w>0) -         { -ANS+=C[W]; w-=Lowbit (w); -         } in     returnans; - } to  + llg work (llg x,llg y) - { the      *LLG l=y,r=y+n-k-K; $     if(r>N)Panax Notoginseng     { -r-=N; the         returnSUM (n)-sum (l1)+sum (r); +     } A     Else returnSUM (r)-sum (l1); the } +  - intMain () $ { $Yyj"D"); -Cin>>n>>K; -     if(k>n/2) k=n-K; thex=1; -ans=1;//Add (+);Wuyi      for(Llg i=1; i<=n;i++) the     { -y=x+K; Wu         if(y>n) y-=N; -ans+=i-Work (x, y); AboutAdd (x,1); $x=y; -printf"%lld", ans); -     }  -     return 0; A}

Codeforces D. Polandball and Polygon

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.