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 ...
随机推荐
- DNS重绑定DNS Rebinding攻击
DNS重绑定DNS Rebinding攻击 在网页浏览过程中,用户在地址栏中输入包含域名的网址.浏览器通过DNS服务器将域名解析为IP地址,然后向对应的IP地址请求资源,最后展现给用户.而对于域名所有 ...
- javaScript 时间转换,将后台返回的时间为一串数字转成正常格式
js完整代码: function transferTime(cTime){ var jsonDate = new Date(parseInt(cTime)); Date.prototype.forma ...
- java判断日期与星期
原文:http://www.open-open.com/code/view/1440592372888 import java.text.SimpleDateFormat; import java.u ...
- iOS -- MBProgressHUB
高级: http://www.jianshu.com/p/485b8d75ccd4 //只有小菊花 - (void)indeterminateExample { // Show the HUD on ...
- UNIX&Linux发展图谱
来自为知笔记(Wiz)
- 1、CRM2011编程实战——清空指定页签以下的全部选项,并对页签以下的指定控件进行操作
需求:当页面载入时,"呼叫编号"保持不变,"任务号"自己主动更新."接报时间"和"发生日期"自己主动设置为当天日期和时间 ...
- AngularJS的表单验证提交示例
代码下载:https://files.cnblogs.com/files/xiandedanteng/angularjsFormSubmit.rar 前台代码: <%@ page content ...
- java性能监控工具jcmd-windows
jcmd Sends diagnostic command requests to a running Java Virtual Machine (JVM). Synopsis jcmd [-l|-h ...
- LoadRunner多负载产生器
Executive Summary : The following explains why it is necessary to have about 6 load generators when ...
- hql 多对多查询
这种查询,hibernate 建议用 From Dealer s inner join fetch s.carSerieses cs 实现,注意这种实现只支持b.c,不支持b.cs. 如果要用b.c ...