标签:
Time Limit: 20 Sec
Memory Limit: 256 MB
http://codeforces.com/gym/100463
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个数, 第i-1个数等于(a*i+b)%n,问逆序对有多少
题解:
树状数组
代码
1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 #include <ctime> 5 #include <iostream> 6 #include <algorithm> 7 #include <set> 8 #include <vector> 9 #include <queue> 10 #include <map> 11 #include <stack> 12 #define MOD 1000000007 13 #define maxn 32001 14 using namespace std; 15 typedef __int64 ll; 16 inline ll read() 17 { 18 ll x=0,f=1; 19 char ch=getchar(); 20 while(ch<‘0‘||ch>‘9‘) 21 { 22 if(ch==‘-‘)f=-1; 23 ch=getchar(); 24 } 25 while(ch>=‘0‘&&ch<=‘9‘) 26 { 27 x=x*10+ch-‘0‘; 28 ch=getchar(); 29 } 30 return x*f; 31 } 32 //******************************************************************* 33 34 struct ss 35 { 36 int v,index; 37 } in[1000005]; 38 int c[1000005]; 39 int a[1000005]; 40 ll n; 41 bool cmp(ss s1,ss s2) 42 { 43 return s1.v<s2.v; 44 } 45 int lowbit(int x) 46 { 47 return x&(-x); 48 } 49 int getsum(int x) 50 { 51 int sum=0; 52 while(x>0) 53 { 54 sum+=c[x]; 55 x-=lowbit(x); 56 } 57 return sum; 58 } 59 void update(int x,int value) 60 { 61 while(x<=n) 62 { 63 c[x]+=value; 64 x+=lowbit(x); 65 } 66 } 67 68 int main() 69 { 70 ll x,y; 71 int oo=1; 72 while(scanf("%lld%lld%lld",&n,&x,&y)!=EOF) 73 { 74 if(n==0&&x==0&&y==0)break; 75 memset(c,0,sizeof(c)); 76 for(int i=0; i<n; i++) 77 { 78 a[i+1]=(x*i+y)%n+1; 79 } 80 ll ans=0; 81 for(int i=1; i<=n; i++) 82 { 83 update(a[i],1); 84 ans+=i-getsum(a[i]); 85 } 86 printf("Case %d: ",oo++); 87 cout<<ans<<endl; 88 } 89 90 91 return 0; 92 }
标签:
原文地址:http://www.cnblogs.com/zxhl/p/4665453.html