直角三角形,三条边的长度都是整数。给出周长N,求符合条件的三角形数量。

 例如:N = 120,共有3种不同的满足条件的直角3角行。分别是:{20,48,52}, {24,45,51}, {30,40,50}。
 输入:
第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 50000)
第2 - T + 1行:每行1个数N(12 <= N <= 10^7)。
输出:
输出共T行,每行1个对应的数量。

解法1:所有本原直角三角形(即边为a、b、c,gcd(a,b,c)==1)可表示为两奇数s和t,s>t,gcd(s,t)==1, 边为st,(s*s-t*t)/2,(s*s+t*t)/2
反之,任意符合条件的s,t也可通过这样组成本原直角三角形
所以周长C= s*(s+t) ,对于C<=1e7,发现是s是sqrt级别的,可以s^2暴力求gcd即O(n*gcd复杂度),求出所有在数据范围内的本原直角三角形的周长
那么对于一个周长n,不同的直角三角形必定对应着不同的本原直角三角形,所以本原直角三角形周长必定是n的因子,枚举n的因子,然后统计答案
#include<bits/stdc++.h>
#define mst(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x&(-x))
#define lson (rt<<1)
#define rson (rt<<1|1)
using namespace std;
typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef pair<ll,int>pli;
typedef pair<int,int> pii;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 1e7+;
const int mod = 1e9+;
int mi[maxn],use[maxn],ggg[maxn];
void init(int c=maxn-){
int cc=;
for(int i=; i<=c; ++i) {
if(!mi[i])use[++cc]=i,mi[i]=i;
int to=c/i;
for(int j=; j<=cc and use[j]<=to; ++j) {
mi[use[j]*i]=use[j];
if(i%use[j]==) break;
}
}
int u=sqrt(c);
for(int i=;i<=u;i+=){
int to=min(i-,(c-i*i)/i);
for(int j=;j<=to;j+=){
if(__gcd(i,j)==){
++ggg[i*(i+j)];
}
}
}
}
int f[],cnt[],cc;
int d[],gg;
void dfs(int cur,int now){
if(cur>cc){
d[++gg]=now;
}else{
for(int i=;i<=cnt[cur];++i){
dfs(cur+,now);
now*=f[cur];
}
}
}
int solve(int val,int ti){
int res=;
int tmp=val;
cc=;
while(tmp>){
int v=mi[tmp];
f[++cc]=v;
cnt[cc]=;
while(tmp%v==){
++cnt[cc];
tmp/=v;
}
}
gg=;
dfs(,);
for(int i=;i<=gg;++i)
res+=ggg[d[i]];
return res;
}
int main() {
#ifdef local
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
init();
int T;cin>>T;
for(int i=;i<=T;++i){
int val;cin>>val;
if(val&)cout<<<<"\n";
else cout<<solve(val,i)<<"\n"; }
return ;
}
解法2:化式子
  a*a+b*b = c*c
  a+b+c = n
-> a+b+sqrt(a*a+b*b) = n
-> sqrt(a*a+b*b) = n-a-b
-> 两边平方并化简
-> n^2 - 2an = 2bn-2ab
-> b = (n^2-2an)/(2n-2a)
令f = n-a
则 b = n(2n-2a -n)/(2f) = n(2f - n)/(2f) = n- n^2/(2f)
则有2f | n^2
再看适用范围,有
0 < a < n/3
a < b (不会有等于,abc都是整数,a=b,c=sqrt(2)a × )
得到 n > f > 2n/3 -> 2n > 2f > 4n/3
n-f < n-n^2/(2f) -> 2f^2 > n^2 -> 2f > sqrt(2)n > 4n/3
所以 sqrt(2)n < 2t < 2n
当2t确定,a也确定了
所以在n^2的因子中找符合条件的数
#include<bits/stdc++.h>
#define mst(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x&(-x))
#define lson (rt<<1)
#define rson (rt<<1|1)
using namespace std;
typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef pair<ll,int>pli;
typedef pair<int,int> pii;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 1e7+;
const int mod = 1e9+;
int mi[maxn],use[maxn],ggg[maxn];
inline int gcd(int a,int b){
return b?gcd(b,a%b):a;
}
void init(int c=maxn-){
int cc=;
for(int i=; i<=c; ++i) {
if(!mi[i])use[++cc]=i,mi[i]=i;
int to=c/i;
for(int j=; j<=cc and use[j]<=to; ++j) {
mi[use[j]*i]=use[j];
if(i%use[j]==) break;
}
}
// int u=sqrt(c);
// for(int i=3;i<=u;i+=2){
// int to=min(i-1,(c-i*i)/i);
// for(int j=1;j<=to;j+=2){
// if(gcd(i,j)==1){
// ++ggg[i*(i+j)];
// }
// }
// }
}
int f[],cnt[],cc;
ll d[],gg;
void dfs(int cur,ll now){
if(cur>cc){
d[++gg]=now;
}else{
for(int i=;i<=cnt[cur];++i){
dfs(cur+,now);
now*=f[cur];
}
}
}
int solve(int val,int ti){
int res=;
int tmp=val;
cc=;
while(tmp>){
int v=mi[tmp];
f[++cc]=v;
cnt[cc]=;
while(tmp%v==){
++cnt[cc];
tmp/=v;
}
cnt[cc]<<=;
}
gg=;
dfs(,);
int l=sqrt()*val,r=val<<;
for(int i=;i<=gg;++i){
ll v=d[i];
if(v&)continue;
if(v>l and v<r)++res;
}
return res;
}
int main() {
#ifdef local
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
init();
int T;cin>>T;
for(int i=;i<=T;++i){
int val;cin>>val;
if(val&)cout<<<<"\n";
else cout<<solve(val,i)<<"\n"; }
return ;
}

