[类欧几里得算法 数学技巧] 51Nod 1132 覆盖数字的数量 V2 & Ural 1797 Summit Online Judge. Version 2

传送门:http://acm.timus.ru/problem.aspx?space=1&num=1797

讨论帖 膜拜各路神犇:http://www.51nod.com/question/index.html#!questionId=683

分类讨论 n>=x*y 以及 n<x*y 详见讨论帖  


#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long ll;

inline char nc(){
  static char buf[100000],*p1=buf,*p2=buf;
  if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
  return *p1++;
}

inline void read(ll &x){
  char c=nc(),b=1;
  for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
  for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc());
}

inline ll solve(ll n,ll y,ll x){
  if (x==1){
    ll m=n/y;
    return (m+1)*n-m*(m+1)/2*y;
  }
  ll a=y/x,b=y%x,m=n/y,c=(n-m*b)/x,t=n-x*c;
  ll tem=c*(m+1)-m*(m+1)/2*a;
  if (t>=x) tem+=solve(t,x,b)+t/x-t/b;
  return tem;
}

inline ll Calc(ll n,ll x,ll y){
  if (x>y) swap(x,y);
  ll d=__gcd(x,y);
  x/=d; y/=d; n/=d;
  if (n<y) return n/x+1;
  if ((long double)x*y<=n) return n-(x-1)*(y-1)/2+1;
  return solve(n,y,x)+n/y+1;
}

int main(){
  ll x,y,s,t,Q;
  freopen("t.in","r",stdin);
  freopen("t.out","w",stdout);
  read(Q);
  while (Q--){
    read(x); read(y); read(s); read(t);
    printf("%lld\n",Calc(t,x,y)-Calc(s-1,x,y));
  }
  return 0;
}


自己推类欧的过程




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值