[类欧几里得算法 数学技巧] 51Nod 1132 覆盖数字的数量 V2 & Ural 1797 Summit Online Judge. Version 2
2016-11-25 20:32
603 查看
传送门: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 详见讨论帖
自己推类欧的过程
讨论帖 膜拜各路神犇: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; }
自己推类欧的过程
相关文章推荐
- 51nod 1132 覆盖数字的数量 V2
- [乱搞 分类讨论 数学] 51Nod 1131 覆盖数字的数量
- 51nod-1131 覆盖数字的数量
- 51nod 1131 覆盖数字的数量
- 51Nod 1009 数字1的数量 (数学
- 51Nod-1131-覆盖数字的数量
- 51nod 1170 1770 数数字(数学技巧)
- 51nod 1009 数字1的数量(数学)
- 【51nod】--机器人走方格V2(组合数学&&逆元&&费马小定理)
- [51NOD] 1009 数字1的数量 [数学][详解]
- 【51Nod】1042 - 数字0-9的数量(数位dp & 递归)
- 51nod 1096 距离之和最小 && 1108-距离之和最小 V2(简单数学)&& 1110 距离之和最小 V3(逆思维)
- 51nod 1009:数字1的数量
- hdoj The sum problem 2058 (数学等差公式&技巧转换)
- CodeForces - 593B Anton and Lines (数学方程&技巧) 判断直线是否相交
- CodeForces - 659D Bicycle Race (数学几何&技巧转换)
- hdoj Rectangles 2056 (数学几何&技巧)求两矩形相交面积
- LA 3708 - Graveyard (数学几何&技巧)
- 51NOD 1009 数字1的数量
- CodeForces - 621B Wet Shark and Bishops (数学几何&技巧)