/*
找规律
题意:abs(i%A - i%B) 对i从0~N-1求和
从0~N-1一个一个算必TLE,着A,B两者差相同的部分合并起来算
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<math.h>
using namespace std;
typedef long long int64;
//typedef __int64 int64;
const int maxn = 105;
const int inf = 0x7fffffff;
const double pi=acos(-1.0);
const double eps = 1e-8;
int64 gcd( int64 a,int64 b ){
int64 r;
while( b ){
r = a%b;
a = b;
b = r;
}
return a;
}
int64 lcm( int64 a,int64 b,int64 Gcd ){
return a*b/Gcd;
}
int64 Abs( int64 a ){
if( a<0 ) return -a;
else return a;
}
int64 solve( int64 n,int64 a,int64 b ){
int64 Index,tmpLen,res,Indexa,Indexb;
res = Index = Indexa = Indexb = 0;
while( Index<n ){
tmpLen = min( a-Indexa,b-Indexb );
if( Index+tmpLen>n ) tmpLen = n-Index;//Index:表示新的开始分割的位置
res += tmpLen*Abs( Indexa-Indexb );
Indexa += tmpLen, Indexa %= a;
Indexb += tmpLen, Indexb %= b;
Index += tmpLen;
}
return res;
}
int main(){
int T;
scanf("%d",&T);
while( T-- ){
int64 n,a,b;
//scanf("%lld%lld%lld",&n,&a,&b);
scanf("%I64d%I64d%I64d",&n,&a,&b);
int64 LCM = lcm( a,b,gcd(a,b) );
int64 ans = 0;
if( LCM>=n ) ans = solve( n,a,b );
else ans = (n/LCM)*solve( LCM,a,b )+solve( n%LCM,a,b );
//printf("%lld\n",ans);
printf("%I64d\n",ans);
}
return 0;
}
分享到:
相关推荐
300+ AC 代码 。 大数 , 线段树 , 字符串 , dp.....
2、new做两件事,一是分配内存,二是调用类的构造函数 3、new建立的是一个对象,而malloc分配的是一块内存 4、new/delete是保留字,不需要头文
本人准备2020年保研机试时刷的题目(虽然最后机试取消了,...来自某中流985,在HDU和vjudge平台上大概刷了400道。本文件地图(excel表格)包含了绝大部分我刷过的题目,笔记中具有思路、代码、总结和心得。 大佬勿入!
ACM题库,一些题目和答案,以及解题报告,传上来共享
杭电OnlineJudge 200-2099的解题报告
acm入门训练和日常训练 对于初学者以及acm爱好者有叫大帮助
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
HDU1059的代码
杭电ACMhdu1163
hdu1001解题报告
HDU的一题........HDU DP动态规
hdu 1574 passed sorce
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
搜索 dfs 解题代码 hdu1241
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
Problem Description 话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫“大明”。 这时他已经不是那个只会做100以内加法的那个“小明”了,现在他甚至会任意长度的正小数的加法。...
HDU最全ac代码