思路:定义每个数为(a*i+b)%n,然后求这个序列的逆序,直接树状数组搞
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn = 1000000+500;
int c[maxn];
int lowbit(int x){return x&(-x);}
void update(int i,int d)
{
while(i<=maxn)
{
c[i]+=d;
i+=lowbit(i);
}
}
int query(int i)
{
int ans = 0;
while(i)
{
ans+=c[i];
i-=lowbit(i);
}
return ans;
}
int main()
{
int cas=1;
LL n,a,b;
while(scanf("%lld%lld%lld",&n,&a,&b)!=EOF)
{
memset(c,0,sizeof(c));
if(n==0 &&a==0&&b==0)
break;
LL ans = 0;
for(int i = 0;i<n;i++)
{
LL num = (a*i+b)%n+1;
ans+=query(num-1);
update(num,1);
}
printf("Case %d: %lld\n",cas++,n*(n-1)/2-ans);
}
}