hdu 4611 模拟+规律
2013-07-26 20:32
232 查看
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #define LL __int64 using namespace std; LL gcd(LL a,LL b){ return b==0?a:gcd(b,a%b); } LL lcm(LL a,LL b){ return a/gcd(a,b)*b; } LL solve(LL n,LL a,LL b){ LL sa=0,sb=b,ans=0,LCM; LCM=lcm(a,b); while(sa<n){ if(sa<=sb && sa+a>sb){ ans+=(sb-sa)*(sa%b)+(sa+a-sb)*(sb%a); sb+=b; sa+=a; } else{ ans+=(sa%b)*a; sa+=a; } if(sa==LCM){ LL k=n/LCM; ans*=k; sa=n/LCM*LCM; sb=sa+b; } } LL i; if(sa>=n){ for(i=sa-1;i>=n;i--){ ans-=fabs(i%a-i%b); } } return ans; } int main(){ int t; scanf("%d",&t); while(t--){ LL n,a,b; LL tt; scanf("%I64d%I64d%I64d",&n,&a,&b); if(a>b){ tt=a; a=b; b=tt; } printf("%I64d\n",solve(n,a,b)); } return 0; }
相关文章推荐
- HDU 5047 Sawtooth 规律+ C++大数模拟 2014 ACM/ICPC Asia Regional Shanghai Online
- HDU 5308 规律+模拟
- HDU 6121 Build a tree(找规律+模拟)
- HDU-4611 Balls Rearrangement 循环节,模拟
- HDU 4588 Count The Carries(找规律,模拟)
- HDU - 5929 Basic Data Structure(模拟+规律)
- HDU 4611 Balls Rearrangement(规律)
- HDU 4611 (2013多校联赛1001 数学+模拟)
- hdu 5929 Basic Data Structure 找规律 模拟
- HDU 3257 Hello World!(字符串找规律——位运算来模拟过程)
- hdu 5929 Basic Data Structure(找规律,模拟)
- 规律,模拟,贪心(Travelling Salesman Problem,HDU 5402)
- hdu 1998 奇数阶魔方(找规律+模拟)
- HDU 5308 I Wanna Become A 24-Point Master(找规律+模拟)
- hdu 1998 奇数阶魔方(找规律+模拟)
- hdu 5373 The shortest problem(模拟、数学规律)
- HDU 5319 Painter(模拟 + 规律)——2015 Multi-University Training Contest 3
- 找规律,模拟(HDU 5308,I Wanna Become A 24-Point Master)
- hdu 4611 Balls Rearrangement(规律)
- HDU 5402 Travelling Salesman Problem (模拟 有规律)(左上角到右下角路径权值最大,输出路径)