Crossings

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/gym/100463

Description

Given a permutation P of {0, 1, ..., n − 1}, we define the crossing number of it as follows. Write the sequence 0, 1, 2, . . . , n − 1 from left to right above the sequence P(0), P(1), . . . , P(n − 1). Draw a straignt line from 0 in the top line to 0 in the bottom line, from 1 to 1, and so on. The crossing number of P is the number of pairs of lines that cross. For example, if n = 5 and P = [1, 3, 0, 2, 4], then the crossing number of P is 3, as shown in the figure below. !""""#""""""""""""&" In this problem a permutation will be specified by a tuple (n, a, b), where n is a prime and a and b are integers (1 ≤ a ≤ n − 1 and 0 ≤ b ≤ n − 1). We call this permutation Perm(n, a, b), and the ith element of it is a ∗ i + b mod n (with i in the range [0, n − 1]). So the example above is specified by Perm(5, 2, 1).

Input

There are several test cases in the input file. Each test case is specified by three space-separated numbers n, a, and b on a line. The prime n will be at most 1,000,000. The input is terminated with a line containing three zeros.

Output

For each case in the input print out the case number followed by the crossing number of the permutation. Follow the format in the example output.

Sample Input

5 2 1

19 12 7

0 0 0

Sample Output

Case 1: 3

Case 2: 77

给你三个数n,a,b

满足第i个数等于(a*i+b)%n,然后问你逆序数是多少

这个n有1e6呢,所以树状数组和归并都能解决这个题吧

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+;
int d[N];
ll n,a,b;
void update(int x,int y) {
while(x<=n) {
d[x]+=y;
x+=x&-x;
}
}
int sum(int x) {
int s=;
while(x>) {
s+=d[x];
x-=x&-x;
}
return s;
}
int main() {
int k=;
while(~scanf("%lld%lld%lld",&n,&a,&b)) {
if(!n&&!a&&!b)
break;
memset(d,,sizeof(d));
ll ans=;
for(int i=; i<n; i++) {
int x=(a*i+b)%n+;
ans+=sum(x-);
update(x,);
}
printf("Case %d: %lld\n",k++,(n-)*n/-ans);
}
}

2011 Michigan Invitational Programming Contest的更多相关文章

  1. The North American Invitational Programming Contest 2017 题目

    NAIPC 2017 Yin and Yang Stones 75.39% 1000ms 262144K   A mysterious circular arrangement of black st ...

  2. 2014 ACM-ICPC Beijing Invitational Programming Contest

    点击打开链接 Happy Reversal Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      J ...

  3. The North American Invitational Programming Contest 2018 D. Missing Gnomes

    A family of nn gnomes likes to line up for a group picture. Each gnome can be uniquely identified by ...

  4. The North American Invitational Programming Contest 2018 H. Recovery

    Consider an n \times mn×m matrix of ones and zeros. For example, this 4 \times 44×4: \displaystyle \ ...

  5. The North American Invitational Programming Contest 2018 E. Prefix Free Code

    Consider nn initial strings of lower case letters, where no initial string is a prefix of any other ...

  6. North American Invitational Programming Contest (NAIPC) 2017

    (待补) A. Pieces of Parentheses 将括号处理完成后排序,方式参加下面的博客.然后做一遍背包即可. 2018 Multi-University Training Contest ...

  7. North American Invitational Programming Contest (NAIPC) 2016

    (待补) A. Fancy Antiques 爆搜. B. Alternative Bracket Notation C. Greetings! D. Programming Team 0/1分数规划 ...

  8. North American Invitational Programming Contest 2018

    A. Cut it Out! 枚举第一刀,那么之后每切一刀都会将原问题划分成两个子问题. 考虑DP,设$f[l][r]$表示$l$点顺时针一直到$r$点还未切割的最小代价,预处理出每条边的代价转移即可 ...

  9. BNU 34990 Justice String 2014 ACM-ICPC Beijing Invitational Programming Contest

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34990 DEBUG了非常久,还是legal的推断函数写错了... 此题做法.枚举Stri ...

随机推荐

  1. Java程序流程控制之if-else if-else

    java基础之流程控制(一)    流程控制 Flow Control :       流程控制语句是编程语言中的核心之一.可以分为 分支语句.循环语句和跳转语句.        本讲内容包括分支语句 ...

  2. linux winqq 不能输入中文的解决办法

    wineqq的运行脚本是/usr/share/deepinwine/qqintl/wine-qqintl编辑此脚本,在最开始加入: export XMODIFIERS="@im=fcitx& ...

  3. 使用POI创建word表格-在表格单元格中创建子表格

    要实现的功能如下:表格中的单元格中有子表格 实现代码如下: XWPFParagraph cellPara = row.getCell(j).getParagraphArray(0); //row.ge ...

  4. ef导航属性

    https://msdn.microsoft.com/en-us/data/jj574232.aspx  场景是   A表中有B,B表中又C.都是一堆多的关系.怎样Mapping是个问题啊.  var ...

  5. Android学习总结(三)——IntentService的用法

    一.IntentService的基本概念 IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentServi ...

  6. 如何计算CDS view里两个时间戳之间的天数间隔

    ABAP透明表里的时间戳,数据类型为dec: 有个需求:计算这两个时间戳之间的天数间隔,丢弃时间戳年-月-日8位后面的小时:分钟:秒. 举个例子:如果时间戳是20180918173132,丢弃1731 ...

  7. GUI进化--数据与界面分离

    http://blog.csdn.net/doon/article/details/5946862 1.何谓数据和界面分离? GUI,即Graphic User Interface,人机交换界面.连接 ...

  8. VM虚拟机下的Linux不能上网

    虚拟机linux上网配置 图解教程 首先查看window7主机下的网络配置VMNet1或VMNet8是否开启,其实linux系统的网络连接跟linux系统一致 在虚拟机界面将桥接改为NAT连接 点虚拟 ...

  9. Windows MinGW 64-bit boost 踩坑

    >g++ -Wall -shared -g -DBUILD_DLL main.cpp -ID:\gcc\boost\include\boost-1_69 -LD:\gcc\boost\lib - ...

  10. HTML网页的浏览器标题栏显示小图标的方法

    HTML网页的浏览器标题栏显示小图标的方法   就像这种效果,方法其实很简单,就是 在head头部里写: <link rel='icon' href='pic.ico ' type='image ...