实验课.简单数学(基本数学题)

基本数学题

很早就想整理一下,但一直苦苦哀叹,(其实就是我懒)

求解针对数据处理问题:

此类简单数学题暂时分为四个基本点:

1.求n^n最右侧的数字:

当时这道题一直炸,其实思路很好懂,就是细节的处理。
初始思路:不断对十求余;

在这里插入图片描述
但是仔细思考一下,不断求余的话,量太大了,开ll也会炸掉;
题目描述:
Description:
Given a positive integer N, you should output the most right digit of N^N.
Input:
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
Output:
For each test case, you should output the rightmost digit of N^N.

以下为AC代码:
(1):
using namespace std;
typedef long long ll;
const int maxn=1e5;
const ll _InF = -8e18;
const int n = 1e5 + 10;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll n,ans=1,cnt;
        cin>>n;
        cnt=n;
        n%=10;
        while(cnt)
        {
            if(cnt%2)
                ans=(ans*n)%10;
            cnt/=2;
            n=(n*n)%10;
        }
        printf("%d\n",ans%10);
    }
    return 0;
}

(2)
其实还是有一种思路的,就是找规律,很简单的思路(老师讲的,但我对着一点不敏感)
老师之神图:
在这里插入图片描述
那,主要代码也是转载的:
在这里插入图片描述

2:求最左侧数字:

题目描述(题面和右侧差不多):
Description
Given a positive integer N, you should output the leftmost digit of N^N.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
Output
For each test case, you should output the leftmost digit of N^N.

这道题呢是求最左侧数字,刚开始觉得可以不断除100后计算,留下最左面的数,进行计算,后来发现走不太通,或者是我走不通(想来可以继续试试)
思路:可以说是一个固定的公式在这里插入图片描述

using namespace std;
typedef long long ll;
const int maxn=1e5;
const ll _InF = -8e18;
const int n = 1e5 + 10;

int main()
{
   int t,ans;
   double x,m;
   cin>>t;
   while(t--)
   {
       ll n;
       cin>>n;
       x=n*1.0*log10((double)n);
       m=floor(x);
       x-=m;
       ans=pow(10.0,x);
       printf("%d\n",ans);

   }
   return 0;
}

3.阶乘后最后一个非0位(不为0的数字):

Description
N的阶乘写作N!表示小于等于N的所有正整数的乘积。阶乘会很快的变大,如13!就必须用32位整数类型来存储,70!即使用浮点数也存不下了。你的任务是找到阶乘最后面的非零位。举个例子,5!=12345=120所以5!的最后面的非零位是2,7!=1234567=5040,所以最后面的非零位是4。
Input
共一行,一个整数不大于4,220的整数N。
Output
共一行,输出N!最后面的非零位。
思路:这道题完全是摸着石头过河,最后老师的思路如下:
还是计算5的个数。
去掉与5一样多的2
再对10求模

using namespace std;
typedef long long ll;
const int maxn=1e5;
const ll _INF = -8e18;
const int N = 1e5 + 10;

int main()
{
    int n;
    ll i;
    cin>>n;
    ll ans = 1;
    for(i = 1; i <= n; i ++)
    {
        ans *= i;
        while(ans%10 == 0)
            ans /= 10;
        ans = ans%1000;
    }
    printf("%lld",ans%10);
    return 0;
}

4.阶乘后尾数0的个数

Description
The most important part of a GSM network is so called Base Transceiver Station (BTS). These transceivers form the areas called cells (this term gave the name to the cellular phone) and every phone connects to the BTS with the strongest signal (in a little simplified view). Of course, BTSes need some attention and technicians need to check their function periodically.
ACM technicians faced a very interesting problem recently. Given a set of BTSes to visit, they needed to find the shortest path to visit all of the given points and return back to the central company building. Programmers have spent several months studying this problem but with no results. They were unable to find the solution fast enough. After a long time, one of the programmers found this problem in a conference article. Unfortunately, he found that the problem is so called “Travelling Salesman Problem” and it is very hard to solve. If we have N BTSes to be visited, we can visit them in any order, giving us N! possibilities to examine. The function expressing that number is called factorial and can be computed as a product 1.2.3.4…N. The number is very high even for a relatively small N.
The programmers understood they had no chance to solve the problem. But because they have already received the research grant from the government, they needed to continue with their studies and produce at least some results. So they started to study behaviour of the factorial function.
For example, they defined the function Z. For any positive integer N, Z(N) is the number of zeros at the end of the decimal form of number N!. They noticed that this function never decreases. If we have two numbers N1 < N2, then Z(N1) <= Z(N2). It is because we can never “lose” any trailing zero by multiplying by any positive number. We can only get new and new zeros. The function Z is very interesting, so we need a computer program that can determine its value efficiently.
Input
There is a single positive integer T on the first line of input. It stands for the number of numbers to follow. Then there is T lines, each containing exactly one positive integer number N, 1 <= N <= 1000000000.
Output
For every number N, output a single line containing the single non-negative integer Z(N).

思路:这道题要求的是尾数0的个数。N!是没法直接求的,所以得间接解决。尾数0来源于5和偶数的乘积,N!中偶数个数总是大于5的个数,所以只要求出N!由多少个5组成,即求N的因子中有多少5。但是存在一些无法实施的条件。
首先从头开始分析:
5!=120,其末尾所含有的“0”的个数为1;
10!= 3628800,其末尾所含有的“0”的个数为2;
20!= 2432902008176640000,其末尾所含有的“0”的个数为4。
那么1234!末尾0的个数同样可以得出:1234!=2p13p25p3*…….
0是由2和5产生的,2的个数比5的多,只要计算出5的个数即可。
1234! 的尾数 0 的个数计算如下: 代码:
1234/5 = 246
246/5 = 49
49/5 = 9
9/5 = 1
1/5 = 0
------ ---------- 305
原理:行(1)得到的是1n中因子含5的数的个数
行(2)得到的是1
n中因子含25的数的个数
.
.
.
行(n)得到的是1~~n中因子含5^n的数的个数
将这些数进行累计就得到了5因子的个数,也就是末尾0的个数

using namespace std;
typedef long long ll;
const int maxn=1e5;
const ll _InF = -8e18;
const int n = 1e5 + 10;

int main()
{
    int T;
    ll n,num;
    cin>>T;
    while(T--)
    {
        cin>>n;
        num=0;
        while(n>=5)
        {
            n/=5;
            num+=n;
        //cout<<n;
        //printf("\n");
        }

        cout<<num<<endl;
    }
    return 0;
}

求解约数问题:

link.

特殊例题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值