题目戳这里

我们只需要计算每增加一条线后穿过了几条已有的线即可。为了方便,我们令\(K \le N/2\),并且给每条线一个方向,即\(x\)到\((x+K) \; mod \; N\)。然后我们假设现在我们正在链接\(a\)到\((a+K) \; mod \; N\)这条线,于是他穿过的线就是从\((a-K,a+k)\)(模\(K\)意义下)$这段区间中点射出边的数目,拿个树状数组维护一下即可。

#include<cstdio>
#include<cstdlib>
using namespace std; typedef long long ll;
#define lowbit(a) (a&-a)
const int maxn = 1000010;
int N,K,now,tree[maxn]; ll ans = 1LL; inline void ins(int a) { for (++a;a <= N;a += lowbit(a)) ++tree[a]; }
inline ll calc(int a) { int ret = 0; for (++a;a;a -= lowbit(a)) ret += tree[a]; return (ll)ret; } int main()
{
freopen("755D.in","r",stdin);
freopen("755D.out","w",stdout);
scanf("%d %d",&N,&K); if (N - K < K) K = N-K;
do
{
if (now+K-1<N)
{
ans += calc(now+K-1)-calc(now);
if (now - K < 0) ans += calc(now-1)+calc(N-1)-calc(now-K+N);
else ans += calc(now-1)-calc(now-K);
}
else
{
ans += calc(N-1)-calc(now)+calc(now+K-1-N);
if (now - K < 0) ans += calc(now-1)+calc(N-1)-calc(now-K+N);
else ans += calc(now-1)-calc(now-K);
}
ins(now); now += K; if (now >= N) now -= N;
printf("%lld ",++ans);
}
while (now);
fclose(stdin); fclose(stdout);
return 0;
}

Codeforces755D PolandBall and Polygan的更多相关文章

  1. codeforces 755C. PolandBall and Forest

    C. PolandBall and Forest time limit per test 1 second memory limit per test 256 megabytes input stan ...

  2. codeforces 755D. PolandBall and Polygon

    D. PolandBall and Polygon time limit per test 4 seconds memory limit per test 256 megabytes input st ...

  3. Codeforces 755F PolandBall and Gifts bitset + 二进制优化多重背包

    PolandBall and Gifts 转换成置换群后, 对于最大值我们很好处理. 对于最小值, 只跟若干个圈能否刚好组能 k 有关. 最直观的想法就是bitset优化背包, 直接搞肯定T掉. 我们 ...

  4. codeforces 755F F. PolandBall and Gifts(贪心+多重背包)

    题目链接: F. PolandBall and Gifts time limit per test 1.5 seconds memory limit per test 256 megabytes in ...

  5. Codeforces 755 F. PolandBall and Gifts 多重背包+贪心

    F. PolandBall and Gifts   It's Christmas time! PolandBall and his friends will be giving themselves ...

  6. 【codeforces 755B】PolandBall and Game

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【codeforces 755A】PolandBall and Hypothesis

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【codeforces 755D】PolandBall and Polygon

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【codeforces 755C】PolandBall and Forest

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. .NET向WebService传值为decimal、double、int、DateTime等非string类型属性时,服务器端接收不到数据的问题

    最近在做CRM项目时,使用C#调用SAP PI发布的WebService服务时遇到的问题: 向WebService传值为decimal.double.int.DateTime等非string类型数据时 ...

  2. Python基本数据类型及使用

    # 基本数据类型分类 - int 整数 - float 小数 - bool 布尔值 - str 字符串 ## int 整数 - 包括正整数和负整数 - 与java.c等语言相比并没有位数限制,理论上可 ...

  3. pageScope、requestScope、sessionScope、applicationScope的区别

    https://www.cnblogs.com/qianbaidu/p/6006459.html 1.区别: 1.page指当前页面有效.在一个jsp页面里有效 2.request 指在一次请求的全过 ...

  4. C语言函数篇(一)函数的组成

    函数的组成: 函数名 输入参数 返回值 返回值 函数名 (输入参数){ 执行体 } 用指针保存函数: int func(int a, int b, char c){ } --> int (*fu ...

  5. PHP.TP框架下商品项目的优化3-php封装下拉框函数

    php封装下拉框函数 因为在项目中会经常使用到下拉框,所以根据一个表中的数据制作下拉框函数,以便调用 //使用一个表的数据做下拉框函数 function buildSelect($tableName, ...

  6. python ranndom模块及生成验证码

    python的random模块用于生成随机数,下面介绍一下random模块的常用方法: 取随机小数: 数学计算 random.random() 用于生成一个0-1的随机浮点数 0<=n<1 ...

  7. django之python3.4及以上连接mysql的一些问题记录

    首先,祭出大杀器whl https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient django1.x与django2.x 在项目的写法上有一些区别 ...

  8. 如何在一个顶级域名下用两个二级域名访问vps下的两个项目网站--完美解决骗

    本人是原址是http://www.webzhe.com/server/340 后经过本人的实践修改,增加截图,等具体的步骤,完美解决 如何在vps中设置二级域名开通子网站,这个问题涉及到两步:一首先要 ...

  9. QQ空间相册展示特效

    <!doctype html> <html lang="en"> <head> <title>QQ空间相册展示特效<title ...

  10. 【java下午茶】12306的双人票

    明天下午就要和客户谈需求了,今天还在列车上假象着明天的情景,由于这是一个旅游的项目,所以想尽可能设计得人性化一些. 不过有件很不爽的事情就是和老公的位子是分开的,虽然我们订的是连坐号.就这个问题也是我 ...