发现两种解法的运行速度差不多。。。


51nod 1165 整边直角三角形的数量(两种解法)的更多相关文章

  1. 51nod 1165 整边直角三角形的数量

    1165 整边直角三角形的数量 直角三角形,三条边的长度都是整数.给出周长N,求符合条件的三角形数量. 例如:N = 120,共有3种不同的满足条件的直角3角行.分别是:{20,48,52}, {24 ...

  2. Java描述表达式求值的两种解法:双栈结构和二叉树

    Java描述表达式求值的两种解法:双栈结构和二叉树 原题大意:表达式求值 求一个非负整数四则混合运算且含嵌套括号表达式的值.如: # 输入: 1+2*(6/2)-4 # 输出: 3.0 数据保证: 保 ...

  3. Letter Combinations of a Phone Number:深度优先和广度优先两种解法

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  4. leetcode-91-解码方法(动态规划和递归两种解法)

    题目描述: 一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数 ...

  5. POJ 1157 LITTLE SHOP OF FLOWERS (超级经典dp,两种解法)

    You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flo ...

  6. Sort List[leetcode] 由归并排序的递归和循环,到本题的两种解法

    归并排序能够有两种思路----top-down 和 bottom-up top-down: 递归实现,将数组分成两半.分别处理.再合并. 伪代码例如以下: split ( A[], l, r) { i ...

  7. POJ 1182食物链(分集合以及加权两种解法) 种类并查集的经典

    题目链接:http://icpc.njust.edu.cn/Problem/Pku/1182/ 题意:给出动物之间的关系,有几种询问方式,问是真话还是假话. 定义三种偏移关系: x->y 偏移量 ...

  8. 【Java面试真题】剑指Offer53.2——0~n-1中缺失的数字(异或、二分两种解法)

    [Java实现]剑指Offer53.2--0~n-1中缺失的数字:面试真题,两种思路分享 前面有另一道面试题[Java实现]剑指offer53.1--在排序数组中查找数字(LeetCode34:在排序 ...

  9. [LeetCode] Validate Binary Search Tree (两种解法)

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

随机推荐

  1. 日历控件datetimepicker(IE11)

    1.安装 smalot.bootstrap-datetimepicker 2.引用 bootstrap.css bootstrap-datetimepicker.min.css jquery-1.10 ...

  2. ubuntu14.04 x86编译upx 3.92 及so加固

    的参考文章: http://www.cnblogs.com/fishou/p/4202061.html 1.download upx和所依赖的组件 upx3.:https://www.pysol.or ...

  3. 03 Go语言特性

    一.基本注意事项 1.转义字符 \t 一个制表符,代表一次tab \n 换行符 \\ 转义代表 \ \" 转义代表 " \r 一个回车,从当前行的最前面开始输出,会覆盖以前的内容, ...

  4. 鼠标右键点击弹出菜单(jQuery)

    禁用浏览器默认事件,此处是兼容写法 $(document).contextmenu(function (e) { var event = e || window.event; if (event.pr ...

  5. 快递100API

    url:http://www.kuaidi100.com/query 拼接参数: 参数名称 参数取值 参数类型 type 快递码,请参考快递100码 String postid 快递单号 String ...

  6. Android-分享多图到微信好友

    /** * 微信分享(多图片) */ private static void shareWeChatByImgList(String kDescription, List<File> im ...

  7. ubuntu 共享WIFI并分享主机的代理服务

    背景是这样的: 公司内的主机访问外网需要通过一个HTTP代理服务器,主机ubuntu共享wifi给手机使用的时候需要在手机上配置一个代理才能访问互联网. 我觉得这样比较麻烦,所以想在主机上直接把共享w ...

  8. docker容器里面安装php的redis扩展

      docker exec -i -t php /bin/bash 进入php容器内执行:pecl install -o -f redis  修改php.ini,添加:extension=redis. ...

  9. webpack整合 .vue 文件,集成 vue-loader

    webpack集成vue-loader 创建一个文件夹 test_webpack_vue 在 test_webpack_vue 下新建目录 src 在 src 目录下 新建文件 index.html ...

  10. MySQL介绍及安装环境配置

    MySQL介绍及安装环境配置 MySQL是一种关系数据库管理系统,是一种开源软件.由瑞典MySQL AB公司开发,2008年1月16号被Sun公司收购.2009年,SUN又被Oracle收购.MySQ ...