直角三角形,三条边的长度都是整数。给出周长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. .Net Core 3.0 内置依赖注入:举例

    原文:.Net Core 3.0 内置依赖注入:举例 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn ...

  2. C#开发微信公众平台-就这么简单(转载)(附原文链接)

    一直使用的是一百八的诺鸡鸭,没有想去接触看起来风风火火的移动互联网:但因工作需要维护一个微信公众订阅号,考虑以前有做网站的基础,就想着做个简单的微信后台管理:看了官方的开发文档,比狗哥地图的短许多,又 ...

  3. ubuntu 编译zbar 静态库

    wget http://downloads.sourceforge.net/project/zbar/zbar/0.10/zbar-0.10.tar.gz tar -zvxf zbar-0.10.ta ...

  4. css 单位

    CSS 有几个不同的单位用于表示长度. 一些设置 CSS 长度的属性有 width, margin, padding, font-size, border-width, 等. 长度有一个数字和单位组成 ...

  5. element-ui el-table表格排序sortable参数解析

    表格组件的排序功能,点击排序表头可以进行升序和降序进行排序 页面代码,基本上排序的参数都使用了 <el-table :data="tableData" style=" ...

  6. postman安装时提示打不开

    安装postman6.6.1时,提示打不开,如下图: 解决办法: 1.找到以下两个路径直接删除文件,注安装路径不同有可能不同 C:\Users\Administrator\AppData\Roamin ...

  7. java_day10_多线程

    第十章:线程 1.进程和线程的概述 1)进程和线程定义 进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和调度的一个独立单位. 线程是进程的一个实体,是CPU调度和 ...

  8. mysql 设置服务器的MySQL允许远程访问/外网访问

    设置服务器的MySQL允许远程访问/外网访问 https://blog.csdn.net/weixin_34232363/article/details/85889037

  9. Kivy 简单尝试

    Kivy 简明教程 首先,有两个网址你不得不看: Kivy: Cross-platform Python Framework for NUI在官网逛逛,顺便看看最下面的代码,try to run it ...

  10. 微信小程序开发(三)点击事件

    接着上篇博客继续. 如下修改: // index.wxml <view>Hello World!</view> <button bindtap="but&quo ...