PolandBall has such a convex polygon with n veritces that no three of its diagonals intersect at the same point. 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 are numbered from 1 to n in a clockwise way. PolandBall repeats the following process n times, starting from the vertex 1:
Assume you've ended last operation in vertex x (consider x = 1 if it is the first operation). Draw a new segment from vertex x to k-th next vertex in clockwise direction. This is a vertexx + k or x + k - n depending on which of these 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.
There are only two numbers in the input: n and k (5 ≤ n ≤ 106, 2 ≤ k ≤ n - 2, gcd(n, k) = 1).
You should print n values separated by spaces. The i-th value should represent number of polygon's sections after drawing first i lines.
5 2
2 3 5 8 11
10 3
2 3 4 6 9 12 16 21 26 31
The greatest common divisor (gcd) of two integers a and b is the largest positive integer that divides 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.
![](http://codeforces.com/predownloaded/33/de/33de87d500c2f587988d5c0f86e1af173bf4227e.png)
![](http://codeforces.com/predownloaded/5c/d0/5cd059d6a34bc4d34e9980ea56b9394f6ea929b6.png)
![](http://codeforces.com/predownloaded/1a/0f/1a0f27678edfb803f7a60c2f7ea25feaf3b80f9a.png)
![](http://codeforces.com/predownloaded/ab/0d/ab0dbd4291753344fda575c0f2cbe238e5304626.png)
![](http://codeforces.com/predownloaded/cb/d0/cbd0c456c1bf9e00bb08ac4ae776710eb9d9348f.png)
题目大意:一个n边形,从1号点开始,每次走到(x+k-1)%n+1位置,问每次留下来的路径把这个多边形划分成了几个部分。
解题思路:新加入一条直线后的区域块数 = 原有的区域块数 + 与该直线相交直线的条数 +1,在查询与新加入直线相交直线的条数时可以用树状数组
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
using namespace std;
#define LL long long
const int maxn = 1e6+10;
int a[maxn],n,k;
int lowbit(int x)
{
return x&-x;
}
int getsum(int x)
{
int sum=0;
while(x>0)
{
sum+=a[x];
x-=lowbit(x);
}
return sum;
}
void add(int x)
{
while(x<=n)
{
a[x]+=1;
x+=lowbit(x);
}
}
int main()
{
while(~scanf("%d %d",&n,&k))
{
LL ans=1;
int s=1,e;
memset(a,0,sizeof(a));
if(k>(n/2)) k=n-k;
for(int i=0; i<n; ++i)
{
e=s+k;
if(e>n) e-=n;
if(s<e) ans+=getsum(e-1)-getsum(s)+1;
else ans+=getsum(n)-getsum(s)+getsum(e-1)+1;
if(i!=n-1) printf("%lld ",ans);
else printf("%lld\n",ans);
add(s);
add(e);
s=e;
}
}
return 0;
}