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

HINT

思路:

很明显的逆序对。。照着公式把每个值算出来,然后直接求逆序对个数就好了。

注意要开long long 要不会超时。。。因为中间计算公式的时候值是超过了in范围t的,

不开long long就会陷入死循环

实现代码:

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#define ll long long
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1
const int M = 1e6 + ;
const double EPS = 1e-;
//inline int sgn(double x) return (x > EPS) - (x < -EPS); //浮点数比较常数优化写法
int c[M],l[M];
ll n; int lowbit(int x){
return x&(-x);
} int getsum(int x){
int sum = ;
while(x>){
sum += c[x];
x -= lowbit(x);
}
return sum;
} void update(int x,int value){
while(x<=n){
c[x] += value;
x += lowbit(x);
}
} int main()
{
ll a,b;
int cal = ;
while(scanf("%lld%lld%lld",&n,&a,&b)!=EOF){
memset(c,,sizeof(c));
if(n==&&a==&&b==) break;
for(int i = ;i <= n;i ++){
l[i] = ((i-)*a+b)%n+;
}
ll ans = ;
for(int i = ;i <= n;i ++){
update(l[i],);
ans +=i - getsum(l[i]);
} printf("Case %d: %lld\n",cal++,ans);
}
return ;
}

最新文章

  1. WDCP突破phpmyadmin导入文件时只有20M
  2. Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project LogTest: Compilation failure -&gt; [Help 1]
  3. 转 LoadRunner 技巧之协议分析
  4. 【工作笔记】BAT批处理学习笔记与示例
  5. python random
  6. 【项目总结】之——导出Excel
  7. Java 第十章 类和对象
  8. Java日期时间处理常用方法
  9. 网页标签图片如何保存&amp;下载?
  10. android 通过代码设置drawableLeft
  11. SCU 3133(博弈)
  12. Java 之关键字 null 使用总结
  13. 教学服务系统设计之PHP后台设计
  14. python数据结构与算法篇:排序
  15. titanium环境配置
  16. Android手机有的不显示Toast
  17. 记录pageHelper分页orderby的坑
  18. bug处理
  19. 微信小程序阿里云服务器https搭建
  20. C#剪切板

热门文章

  1. day05-列表类型
  2. 创世纪 BZOJ3037 &amp; [Poi2004]SZP BZOJ2068
  3. mac下载、破解、安装webstorm编辑器
  4. ccf201703-2学生排队
  5. 20155234 Exp3 免杀原理与实践
  6. EZ 2018 03 30 NOIP2018 模拟赛(六)
  7. vijos 1641 Vs Snowy
  8. Arcgis安装要素
  9. PowerBI开发 第十三篇:增量刷新
  10. 利用JS实现一个简单的二级联动菜单