bzoj2987&luogu5171 Earthquake

题目链接:luogu5171

这玩意在bzoj上是个权限题

统计\(ax+by\leq c\)的非负正整数解,考虑将原式变形成\(y\leq\frac{-ax+c}{b}\)

我们枚举\(x\)可以得到最后的答案就是\(\sum_{x=0}^{\lfloor\frac{c}{a}\rfloor}(\frac{-ax+b}{b}+1)=\sum_{x=0}^{\lfloor\frac{c}{a}\rfloor}\frac{-ax+b+c}{b}\)

直接令\(A=-a,B=b+c,C=b,N=\lfloor\frac{c}{a}\rfloor\),跑类欧即可

类欧可以参见:https://www.cnblogs.com/encodetalker/p/11037506.html

#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
#define lowbit(x) (x)&(-x)
#define sqr(x) (x)*(x)
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define fir first
#define sec second
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define maxd 998244353
#define eps 1e-8
ll a,b,c;
ll read()
{
    ll x=0;int 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;
}

ll gcd(ll x,ll y)
{
    if ((!x) || (!y)) return x+y;
    return gcd(y,x%y);
}

ll work(ll a,ll b,ll c,ll n)
{
    if (!a) return (n+1)*(b/c);
    if (!n) return b/c;
    if (n==1) return (a+b)/c+b/c;
    if (c<0) return work(-a,-b,-c,n);
    ll d=abs(gcd(gcd(a,b),c));
    a/=d;b/=d;c/=d;
    if ((a>=c) || (b>=c)) return work(a%c,b%c,c,n)+n*(n+1)/2*(a/c)+(n+1)*(b/c);
    if ((a<0) || (b<0)) return work(a%c+c,b%c+c,c,n)+n*(n+1)/2*(a/c-1)+(n+1)*(b/c-1);
    ll m=(a*n+b)/c;
    return n*m-work(c,c-b-1,a,m-1);
}

int main()
{
    a=read();b=read();c=read();
    printf("%lld",work(-a,b+c,b,c/a));
    return 0;
}
posted @ 2019-06-17 00:06  EncodeTalker  阅读(135)  评论(0编辑  收藏  举报