【数论】卡片

D e s c r i p t i o n Description Description

I n p u t Input Input

O u t p u t Output Output

S a m p l e Sample Sample I n p u t Input Input#1
2 4 3 4
S a m p l e Sample Sample I n p u t Input Input#2
3 4 4 4
S a m p l e Sample Sample I n p u t Input Input#3
2020 1024 2021 1025
S a m p l e Sample Sample O u t p u t Output Output#1
8
S a m p l e Sample Sample O u t p u t Output Output#2
24
S a m p l e Sample Sample O u t p u t Output Output#3
828200

H i n t Hint Hint

样例解释1

T r a i n Train Train o f of of T h o u g h t Thought Thought

数论
先求出一共需要滚多远
也就是大正方形周长与小正放形边长的最小公倍数(x
那么当不存在样例解释中第二、三张图转角时需要两步的情况
答案就是x / a步
但是因为存在两步转角
所以要加上到达原本位置的路途上有多少个两步转角
然后我们可以设从开始到第一次刚好到b的最左边的距离为y
这个距离内,所有转角都需要两步
然后答案就是x / a加上总距离x除与y再乘上(y / b - 1)(在y距离中有多少个转角)

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define ll long long
using namespace std;

ll n, m, a, b;

ll gcd(ll a, ll b) 
{
    
    return (b) ? gcd(b, a % b) : a;}

int main()
{
    
    
	scanf("%lld%lld%lld%lld", &a, &m, &b, &n);
	ll x = a * (n * b) / gcd(a, n * b);
	ll l = x / a;
	ll y = a * b / gcd(a, b);
	ll r = x / y * (y / b - 1);
	printf("%lld", l + r);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/SSL_wujiajie/article/details/109430244