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

题意

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

题解:

树状数组,大胆上

代码

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 1000101
#define mod 10007
#define eps 1e-9
const int inf=0x7fffffff;   //无限大
/*
inline ll read()
{int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;
}
*/
//**************************************************************************************
int d[maxn];
int c[maxn];
ll n;
int t;
inline int read()
{int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;
}
int lowbit(int x)
{return x&-x;
}void update(int x,int y)
{while(x<=n){d[x]+=y;x+=lowbit(x);}
}
int sum(int x)
{int s=0;while(x>0){s+=d[x];x-=lowbit(x);}return s;
}
int num[maxn];
ll a,b;
int main()
{int t=0;while(scanf("%lld%lld%lld",&n,&a,&b)!=EOF){t++;if(n==0&&a==0&&b==0)break;memset(d,0,sizeof(d));ll ans=0;for(int i=0;i<n;i++){int x=(a*i+b)%n+1;ans+=sum(x-1);update(x,1);}printf("Case %d: %lld\n",t,(n-1)*n/2-ans);}
}

转载于:https://www.cnblogs.com/qscqesze/p/4665222.html

Codeforces Gym 100463A Crossings 逆序数相关推荐

  1. Codeforces 645B Mischievous Mess Makers【逆序数】

    题目链接: http://codeforces.com/problemset/problem/645/B 题意: 给定步数和排列,每步可以交换两个数,问最后逆序数最多是多少对? 分析: 看例子就能看出 ...

  2. 剑指 offer set 22 数组中的逆序数

    总结 1. 题目为归并排序的变形, 不过我完全没想到 2. 在归并排序进行字符组 merge 时, 统计逆序数. merge 后, 两个子数组是有序的了, 下次再 merge 的时候就能以 o(n) ...

  3. 二叉树:二叉搜索树实现 逆序数问题

    关于逆序数的问题描述如下: 已知数组nums,求新数组count,count[i]代表了在nums[i]右侧且比 nums[i]小的元素个数. 例如: nums = [5, 2, 6, 1], cou ...

  4. 递归/归并:count of smaller numbers求逆序数

    已知数组nums,求新数组count,count[i]代表了在nums[i]右侧且比 nums[i]小的元素个数. 例如: nums = [5, 2, 6, 1], count = [2, 1, 1, ...

  5. 求排列的逆序数(分治)

    考虑1,2,-,n (n <= 100000)的排列i1,i2,-,in,如果其中存在j,k,满足 j < k 且 ij > ik, 那么就称(ij,ik)是这个排列的一个逆序. 一 ...

  6. 算法笔记-归并算法面试题、逆序数问题

    1. 题目 逆序对问题:在一个数组中,左边的数如果比右边大,则这两个数构成一个逆序对,请打印所有逆序对 比如说: 数组: 5, 1, 3,4,2 第一个元素是5,其右边有小于5的数,即1,3,4,2, ...

  7. 树状数组 _ 求逆序数

    注: 本文只是记录  ,您将从上面学习不到任何知识,除了 代码 (废话)第一次接触到树状数组,感觉接触到了新世界,理解这个思想用了好长时间,终于弄明白了(似懂非懂). 还有接触到了  离散化的思想, ...

  8. [置顶] 归并排序,逆序数

    写了个模板类,指针 a为被排序,b为tmp 返回逆序数,调用时候Merge.sort(a,b,n); #define ll long long template<class T> clas ...

  9. 分治递归逆序数_[模板] 归并排序 逆序数 分治

    归并排序 图来自维基 递归调用的过程需要在脑中模拟清楚 然后是代码的细节问题 多复习多理解 刘汝佳版 #include using namespace std; const int MAXN = 1e ...

最新文章

  1. 算法----删除链表中的节点(Java)
  2. 浅谈Java反射机制 之 获取类的字节码文件 Class.forName(全路径名) 、getClass()、class...
  3. 傅立叶变换、拉普拉斯变换、Z变换之间 篇一
  4. 影响一生的职业建议 [转] - 看懂了不一定在高位,在高位的必须看懂了、信息量很大,多学学!...
  5. WAMP中的MySQL设置密码(默认密码为空)
  6. JAVA js的escape函数、解析用js encodeURI编码的字符串、utf8转gb2312的函数
  7. virtual box一直正在加载文件_Linux基础导航与文件管理
  8. android开发卡死代码,Android Studio编译卡死(示例代码)
  9. mysql开启远程访问权限
  10. win7 卸载虚拟机重装提示请您确认有足够的权限安装....
  11. rocketmq安装教程以及遇到的坑排查
  12. 软件设计学习笔记2_体系研究与DoD体系架构框架(DoDAF)简介
  13. 用Java代码实现学生管理系统
  14. javascript上传文件到腾讯云COS
  15. 京东把 Elasticsearch 到底用的有多牛?日均5亿订单查询完美解决!
  16. 服务器2008修改虚拟内存,Windows 2008 关闭系统虚拟内存功能 如何删除pagefile.sys
  17. top工具全字段解析+实战(一)
  18. 【LDAP】centos搭建openldap
  19. 计算机语言python-一、计算机语言与python简介
  20. vue 项目进行直播视频 vue-video-player

热门文章

  1. 对一个“算法”的分析
  2. Linux服务器安装svn
  3. Netflix工程总监眼中的分类算法:深度学习优先级最低
  4. 【mongodb系统学习之三】进入mongodb shell
  5. PHP_小数/四舍五入/上进/下取等
  6. 数组中的第K个最大元素
  7. 九个工作谎言_书评:关于工作的九种谎言
  8. cassandra 数据库_使用Apache Cassandra构建分布式NoSQL数据库
  9. 第二十七章:五姓七望
  10. HTML label控件