HackerRank leonardo-and-lucky-numbers —— 模线性方程的通解
题目链接: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 —— 模线性方程的通解的更多相关文章
- [ACM_其他] Modular Inverse [a关于模m的逆 模线性方程]
Description The modular modular multiplicative inverse of an integer a modulo m is an integer x such ...
- HDU 5676 ztr loves lucky numbers (模拟)
ztr loves lucky numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/I Description ztr ...
- codeforces 630C Lucky Numbers
C. Lucky Numbers time limit per test 0.5 seconds memory limit per test 64 megabytes input standard i ...
- POJ2115——C Looooops(扩展欧几里德+求解模线性方程)
C Looooops DescriptionA Compiler Mystery: We are given a C-language style for loop of type for (vari ...
- 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 ...
- C Looooops(扩展欧几里得求模线性方程)
http://poj.org/problem?id=2115 题意:对于C的循环(for i = A; i != B; i+=C)问在k位存储系统内循环多少次结束: 若循环有限次能结束输出次数,否则输 ...
- hdu 5676 ztr loves lucky numbers(dfs+离线)
Problem Description ztr loves lucky numbers. Everybody knows that positive integers are lucky if the ...
- codeforces 630C - Lucky Numbers 递推思路
630C - Lucky Numbers 题目大意: 给定数字位数,且这个数字只能由7和8组成,问有多少种组合的可能性 思路: 假设为1位,只有7和8:两位的时候,除了77,78,87,88之外还哇哦 ...
- poj_2115C Looooops(模线性方程)
题目链接:http://poj.org/problem?id=2115 C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
随机推荐
- spring-cloud - 基础环境搭建
spring-cloud中文文档:https://springcloud.cc/ spring-cloud中文导航:http://springcloud.fun/ 文章纯属用于个人学习的一个归纳,哪里 ...
- 【spring boot】注解@SpringBootApplication 相当于 @Configuration、@EnableAutoConfiguration 、 @ComponentScan 三个的作用
注解@SpringBootApplication 相当于 @Configuration.@EnableAutoConfiguration . @ComponentScan 三个的作用 代码示例:Git ...
- SilverLight:基础控件使用(1)
ylbtech-SilverLight-Basic-Control:基础控件使用(1) 本文详解控件有: Label, TextBox, PasswordBox, Image, Button , Ra ...
- 使用TypeDescriptor给类动态添加Attribute【转】
源文 : http://www.cnblogs.com/bicker/p/3326763.html 给类动态添加Attribute一直是我想要解决的问题,从msdn里找了很久,到Stack Overf ...
- ccs 中的定位
一.相对定位 position:relative; 作用: 相对定位 一般加给定位元素父级 特点: (1)不脱离文档流: (2)不改变元素类型: (3)参照物是元素本身: 二.绝对定位 posi ...
- H2数据库集群
H2数据库集群 1. H2数据库简单介绍 1.1 H2数据库优势 经常使用的开源数据库:H2,Derby,HSQLDB.MySQL,PostgreSQL. 当中H2,HSQLDB相似,十分适合作为嵌入 ...
- logstash+es+kibana+redis搭建
环境信息: CentOS 6.5 redis 3.0.4 logstash elasticsearch kibana 服务端ip:192.168.0.65 客户端ip:192.168.0.66 关系结 ...
- [笔记] 精通正则表达式/Mastering Regular Expressions
/ 匹配<emphasis>这个tag标注的IP地址的RE:‘<emphasis>([0-9]+(\.[0-9]+){3})</emphasis>' / 锚定--a ...
- What is the relationship between Xcode, Swift and Cocoa?
Xcode is an IDE for developing Swift or Objective-C applications, which can use the Cocoa API (which ...
- 【浅墨Unity3D Shader编程】之三 光之城堡篇:子着色器、通道与标签的写法 & 纹理混合
本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://hpw123.net/a/C__/kongzhitaichengxu/2014/1117/120.html 作者:毛星云 ...