zoukankan      html  css  js  c++  java
  • 简单数学专题+总结

    A - 质数筛

    题目:

    Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular coach for his success. He needs some bamboos for his students, so he asked his assistant Bi-Shoe to go to the market and buy them. Plenty of Bamboos of all possible integer lengths (yes!) are available in the market. According to Xzhila tradition,

    Score of a bamboo = Φ (bamboo's length)

    (Xzhilans are really fond of number theory). For your information, Φ (n) = numbers less than n which are relatively prime (having no common divisor other than 1) to n. So, score of a bamboo of length 9 is 6 as 1, 2, 4, 5, 7, 8 are relatively prime to 9.

    The assistant Bi-shoe has to buy one bamboo for each student. As a twist, each pole-vault student of Phi-shoe has a lucky number. Bi-shoe wants to buy bamboos such that each of them gets a bamboo with a score greater than or equal to his/her lucky number. Bi-shoe wants to minimize the total amount of money spent for buying the bamboos. One unit of bamboo costs 1 Xukha. Help him.

                 

    Input

            

    Input starts with an integer T (**≤ 100)**, denoting the number of test cases.

    Each case starts with a line containing an integer n (1 ≤ n ≤ 10000) denoting the number of students of Phi-shoe. The next line contains n space separated integers denoting the lucky numbers for the students. Each lucky number will lie in the range [1, 106].

                 

    Output

            

    For each case, print the case number and the minimum possible money spent for buying the bamboos. See the samples for details.

                 

    Sample Input

            

    3

    5

    1 2 3 4 5

    6

    10 11 12 13 14 15

    2

    1 1

                 

    Sample Output

            

    Case 1: 22 Xukha

    Case 2: 88 Xukha

    Case 3: 4 Xukha

    题意:该题就是说,每个学生所得的bamboo的score的值必须大于或等于他的幸运数字, bamboo的score值就是小于长度x且与x互质的数的个数,并且每单位长度花费1Xukha,求买这些bamboo的最小花费。

    用欧拉函数,因为欧拉函数有一性质:素数的欧拉函数值为该素数值-1

    所以我们可以将幸运数字+1查找,若该数字是素数,-1就是score值,且满足条件,所求的就是满足条件bamboo的长度

     

    #include<cstdio>
    #include<iostream>
    using namespace std;
    const int N=1e6+10;
    int a[N]={1,1,0};
    void sushu()
    {
        for(int i=2;i<N;i++)
        {
            if(!a[i])
            {
                for(int j=i+i;j<=N;j+=i)
                   a[j]=1;
            }
        }
    }
    int main()
    {
        sushu();
        int t,n,m,x=0;
        long long sum;
        scanf("%d",&t);
        while(t--)
        {
            x++;
            sum=0;
            scanf("%d",&n);
            while(n--)
            {
                scanf("%d",&m);
                for(int i=m+1;;i++)
                {
                    if(a[i]==0)
                    {
                        sum+=i;
                        break;
                    }
                }    
            }
            printf("Case %d: %lld Xukha
    ",x,sum);
        } 
        return 0;
     } 

    B - 分解质因数

    题目:

    It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery.

    Aladdin was about to enter to a magical cave, led by the evil sorcerer who disguised himself as Aladdin's uncle, found a strange magical flying carpet at the entrance. There were some strange creatures guarding the entrance of the cave. Aladdin could run, but he knew that there was a high chance of getting caught. So, he decided to use the magical flying carpet. The carpet was rectangular shaped, but not square shaped. Aladdin took the carpet and with the help of it he passed the entrance.

    Now you are given the area of the carpet and the length of the minimum possible side of the carpet, your task is to find how many types of carpets are possible. For example, the area of the carpet 12, and the minimum possible side of the carpet is 2, then there can be two types of carpets and their sides are: {2, 6} and {3, 4}.

                 

    Input

            

    Input starts with an integer T (**≤ 4000)**, denoting the number of test cases.

    Each case starts with a line containing two integers: a b (1 ≤ b ≤ a ≤ 1012) where a denotes the area of the carpet and b denotes the minimum possible side of the carpet.

                 

    Output

            

    For each case, print the case number and the number of possible carpets.

                 

    Sample Input

            

    2

    10 2

    12 2

                 

    Sample Output

            

    Case 1: 1

    Case 2: 2

    题意:该题就是给你两个数a,b,求满足c*d==a且c>=b且d>=b的c,d二元组的对数,并且(c,d)和(d,c)属于同一种情况

    该题需要用唯一分解定理N = p1^a1p2^a2p3^a3* ... *pn^an(其中p1、p2、... pn为N的因子,a1、a2、... 、an分别为因子的指数)

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    using namespace std;
    const int maxn=1e6+10;
    typedef long long ll;
    ll p[maxn],prime[maxn];
    int k=0;
    void dabiao()
    {
        k=0;
        for(ll i=2;i<maxn;i++)
        {
            if(!p[i])
            {
                prime[k++]=i;
                for(ll j=i+i;j<=maxn;j+=i)
                   p[j]=1;
            }
        }
    }
    ll fenjie(ll a)
    {
        ll s=1;
        if(a==0)
           return 0;
        ll q=0,i=0;
        while(prime[i]<a&&i<k)
        {
            q=0;
            if(a%prime[i]==0)
            {
                while(a%prime[i]==0)
                {
                    a/=prime[i];
                    q++;
                }
            }
            s*=q+1;
            i++;
        }
        if(a>1) s*=1+1;
        return s;
    }
    int main()
    {
        ll a,b;
        int t,x=1;
        dabiao();
        cin>>t;
        while(t--)
        {
            cin>>a>>b;
            ll ans,num=0,cnt=0;
            if(b>sqrt(a))
               ans=0;
            else
            {
                for(ll i=1;i<b;i++)
                {
                    if(a%i==0)
                      cnt++;
                }
                num=fenjie(a)/2;//去重
                ans=num-cnt;
            }
            printf("Case %d: %lld
    ",x++,ans);
        }
        return 0;
    }

    D - Leading and Trailing

    题目:

    You are given two integers: n and k, your task is to find the most significant three digits, and least significant three digits of nk.

    Input

    Input starts with an integer T (≤ 1000), denoting the number of test cases.

    Each case starts with a line containing two integers: n (2 ≤ n < 231) and k (1 ≤ k ≤ 107).

    Output

    For each case, print the case number and the three leading digits (most significant) and three trailing digits (least significant). You can assume that the input is given such that nk contains at least six digits.

    Sample Input

    5

    123456 1

    123456 2

    2 31

    2 32

    29 8751919

    Sample Output

    Case 1: 123 456

    Case 2: 152 936

    Case 3: 214 648

    Case 4: 429 296

    Case 5: 665 669

    题意:该题就是给你n,k,求n^k前三位和后三位数

    #include<cstdio>
    #include<cmath>
    #include<iostream>
    typedef long long ll;
    using namespace std;
    //求n^k的前三位数字与后三位数字。
    long long qsm(ll a,ll b,ll mod)
    {
        ll ans=1;
        while(b)
        {
            if(b&1)
               ans=ans*a%mod;
            b>>=1;
            a=a*a%mod;
         } 
         return ans;
     } 
    int main()
    {
        int t;
        int cnt=1;
        cin>>t;
        while(t--)
        {
            ll n,k;
            cin>>n>>k;
            int s=(int)pow(10.0,2.0+fmod(k*log10(n*1.0),1.0));//前三位 
            /*设x = log (n^k)  = k * log10(n),
             那么10^x = n^k.将x = a(整数) + b(小数),
             整数部分10^a是小数点的位置,并不影响前三位数字。
             故只需要求出10^b取前三位。
             使用fmod(a, 1)表示求浮点型数 a 的小数部分。*/
            int e=(int)qsm(n,k,1000);//后三位 
            printf("Case %d: %d %03d
    ",cnt++,s,e);
        }
        return 0;
    }

    E - Goldbach`s Conjecture

    题目:

    Goldbach's conjecture is one of the oldest unsolved problems in number theory and in all of mathematics. It states:

    Every even integer, greater than 2, can be expressed as the sum of two primes [1].

    Now your task is to check whether this conjecture holds for integers up to 107.

    Input

    Input starts with an integer T (≤ 300), denoting the number of test cases.

    Each case starts with a line containing an integer n (4 ≤ n ≤ 107, n is even).

    Output

    For each case, print the case number and the number of ways you can express n as sum of two primes. To be more specific, we want to find the number of (a, b) where

    1)      Both a and b are prime

    2)      a + b = n

    3)      a ≤ b

    Sample Input

    2

    6

    4

    Sample Output

    Case 1: 1

    Case 2: 1

    Note

    1. An integer is said to be prime, if it is divisible by exactly two different integers. First few primes are 2, 3, 5, 7, 11, 13, ...

    题意:该题就是给你一个数n,求出该数能分成几对素数的和

    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int maxn=1e7+10;
    bool a[maxn];
    int prime[1000000];
    int t,n,l=0,x,T=1,num;
    void isprime()
    {
        for(int i=2;i<=10000000;i++)
        {
            if(a[i]==false)
            {
                prime[l++]=i;
                for(int j=i+i;j<=10000000;j+=i)
                   a[j]=true;
            }
        }
    }
    int main()
    {
        isprime();
        int t;
        cin>>t;
        a[0]=a[1]=true;
        while(t--)
        {
            num=0;
            cin>>n;
            for(int i=0;i<l;i++)
            {
                if(prime[i]>=n/2+1)
                   break;
                x=n-prime[i];
                if(a[x]==false)
                {
                    num++;
                }
            }
            printf("Case %d: %d
    ",T++,num);
        }
        return 0;
    }

    H - Harmonic Number

    题目:

    In mathematics, the nth harmonic number is the sum of the reciprocals of the first n natural numbers:

    In this problem, you are given n, you have to find Hn.

    Input

    Input starts with an integer T (≤ 10000), denoting the number of test cases.

    Each case starts with a line containing an integer n (1 ≤ n ≤ 108).

    Output

    For each case, print the case number and the nth harmonic number. Errors less than 10-8 will be ignored.

    Sample Input

    12

    1

    2

    3

    4

    5

    6

    7

    8

    9

    90000000

    99999999

    100000000

    Sample Output

    Case 1: 1

    Case 2: 1.5

    Case 3: 1.8333333333

    Case 4: 2.0833333333

    Case 5: 2.2833333333

    Case 6: 2.450

    Case 7: 2.5928571429

    Case 8: 2.7178571429

    Case 9: 2.8289682540

    Case 10: 18.8925358988

    Case 11: 18.9978964039

    Case 12: 18.9978964139

    题意:求前n项和,1+1/2+1/3.。。。。。

    该题直接暴力不行,需要用调和级数调和级数,f(n)≈ln(n)+C+1n     ,欧拉常数值:C≈0.57721566490153286060651209

    #include<cstdio>
    #include<cmath>
    #include<iostream>
    using namespace std;
    //公式:f(n)=ln(n)+C+1/(2*n); 
    const double c=0.57721566490153286060651209;
    double a[10000];
    int main()
    {
        a[1]=1;
        for(int i=2;i<10000;i++)
           a[i]=a[i-1]+1.0/i;
        int t;
        cin>>t;
        for(int i=1;i<=t;i++)
        {
            int n; 
            cin>>n;
            if(n<10000)
               printf("Case %d: %.10lf
    ",i,a[n]);
            else
            {
                double a=log(n)+c+1.0/(2*n);
                printf("Case %d: %.10lf
    ",i,a);
            }
        }
        return 0;
    }

    T - Primes

    题目:

    Write a program to read in a list of integers and determine whether or not each number is prime. A number, n, is prime if its only divisors are 1 and n. For this problem, the numbers 1 and 2 are not considered primes.

    InputEach input line contains a single integer. The list of integers is terminated with a number<= 0. You may assume that the input contains at most 250 numbers and each number is less than or equal to 16000.
    OutputThe output should consists of one line for every number, where each line first lists the problem number, followed by a colon and space, followed by "yes" or "no".
    Sample Input

    1
    2
    3
    4
    5
    17
    0

    Sample Output

    1: no
    2: no
    3: yes
    4: no
    5: yes
    6: yes

    该题就是判断素数
    #include<bits/stdc++.h>
    using namespace std;
    int p[20000];
    void prime()
    {
        memset(p,0,sizeof(p));
        for(int i=2;i<=20000;i++)
        {
            if(!p[i])
            {
                for(int j=i*i;j<=20000;j+=i)
                  p[j]=1;
            }
        }
    }
    int main()
    {
        int c=0;
        int n;
        while(cin>>n)
        {
            if(n<=0)break;
            c++;
            prime();
            if(n==1)
               printf("%d: no
    ",c);
            else if(n==2)
               printf("%d: no
    ",c);
            else
            {
                if(p[n]==1)
                   printf("%d: no
    ",c);
                else
                   printf("%d: yes
    ",c);
                  
            }
            
        }
        return 0;
    } 

    U - Maximum GCD

    题目:

    Given the N integers, you have to find the maximum GCD (greatest common divisor) of every possible pair of these integers.
    Input
    The first line of input is an integer N (1 < N < 100) that determines the number of test cases. The following N lines are the N test cases. Each test case contains M (1 < M < 100) positive integers that you have to find the maximum of GCD.
    Output
    For each test case show the maximum GCD of every possible pair.
    Sample Input
    3

    10 20 30 40

    7 5 12

    125 15 25
    Sample Output
    20

    1

    25

    题意:给你一串数,从中求出最大公约数,难点在于输入及转换,最大公约数gcd函数

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    int gcd(int a,int b)
    {
        if(b==0)
          return a;
        else
          return gcd(b,a%b);
    }
    int main()
    {
        int t,n,len,i,j,a[200]={0};
        char s[200];
        cin>>t;
        getchar();
        while(t--)
        {
            gets(s);
            len=strlen(s);
            int k=0,sum=0,max=0,g;
            for(i=0;i<=len;i++)
            {
                if(s[i]==' '||s[i]=='')
                {
                    a[k++]=sum;
                    sum=0;
                    i++;
                }
                sum=sum*10+s[i]-'0'; 
            }
            sort(a,a+k);
            for(i=k-1;i>0;i--)
              for(j=i-1;j>=0;j--)
              {
                  g=gcd(a[i],a[j]);
                  if(g>max)
                    max=g;
              }
            cout<<max<<endl;
        }
        return 0;
    }

    总结:

    经过两个周数学专题的学习,我再一次深刻意识到了自己的差劲。虽然我做了八道题,但是大部分都查了题解,然后是根据题解,知道用什么方法,再尝试自己做。

    这次专题中的题,有很多都和素数有关,素数的筛选出现的次数很多,我也差不多掌握了些皮毛。还知道了一些数学公式,就是那种简便的思想,如唯一分解定理:N = p1^a1p2^a2p3^a3* ... *pn^an(其中p1、p2、... pn为N的因子,a1、a2、... 、an分别为因子的指数),快速幂,调和级数,f(n)≈ln(n)+C+1n     ,欧拉常数值:C≈0.57721566490153286060651209。。。。

    总之,题不会做的原因就是知道的太少,题练的太少,我会多看书,也会好好做题的。。。。

  • 相关阅读:
    Array方面Js底层代码学习记录
    DOM 节点
    跨域
    狂雨cms代码审计:后台文件包含getshell
    在PHP一句话木马使用过程中的种种坑点分析
    记对某CMS的一次代码审计
    通达OA任意文件上传并利用文件包含导致远程代码执行漏洞分析
    DedeCMS V5.7 SP2后台存在代码执行漏洞
    zzzcms(php) v1.7.5 前台SQL注入及其他
    权限维持及后门持久化技巧总结
  • 原文地址:https://www.cnblogs.com/ylrwj/p/10990552.html
Copyright © 2011-2022 走看看