HackerRank leonardo-and-lucky-numbers —— 模线性方程的通解
来源:互联网 发布:gephi输入数据 编辑:程序博客网 时间:2024/05/16 05:18
题目链接: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 LOCALusing namespace std;typedef long long LL;const int INF = 2e9;const LL LNF = 9e18;const int mod = 1e9+7;const int maxn = 200000+10;int main(){ LL q, n; scanf("%lld",&q); while(q--) { LL x, y, k, n; int B = 0; scanf("%lld",&n); for(k = n/4-1; k<=(2*n)/7+1; k++) //除法可能除不尽,所以要左右各扩一个单位 { x = -1LL*n+1LL*4*k; y = 1LL*2*n-1LL*7*k; if(x<0 ||y<0) continue; if(7*x+4*y==n) { B = 1; break; } } if(B) puts("Yes"); else puts("No"); }}
阅读全文
0 0
- HackerRank leonardo-and-lucky-numbers —— 模线性方程的通解
- hackerrank Lucky Numbers(扩展gcd/规律)
- A. Roma and Lucky Numbers
- 青蛙的约会——模线性方程
- Lucky Numbers
- Lucky Numbers
- Lucky Numbers
- Codeforces Round #160 (Div. 2)---A. Roma and Lucky Numbers
- Codeforces 288E Polo the Penguin and Lucky Numbers
- Hackerrank Fibonacci Numbers Tree
- hackerrank w29 Megaprime Numbers
- 小小的模线性方程(组)
- POJ3393——Lucky and Good Months by Gregorian Calendar
- 漫步线性代数二——线性方程的几何形状
- 过桥问题的通解
- 【*】POJ-3899(数论很难)(Lucky Numbers——统计)
- hdu 5676 ztr loves lucky numbers(BC——暴力打表+二分查找)
- 【打CF,学算法——二星级】CodeForces 96B Lucky Numbers (构造/dfs)
- What Is Your Grade? HDU
- PHP 正则表达式抓取网页内容。
- 文章标题
- shiro
- 数据结构——栈
- HackerRank leonardo-and-lucky-numbers —— 模线性方程的通解
- ajax+jason=http 406
- 【C#】真分页功能的实现
- 暑期集训—day3—hdu 4506, 2855, 1097-快速幂与矩阵快速幂
- 7月22日考试总结
- 安卓手机分区
- spark架构解析
- 虚函数实现说明
- 【代码片-1】 Servlet总结