整理下《算法笔记》,方便查看。

一、最大公约数&最小公倍数

欧几里得定理:设a,b均为正整数,那么gcd(a,b)=gcd(b,a%b)。

若,定理就先交换a和b。

注意:0和任意正整数a的gcd是a。

//最大公约数
int gcd(int a,int b)
{
return !b ? a : gcd(b,a % b);
}

设最大公约数为res,最小公倍数lcm即为。

二、分数

PAT甲1088是比较经典的分数处理问题,求2个分数的和、差、积、商,输出最简形式。

表示、化简、运算、输出,代码阐释得很清楚。

#include <cstdio>
#include <algorithm> using namespace std; typedef long long ll; ll gcd(ll a,ll b)
{
return !b ? a : gcd(b,a % b);
} struct Fraction{
ll nume,deno;
}; Fraction reduction(Fraction a)
{
if(a.deno < 0)
{
a.deno = -a.deno;
a.nume = -a.nume;
}
if(a.nume == 0)
{
a.deno = 1;
}
else
{
int d = gcd(abs(a.nume),abs(a.deno));
a.nume /= d;
a.deno /= d;
}
return a;
} Fraction add(Fraction a,Fraction b)
{
Fraction res;
res.deno = a.deno * b.deno;
res.nume = a.deno * b.nume + a.nume * b.deno;
return reduction(res);
} Fraction sub(Fraction a,Fraction b)
{
Fraction res;
res.deno = a.deno * b.deno;
res.nume = a.nume * b.deno - a.deno * b.nume;
return reduction(res);
} Fraction times(Fraction a,Fraction b)
{
Fraction res;
res.deno = a.deno * b.deno;
res.nume = a.nume * b.nume;
return reduction(res);
} Fraction divide(Fraction a,Fraction b)
{
Fraction res;
res.deno = a.deno * b.nume;
res.nume = a.nume * b.deno;
return reduction(res);
} void showFrac(Fraction a)
{
a = reduction(a);
if(a.nume < 0)
{
printf("(");
}
if(a.deno == 1)
{
printf("%lld",a.nume);
}
else if(abs(a.nume) > abs(a.deno))
{
printf("%lld %lld/%lld",a.nume / a.deno,abs(a.nume) % a.deno,a.deno);
}
else
{
printf("%lld/%lld",a.nume,a.deno);
}
if(a.nume < 0)
{
printf(")");
}
} int main()
{
Fraction a,b;
scanf("%lld/%lld%lld/%lld",&a.nume,&a.deno,&b.nume,&b.deno); showFrac(a);
printf(" + ");
showFrac(b);
printf(" = ");
showFrac(add(a,b));
printf("\n"); showFrac(a);
printf(" - ");
showFrac(b);
printf(" = ");
showFrac(sub(a,b));
printf("\n"); showFrac(a);
printf(" * ");
showFrac(b);
printf(" = ");
showFrac(times(a,b));
printf("\n"); showFrac(a);
printf(" / ");
showFrac(b);
printf(" = ");
if(b.nume == 0)
{
printf("Inf\n");
}
else
{
showFrac(divide(a,b));
printf("\n");
} return 0;
}

三、素数

1、判断素数

bool isPrime(int a)
{
if(a <= 1) //1不是素数,也不是合数
return false;
int tmp = (int)sqrt(1.0 * a);
for(int i = 2;i <= tmp;i++)
{
if(a % i == 0)
return false;
}
return true;
}

2、打素数表

第一种方法是枚举判断。

const int maxn = 10010;
int prime[maxn],num = 0; void Prime_table()
{
for(int i = 2;i < maxn;i++)
{
if(isPrime(i))
{
prime[num++] = i;
}
}
}

第二种是Eratosthenes筛法,复杂度比枚举更优,代码更短。

const int maxn = 10010;
int prime[maxn],num = 0;
bool p[maxn] = {false}; //i为素数,p[i]为false void Prime_table()
{
for(int i = 2;i < maxn;i++)
{
if(p[i] == false)
{
prime[num++] = i;
for(int j = i + i;j < maxn;j += i)
{
p[j] = true;
}
}
}
}

3、分解质因子

注意:1要特判。

