Gym 100463A Crossings (reversed pairs of tree-like arrays)

Crossings

Time Limit: 20 Sec

Memory Limit: 256 MB

topic link

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

 Ideas:

Clearly reversed pair. . Calculate each value according to the formula, and then directly find the number of pairs in reverse order.

Note that to open long long or not time out. . . Because the value of the intermediate calculation formula exceeds the in range t,

If you don't open long long, you will fall into an infinite loop

Implementation code:

//#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 + 10;
const double EPS = 1e-8;
//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 = 0;
    while(x>0){
        sum += c[x];
        x -= lowbit(x);
    }
    return sum;
}

void update(int x,int value){
    while(x<=n){
        c[x] += value;
        x += lowbit(x);
    }
}

intmain ()
{
    ll a,b;
    int cal = 1;
    while(scanf("%lld%lld%lld",&n,&a,&b)!=EOF){
        memset(c,0,sizeof(c));
        if(n==0&&a==0&&b==0) break;
        for(int i = 1;i <= n;i ++){
            l[i] = ((i-1)*a+b)%n+1;
        }
        ll ans = 0;
        for(int i = 1;i <= n;i ++){
            update(l[i],1);
            ans +=i - getsum(l[i]);
        }

        printf("Case %d: %lld\n",cal++,ans);
    }
    return 0;
}

 

Time Limit: 20 Sec

Memory Limit: 256 MB

topic link

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

 Ideas:

Clearly reversed pair. . Calculate each value according to the formula, and then directly find the number of pairs in reverse order.

Note that to open long long or not time out. . . Because the value of the intermediate calculation formula exceeds the in range t,

If you don't open long long, you will fall into an infinite loop

Implementation code:

//#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 + 10;
const double EPS = 1e-8;
//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 = 0;
    while(x>0){
        sum += c[x];
        x -= lowbit(x);
    }
    return sum;
}

void update(int x,int value){
    while(x<=n){
        c[x] += value;
        x += lowbit(x);
    }
}

intmain ()
{
    ll a,b;
    int cal = 1;
    while(scanf("%lld%lld%lld",&n,&a,&b)!=EOF){
        memset(c,0,sizeof(c));
        if(n==0&&a==0&&b==0) break;
        for(int i = 1;i <= n;i ++){
            l[i] = ((i-1)*a+b)%n+1;
        }
        ll ans = 0;
        for(int i = 1;i <= n;i ++){
            update(l[i],1);
            ans +=i - getsum(l[i]);
        }

        printf("Case %d: %lld\n",cal++,ans);
    }
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325125639&siteId=291194637