数学1

· · 算法·理论

看到标题时%99.99999999999999999的人(剩下那%0.000000000000000001都是c++大佬)都觉得是学校学的

而我们今天说一说c++中的数学

写题之前我们先预习了解一下用到的数学知识

1.两点之间的距离公式:

\sqrt{(x1-x2)^2+(y1-y2)^2}

2.海伦公式 若三角形的边长为a,b,c,则三角形的面积是

\sqrt{s(s-a)(s-b)(s-c)}

s=\frac{1}{2}(a+b+c)

3.小数保留整数

1.int(最后答案+0.5) 2.round返回浮点数,(int)(round(最后答案))

直接cout,会使用科学计数法

整数部分超过6位会采用科学计数法

4.异或

偶数次相同的数字异或,答案是0

a ^ a=0

奇数次相同的数字异或,答案是数字本身

a ^ a ^ a=0 ^a=a

异或规则:相同的位都是1,或都是0的时候,答案为0

0异或任何数都等于原数字

5.幂次方

幂的性质\begin{cases} &{(a×b)}^c= a^c×b^c\\ &{(a^b)}^c= a^{b×c}\\ &a^b×a^c= a^{b+c}\\ &(a^b)^c= (a^c)^b\\ &\frac{a^b}{a^c} =a^{b-c} \end{cases} {a底数}^{b指数}

6.唯一分解定理

给定一个大于1的正整数n,一定可以为已拆分为若干个质因子相乘

n={p1}^{k1}×{p2}^{k2}×{p3}^{k3}×......{pm}^{km}

如果d|n(整除符号:代表n%d==0)

n能够唯一分解

d能够唯一分解

①n的质因子,一定包含d的质因子种类

②n的对应的质因子幂次大于等于d的质因子幂次

历经千辛万苦(你们不苦,我命苦!!),终于可以写题了

第1题 ------------ B3638 T1 三角形面积

直接用题目给的公式(1.2.3条公式)做就行了

#include<bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const int TWO=1005;
const int ONE=1000005;
const int im=INT_MAX;
//int a[1000005],d[1000005];
//int x[1000005],y[1000005];
double ss(int x,int y,int x2,int y2){
    return sqrt((x-x2)*(x-x2)+(y-y2)*(y-y2));
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    double a,b,c,d,e,f;
    cin>>a>>b>>c>>d>>e>>f;
    double s1=ss(a,b,c,d);
    double s2=ss(a,b,e,f);
    double s3=ss(c,d,e,f);
    double s=(s1+s2+s3)/2;
    double y=sqrt(s*(s-s1)*(s-s2)*(s-s3));
    cout<<(int)(y+0.5);
    return 0;
}

第2题 ------------ P1469 找筷子

根据第4条的性质做

#include<bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const int TWO=1005;
const int ONE=1000005;
const int im=INT_MAX;
int ans=0;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        ans^=x;
    }
    cout<<ans;
    return 0;
}

第3题 ------------ P5514 [MtOI2019] 永夜的报应

根据第4条的性质做

#include<bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const int TWO=1005;
const int ONE=1000005;
const int im=INT_MAX;
int ans=0;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        ans^=x;
    }
    cout<<ans;
    return 0;
}

第4题 ------------ P8448 [LSOT-1] 暴龙的土豆

题目说z 是奇素数,又要操作次数最多,那么z一直取3就行了

#include<bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const int TWO=1005;
const int ONE=1000005;
const int im=INT_MAX;
void work(){
    int n,ans=0;
    cin>>n;
    for(int i=2;i*i*i<=n;i++){
        int st=i*i*i;
        while(n%st==0){
            n/=st;
            ans++;
        }
    }
    cout<<ans<<"\n";
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        work();
    }
    return 0;
}

第5题 ------------ B3715 分解质因子 2

一个一个试就行了

#include<bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const int TWO=1005;
const int ONE=1000005;
const int im=INT_MAX;
void work(){
    int n;
    cin>>n;
    if(n<=1){
        return ;
    }
    for(int i=2;i*i<=n;i++){
        if(n%i==0){
            while(n%i==0){
                n/=i;
                cout<<i<<" ";
            }
        }
    }
    if(n>=1){
        cout<<n;
    }
    cout<<"\n";
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        work();
    }
    return 0;
}

第6题 ------------ P10495 阶乘分解

由于会爆long long,所有把1~n都分解后输出

#include<bits/stdc++.h>
//#define int long long
#define double long double
using namespace std;
const int TWO=1005;
const int ONE=1000005;
const int im=INT_MAX;
map<int,int> mp;
void work(int n){
    if(n<=1){
        return ;
    }
    for(int i=2;i*i<=n;i++){
        if(n%i==0){
            while(n%i==0){
                n/=i;
                mp[i]++;
            }
        }
    }
    if(n>1){
        mp[n]++;
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        work(i);
    }
    for(int i=1;i<=n;i++){
        if(mp[i]>=1){
            cout<<i<<" "<<mp[i]<<"\n";
        }
    }
    return 0;
}

手费了!!

作业

P2043 质因子分解

P2626 斐波那契数列(升级版)

Coins