题目链接:https://vjudge.net/problem/HackerRank-leonardo-and-lucky-numbers

题解:

1.根据扩展欧几里得:7*x + 4*y = gcd(7,4) = 1,必有整数解,其中一组为(-1,2),通解为:(-1+4*k, 2-7*k)。

2.当:7*x + 4*y = n,其中一组解为(-n,2*n),通解为:(-n+4*k, 2*n-7*k)。

3.若要上式有解,则通解(-n+4*k, 2*n-7*k)中必须至少有一对非负的整数解。

4. x = -n+4*k >=0   &&   y =  2*n-7*k >=0 ,推出k的范围:  n/4<=k<=2n/7。然后再在这个范围内枚举k,得到x,y,x、y为非负数,并且7x+4y = n。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
//#define LOCAL
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = +; int main()
{
LL q, n;
scanf("%lld",&q);
while(q--)
{
LL x, y, k, n;
int B = ;
scanf("%lld",&n);
for(k = n/-; k<=(*n)/+; k++) //除法可能除不尽,所以要左右各扩一个单位
{
x = -1LL*n+1LL**k;
y = 1LL**n-1LL**k;
if(x< ||y<) continue;
if(*x+*y==n)
{
B = ;
break;
}
}
if(B) puts("Yes");
else puts("No");
}
}

HackerRank leonardo-and-lucky-numbers —— 模线性方程的通解的更多相关文章

  1. [ACM_其他] Modular Inverse [a关于模m的逆 模线性方程]

    Description The modular modular multiplicative inverse of an integer a modulo m is an integer x such ...

  2. HDU 5676 ztr loves lucky numbers (模拟)

    ztr loves lucky numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/I Description ztr ...

  3. codeforces 630C Lucky Numbers

    C. Lucky Numbers time limit per test 0.5 seconds memory limit per test 64 megabytes input standard i ...

  4. POJ2115——C Looooops(扩展欧几里德+求解模线性方程)

    C Looooops DescriptionA Compiler Mystery: We are given a C-language style for loop of type for (vari ...

  5. POJ2115 C Looooops 模线性方程(扩展欧几里得)

    题意:很明显,我就不说了 分析:令n=2^k,因为A,B,C<n,所以取模以后不会变化,所以就是求(A+x*C)%n=B 转化一下就是求 C*x=B-A(%n),最小的x 令a=C,b=B-A ...

  6. C Looooops(扩展欧几里得求模线性方程)

    http://poj.org/problem?id=2115 题意:对于C的循环(for i = A; i != B; i+=C)问在k位存储系统内循环多少次结束: 若循环有限次能结束输出次数,否则输 ...

  7. hdu 5676 ztr loves lucky numbers(dfs+离线)

    Problem Description ztr loves lucky numbers. Everybody knows that positive integers are lucky if the ...

  8. codeforces 630C - Lucky Numbers 递推思路

    630C - Lucky Numbers 题目大意: 给定数字位数,且这个数字只能由7和8组成,问有多少种组合的可能性 思路: 假设为1位,只有7和8:两位的时候,除了77,78,87,88之外还哇哦 ...

  9. poj_2115C Looooops(模线性方程)

    题目链接:http://poj.org/problem?id=2115 C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

随机推荐

  1. ionic build Android错误记录 error in opening zip file

    0.写在前头 运行 :cordova requirements Requirements check results for android: Java JDK: installed 1.8.0 An ...

  2. JDK1.8中的Lambda表达式和Stream

    1.lambda表达式 Java8最值得学习的特性就是Lambda表达式和Stream API,如果有python或者javascript的语言基础,对理解Lambda表达式有很大帮助,因为Java正 ...

  3. 【spring mvc】后台的API,测试中,总提示接口实体的某一个字段不能为null,但是明明给值了还提示不能为空

    实体是这三个字段 接口的实现类Controller 前台测试给值 依旧报错 解决方法: 需要添加@RequestBody注解

  4. 【mybatis】mybatis中insert操作,返回自增id

    需求是这样的: mybatis中insert操作,返回自增id,因为这个自增id需要给后续业务用到. 原本是这样的: 将insert语句传入,正常执行insert操作,返回int永远是 0[失败] 或 ...

  5. OpenGL step to step(2)

    这是一个类似于地球绕太阳旋转的demo 原有的例子是用键盘接受事件,我做了修改,使用了timer把他变成一个动态旋转的 #import <Foundation/Foundation.h> ...

  6. linux查看 cpu及内存和硬盘使用情况的命令top

    使用时输入 top,退出时输入q http://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316399.html 简介 top命令是Linux下常用的 ...

  7. ubuntu安装 uwsgi

    http://www.jianshu.com/p/e6ff4a28ab5a/ sudo apt-get install python-dev #不安装这个,下面的安装可能会失败 sudo pip in ...

  8. SilverLight-DataBinding:二、Bingding to a Collection Objects(绑定一个集合对象)

    ylbtech-SilverLight-DataBinding: Bingding to a Collection Objects(绑定一个集合对象) 1.A, Building  a Data Ob ...

  9. spl_autoload_register的使用

    class Loader{ static function loadClass($class) { $class = $class.'.php'; if(file_exists($class)) { ...

  10. 【转】Linux上的free命令详解

    解释一下Linux上free命令的输出.默认输出是KB,可以用free -m则输出是MB 下面是free的运行结果,一共有4行.为了方便说明,我加上了列号.这样可以把free的输出看成一个二维数组FO ...