题目戳这里

我们只需要计算每增加一条线后穿过了几条已有的线即可。为了方便,我们令\(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. BDC备忘

    更新模式,有下列可选值(更新模式常用的是S)   "A" 异步更新.被调用程序的更新按照没有指定 COMMIT WORK 语句和 AND WAIT 附加的方式执行.       也 ...

  2. hive_异常_01_(未解决)FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. org.apache.hadoop.hbase.HTableDescriptor.addFamily(Lorg/apache/hadoop/hbase/HColumnDescriptor;)V

    一.如果出现如下错误需要编译源码 需要重新编译Hbase-handler源码 步骤如下: 准备Jar包: 将Hbase 中lib下的jar包和Hive中lib下的jar包全部导入到一起. 记得删除里面 ...

  3. flask-login原理详解

    最近发现项目中使用的flask-login中有些bug,直接使用官网的方式确实可以用,但仅仅是可以用,对于原理或解决问题没有什么帮助,最近通过查看网上资料.分析源码.通过demo.从零开始总结了fla ...

  4. python3爬取咪咕音乐榜信息(附源代码)

    参照上一篇爬虫小猪短租的思路https://www.cnblogs.com/aby321/p/9946831.html,继续熟悉基础爬虫方法,本次爬取的是咪咕音乐的排名 咪咕音乐榜首页http://m ...

  5. python文件操作练习之文件备份

    文件备份 ## 文件备份 # 打开文件 def backup(file1, file2): with open(file1, 'rb') as f1,\ open(file2, 'wb') as f2 ...

  6. 笔记-scrapy-辅助功能

    笔记-scrapy-辅助功能 1.      scrapy爬虫管理 爬虫主体写完了,要部署运行,还有一些工程性问题: 限频 爬取深度限制 按条件停止,例如爬取次数,错误次数: 资源使用限制,例如内存限 ...

  7. Samba和NFS文件共享

    SAMBA文件共享服务 通过Yum软件仓库来安装Samba服务程序 [root@zhangjh ~]# yum install samba -y Samba 配置文件注释信息较多,为了便于配置,因此先 ...

  8. Android面试收集录 Android入门

    1.Android的特点有哪些? 编程语言是Java或Kotlin,Android中的Java字节码是允许在Dalvik虚拟机上的 支持4大组件 Android内置了WebKit核心的浏览器,支持H5 ...

  9. SetConsoleCtrlHandler

    Excerpt: Registering a Control Handler Function   This is an example of the SetConsoleCtrlHandler fu ...

  10. Java继承的缺点

    转载自:https://www.cnblogs.com/xz816111/archive/2018/05/24/9080173.html JAVA中使用到继承就会有两个无法回避的缺点: 1.打破了封装 ...