Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/gym/100463
Description
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); } }
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/gym/100463
Description
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); } }