题目:https://www.hackerrank.com/contests/hourrank-16/challenges/leonardo-and-lucky-numbers/problem
题意:判断N是否是4和7的和
思路:
* 1. 7*x+4*y=N 扩展gcd
* 2.两边都除以4,下图为官方题解
*
7*Y <= N
7*0%4 = 0 N>=0
7*1%4 = 1 N>=7
7*2%4 = 2 N>=14
7*3%4 = 3 N>=21
代码:
#include<iostream>
using namespace std;
typedef long long ll;
ll exGcd(ll a,ll b,ll &x,ll &y)
{
if(b == 0)
{
x = 1; y = 0;
return a;
}
ll r = exGcd(b,a%b,x,y);
ll t = x;
x = y;
y = t - a/b*y;
return r;
}
int main()
{
int q;
ll n;
cin >> q;
while(q--)
{
cin >> n;//7*x+4*y=n
ll x = 0,y = 0;
ll d = exGcd(7,4,x,y);
if(n % d != 0)
cout << "No" << "\n";
else
{
x = (x*n%4 + 4) % 4;
y = (n - 7*x) / 4;
if(y >= 0)
cout << "Yes" << "\n";
else
cout << "No" << "\n";
}
}
return 0;
}