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. JVM垃圾回收机制二

    对象的回收 垃圾的回收涉及的几个问题:何时回收,由谁回收,怎样回收.这几个问题我们一一来解决. 1.何时回收----对象的生死判定 对象达到什么条件才能判断这个对象已经无用了.常见的判断对象生死的方法 ...

  2. MySQL常用函数使用示例

    #从指定字符中,随机生成12位字符select substring('0123456789abcdefghijklmnopqrstuvwxyz',floor(0+RAND()*36),12); #显示 ...

  3. 忘记Centos7.2下root用户密码后的处理方式

    1)重启系统 重新启动系统后并按f2键,进入如下的界面,再按e键. 2)修改启动内核代码 在代码的linux16行中,将ro rhgb的ro修改为rw init=/sysroot/bin/sh. 3) ...

  4. sqlserver 视图用 case when

    视图用 case when 需要 用如下格式,[需要的列名]= case when...,而表里面的case 不用这样 [isNormal]=CASE WHENdbo.c_bdm_head.I_E_F ...

  5. HDU 1398 Square Coins 平方硬币 (普通母函数,水)

    题意: 有17种硬币,每种的面值为编号的平方,比如 1,4,9,16.....给出一个数字,求组成这个面值有多少种组法? 思路: 用普通母函数解,主要做的就是模拟乘法,因为硬币是无限的,所以每个构造式 ...

  6. The Django Book - 第四章 模板2

    模板(相应)使用的几种方式: 1.使用HttpResponse返回字符串HTML from django.http import HttpResponse def current_datetime(r ...

  7. 怎么在webstorm中设置代码模板

    大家都知道webstorm对程序员来说是一个很好用的IDE.我们输入几个关键字,webstorm就会给出提示,大大提高了我们的开发效率,可有时候webstorm的默认设置不能满足我们的个性化代码模板的 ...

  8. 树状数组 简单题 cf 961E

    题目链接 : https://codeforces.com/problemset/problem/961/E One day Polycarp decided to rewatch his absol ...

  9. C++ _ const的用法,特别是用在函数前面与后面的区别!

    在普通的非 const成员函数中 this的类型是一个指向类类型的 const指针.可以改变this所指向的值,但不能改变 this所保存的地址. 在 const成员函数中 this的类型是一个指向 ...

  10. ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061) : 第一次设置MySQL也适用

    [MySQL的安装环境]:windows7 64位 [MySQL的版本]:mysql-8.0.16-winx64 [错误描述]: ERROR 2003 (HY000): Can't connect t ...