洛谷1072 hankson的趣味题 数论乱搞 非标准解法
来源:互联网 发布:范磊主讲c语言视频教 编辑:程序博客网 时间:2024/05/22 02:30
传送门
题解:
- 看起来这是一道数学题,,,那么,,先打个暴力放松一下心情。。
#include <cstdio>#include <cstring>#include <algorithm>int n;long long anstot;long long a0, a1, b0, b1;long long gcd(long long x, long long y) { if (y == 0) return (x); return (gcd(y, x % y));}int main () { freopen("son.in", "r", stdin); freopen("son.out", "w", stdout); scanf("%d", &n); while (n--) { anstot = 0; scanf("%lld %lld %lld %lld", &a0, &a1, &b0, &b1); for (long long x = a1; x <= b1; x++) { if (gcd(x, a0) != a1) continue; long long tx = gcd(x, b0); if (x * b0 / tx != b1) continue; anstot++; } printf("%lld\n", anstot); } return 0;}
- 找一张纸简单的推导一下看看有没有啥有用的发现:
∵lcm(x,b0)=b1 ∴x∗b0/gcd(x,b0)=b1 ∴gcd(x,b0)=x∗b0/b1 ∴gcd(b1/b0,b1/x)=1 - 显然x是
b1 的因数,那么直接从1到(√n) 枚举x判断是否成立即可
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>int n;long long anstot;long long a0, a1, b0, b1;long long gcd(long long x, long long y) { if (y == 0) return (x); return (gcd(y, x % y));}void check(long long x) { if (gcd(x, a0) != a1) return; long long cur = gcd(x, b0); if (x * b0 / cur != b1) return; anstot++;}int main () { freopen("son.in", "r", stdin); freopen("son.out", "w", stdout); scanf("%d", &n); while (n--) { anstot = 0; scanf("%lld %lld %lld %lld", &a0, &a1, &b0, &b1); long long c = sqrt(b1); for (long long x = 1; x <= c; x++) { if (b1 % x != 0) continue; check(x); if (x * x == b1) continue; check(b1 / x); } printf("%lld\n", anstot); } return 0;}
90分了,,,再想想,把long long改成int,check的时候压一下常数。。
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>int n;long long anstot;int a0, a1, b0, b1;int gcd(int x, int y) { if (y == 0) return (x); return (gcd(y, x % y));}void check(int x) { if (x % a1) return; if (gcd(x/a1, a0/a1) == 1 && gcd(b1/b0, b1/x) == 1) anstot++;}int main () { freopen("son.in", "r", stdin); freopen("son.out", "w", stdout); scanf("%d", &n); while (n--) { anstot = 0; scanf("%d %d %d %d", &a0, &a1, &b0, &b1); long long c = sqrt(b1); for (int x = 1; x <= c; x++) { if (b1 % x != 0) continue; check(x); if (x * x == b1) continue; check(b1 / x); } printf("%lld\n", anstot); } return 0;}
好像A了,,,,=。=
1 0
- 洛谷1072 hankson的趣味题 数论乱搞 非标准解法
- NOIP2009 Hankson的趣味题 [数论]
- 【数论】【NOIP2009】Hankson的趣味题
- 【NOIP2009】洛谷1072 Hankson的趣味题
- 洛谷1072 Hankson 的趣味题
- Hankson的趣味题
- Hankson的趣味题
- hankson的趣味题
- 【noip2009提高组】 Hankson 的趣味题 欧几里得(数论)
- wikioi 1172 Hankson 的趣味题(数论)
- 【NOIP2009】【数论】T2 Hankson的趣味题 题解
- 蓝桥杯 算法训练 Hankson的趣味题(数论)
- 洛谷 P1072 Hankson 的趣味题
- 数学-洛谷P1072 Hankson 的趣味题
- 洛谷 P1072 Hankson 的趣味题
- NOIP2009 Hankson的趣味题
- Codevs1172 Hankson的趣味题
- NOIP2009 Hankson 的趣味题
- OnTriggerEnter和OnCollisionEnter的区别和用法
- php登录注册页面(新手)
- Linux学习笔记基础01
- QT开发:QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connect...
- TZOJ2016 Day1T1 tree【天津省选】
- 洛谷1072 hankson的趣味题 数论乱搞 非标准解法
- CentOS 7 安装MariaDB
- Django URL传递参数的方法总结(转)
- 简易折半查找
- windows剪切板api
- 《你的灯亮着吗》第一遍读后感
- vs2013 删除空行
- Linux编程Shell基础管道符
- 4. 8个月从CS菜鸟到拿到Google Offer的经历+内推