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=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';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=;
while(x>)
{
s+=d[x];
x-=lowbit(x);
}
return s;
}
int num[maxn];
ll a,b;
int main()
{
int t=;
while(scanf("%lld%lld%lld",&n,&a,&b)!=EOF)
{
t++;
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",t,(n-)*n/-ans);
}
}

Codeforces Gym 100463A Crossings 逆序数的更多相关文章

  1. Gym 100463A Crossings 逆序对

    Crossings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463 Description ...

  2. Gym 100463A Crossings (树状数组 逆序对)

    Crossings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463 Description ...

  3. Codeforces Round #261 (Div. 2)459D. Pashmak and Parmida&#39;s problem(求逆序数对)

    题目链接:http://codeforces.com/contest/459/problem/D D. Pashmak and Parmida's problem time limit per tes ...

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

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

  5. Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem (树状数组求逆序数 变形)

    题目链接 题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求i和j的种类数. 我们可以用map预处理出  ...

  6. Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数

                                                                    E. Infinite Inversions               ...

  7. Codeforces Gym 100187K K. Perpetuum Mobile 构造

    K. Perpetuum Mobile Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

  8. CF 61E 树状数组+离散化 求逆序数加强版 三个数逆序

    http://codeforces.com/problemset/problem/61/E 题意是求 i<j<k && a[i]>a[j]>a[k] 的对数 会 ...

  9. poj3067树状数组求逆序数

    Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Jap ...

随机推荐

  1. 开发框架XUtils

    xUtils 包含了很多实用的android工具. xUtils 最初源于Afinal框架,进行了大量重构,使得xUtils支持大文件上传,更全面的http请求协议支持(10种谓词),拥有更加灵活的O ...

  2. Free Candies

    题意: 有4堆东西,每堆有n个每个有一个颜色,现在有一个篮子最多能装5个不同的颜色的东西,每次都从堆顶拿,当篮子出现两个相同颜色,可以获得这两个东西,求获得的最大数量 分析: 因为就4推,可以把各堆的 ...

  3. 编码的UI测试项目——Visual Studio 2013

    今天实现了一次编码的UI测试项目,以下是我进行测试的过程: 1.新建测试项目 在visual studio中(我用的版本是2013 update2)点击文件->新建->项目,选择“编码的U ...

  4. CMDB反思5

    ITSM工具规划设计 http://blog.vsharing.com/xqscool/A946789.html 相比PPT中被管的数个对象(像培训什么的也都在其中),我们的需求其实就要小得多,但是问 ...

  5. pick定理:面积=内部整数点数+边上整数点数/2-1

    //pick定理:面积=内部整数点数+边上整数点数/2-1 // POJ 2954 #include <iostream> #include <cstdio> #include ...

  6. scala: How to write a simple HTTP GET request client in Scala (with a timeout)

    Scala CookBook: http://scalacookbook.com/ @throws(classOf[java.io.IOException]) @throws(classOf[java ...

  7. Application Cache

    轉發處:http://www.cnblogs.com/blackbird/archive/2012/06/12/2546751.html HTML5提供了一系列的特性来支持离线应用: applicat ...

  8. TopFreeTheme精选免费模板【20130701.特别版】

    今天我们整理了16款WordPress和Joomla的最新主题.它们都是来自Themeforest,RocketTheme,YooTheme以及TemPlaza的高质量主题,赶快收藏起来吧. Este ...

  9. 总结调用Flash的几种方法

    一.Adobe 提供的方法 <object width="200" height="200" classid="clsid:D27CDB6E-A ...

  10. 无法debug断点跟踪JDK源代码——missing line number attributes的解决方法

    在项目工程->Properties->Java Build Path->Libraries中导入的JRE System Library库里,给jar包添加JDK源代码包后,能够直接打 ...