//存储
struct factor{
int x,cnt; //x为质因子,cnt为该质因子个数
}fac[20];
int num = 0;  //记录不同因子个数
//枚举小于等于sqrt(n)内的所有质因子,判断哪个是n的因子
for(int i = 0;prime[i] <= sqrt(n);i++)
{
if(n % prime[i] == 0)
{
fac[num].x = prime[i];
fac[num].cnt = 0;
while(n % prime[i] == 0)
{
fac[num].cnt++;
n /= primep[i];
}
num++;
}
} //如果n仍然大于1,说明n有一个大于sqrt(n)的质因子
if(n != 1)
{
fac[num].x = n;
fac[num++].cnt = 1;
}

Simple Math Problems的更多相关文章

  1. hdu 1757 A Simple Math Problem (乘法矩阵)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  2. HDU1757 A Simple Math Problem 矩阵快速幂

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  3. hdu------(1757)A Simple Math Problem(简单矩阵快速幂)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  4. FZYZ-2071 A Simple Math Problem IX

    P2071 -- A Simple Math Problem IX 时间限制:1000MS      内存限制:262144KB 状态:Accepted      标签:    数学问题-博弈论    ...

  5. A Simple Math Problem(矩阵快速幂)(寒假闭关第一题,有点曲折啊)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...

  6. HDU 1757 A Simple Math Problem (矩阵快速幂)

    题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...

  7. HDU 1757 A Simple Math Problem(矩阵)

    A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...

  8. HDU 1757 A Simple Math Problem (矩阵乘法)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  9. hdu 5974 A Simple Math Problem

    A Simple Math Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

随机推荐

  1. MyBatis(一):第一个MyBatis程序

    本文是按照狂神说的教学视频学习的笔记,强力推荐,教学深入浅出1便就懂!b站搜索狂神说即可 https://space.bilibili.com/95256449?spm_id_from=333.788 ...

  2. mpvue微信小程序怎么写轮播图,和官方微信代码的差别

    目前用mpvue很多第三方的ui库是引入不了的,因为它不支持含有dom操作. 那我们要做轮播图的话一个是手写另外一个就是用小程序的swiper组件了: 官方代码: <swiper indicat ...

  3. (js描述的)数据结构 [数组的一些补充](1)

    (js描述的)数据结构 [数组的一些补充](1) 1. js的数组: 1.优点:高度封装,对于数组的操作就是调用API 2.普通语言的数组: 1.优点:根据index来查询,修改数据效率很高 2.缺点 ...

  4. JAVA中Calendar 类的应用

    转自:https://www.imooc.com/code/2340 侵删! Date 类最主要的作用就是获得当前时间,同时这个类里面也具有设置时间以及一些其他的功能,但是由于本身设计的问题,这些方法 ...

  5. python中汉字转数字

    #!/usr/bin/env python # -*- coding: utf-8 -*- common_used_numerals_tmp ={'零':0, '一':1, '二':2, '三':3, ...

  6. mysql导出

    --all-databases , -A 导出全部数据库. mysqldump -uroot -p --all-databases --all-tablespaces , -Y 导出全部表空间. my ...

  7. 《深入理解 Java 虚拟机》读书笔记:垃圾收集器与内存分配策略

    正文 垃圾收集器关注的是 Java 堆和方法区,因为这部分内存的分配和回收是动态的.只有在程序处于运行期间时才能知道会创建哪些对象,也才能知道需要多少内存. 虚拟机栈和本地方法栈则不需要过多考虑回收的 ...

  8. Python队列的三种队列方法

    今天讲一下队列,用到一个python自带的库,queue 队列的三种方法有: 1.FIFO先入先出队列(Queue) 2.LIFO后入先出队列(LifoQueue) 3.优先级队列(PriorityQ ...

  9. Vm安装虚拟机并使用net模式连接外网

    Vm安装虚拟机并使用net模式连接外网 最近想搭建一个maven私服和阿波罗配置中心一切准备就绪时 发现本地vm虚拟机无法连接外网,嗯 ~ ~ ,一句cnm不由从嘴里崩了出来.没办法,只能配置一下了接 ...

  10. flutter和react native如何选择

    [关于性能]跨平台开发第一个考虑的就是性能问题RN的效率由于是将View编译成了原生View,所以效率上要比基于Cordova的HTML5高很多,但是它也有效率问题,RN的渲染机制是基于前端框架的考虑 ...