Simple math topics + summary

A - prime Sieve

topic:

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

The meaning of problems: the problem is to say, the value of the score of each of the resultant bamboo student must be greater than or equal to his lucky numbers, bamboo score value is less than the number of values ​​of x and a length x coprime, and per unit length spend 1Xukha, seeking to buy a minimum cost of these bamboo.

Euler function, as a function of the Euler properties: primes Euler function value of the pixel value of -1

So we can look lucky numbers +1, if the number is prime, the score value is -1, and the condition is satisfied, the required condition is satisfied length of 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\n",x,sum);
    } 
    return 0;
 } 

B - decomposition of the quality factor

topic:

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

The meaning of problems: the problem is to give you two numbers a, b, seeking to satisfy c * d == a and c> = b and d> = log b, c, d tuples, and (c, d) and (d, c) belong to the same case

The problem with a unique decomposition theorem requires N ^ A1 = P1 P2 ^ A2 P3 * ... * ^ A3 ^ AN PN (wherein p1, p2, ... pn factor of N, a1, a2, ..., an They are exponential factor)

#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\n",x++,ans);
    }
    return 0;
}

D - Leading and Trailing

topic:

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

The meaning of problems: the problem is to give you n, k, n ^ k seek first three and the last three digits

#include <cstdio> 
#include <the cmath> 
#include <the iostream> 
typedef Long  Long LL;
 the using  namespace STD;
 // find the first three digits n ^ k three digits after. 
Long  Long QSM (LL A, LL B, LL MOD) 
{ 
    LL ANS = . 1 ;
     the while (B) 
    { 
        IF (B & . 1 ) 
           ANS = ANS * A% MOD; 
        B >> = . 1 ; 
        A = A * A% MOD ; 
     } 
     return ANS; 
 } 
intmain () 
{ 
    int T;
     int CNT = . 1 ; 
    CIN >> T;
     the while (T-- ) 
    { 
        LL n-, K; 
        CIN >> >> n- K;
         int S = ( int ) POW ( 10.0 , 2.0 + FMOD (K * loglO (* n- 1.0 ), 1.0 )); // first three 
        / * set log x = (^ n-K) = K * loglO (n-), 
         . then 10 ^ x = n ^ k x = a a (an integer) + b (decimal), 
         the integer part is 10 ^ a decimal place, it does not affect the first three digits. 
         It requires only a 10 ^ b taken three. 
         Use fmod (a, 1) represent the fractional portion of a floating-point demand. * /
        int E = ( int ) QSM (n-, K, 1000 ); // three after 
        the printf ( " Case% D: D%% 03D \ n- " , CNT ++ , S, E); 
    } 
    return  0 ; 
}

E - Goldbach`s Conjecture

topic:

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, ...

The meaning of problems: the problem is to give you a number n, find the number can be divided into several pairs of primes and

#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\n",T++,num);
    }
    return 0;
}

H - Harmonic Number

topic:

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

The meaning of problems: the first n items and ask, 1 + 1/2 + 1/3 .. . . . .

The title direct violence not need to use the harmonic series harmonic series, f (n) ≈ln (n) + C + 1n, Euler constant value: 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\n",i,a[n]);
        else
        {
            double a=log(n)+c+1.0/(2*n);
            printf("Case %d: %.10lf\n",i,a);
        }
    }
    return 0;
}

T - Primes

topic:

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 

The problem is to determine the prime number
#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\n",c);
        else if(n==2)
           printf("%d: no\n",c);
        else
        {
            if(p[n]==1)
               printf("%d: no\n",c);
            else
               printf("%d: yes\n",c);
              
        }
        
    }
    return 0;
} 

U - Maximum GCD

topic:

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

Meaning of the questions: to give you a bunch of number, which obtained the greatest common divisor, difficulty is input and conversion, the greatest common divisor gcd function

#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]=='\0')
            {
                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;
}

to sum up:

After two weeks of learning mathematics topics, I once again deeply aware of her own poor. Although I did eight questions, but most of them checked the solution to a problem, and is based on the solution to a problem, we know what means, and then try to do it yourself.

The topic of questions, many of which are related to and primes, the number of prime numbers appear screening a lot, I have almost mastered some fur. Also know some mathematical formulas, is the kind of simple ideas, such as the unique decomposition theorem: N p1 = a1 ^ P2 ^ a2 p3 ^ a3 * ... * the pn ^ AN (where p1, p2, ... pn is N factor, a1, a2, ..., an exponential factor, respectively), rapid power, harmonic series, f (n) ≈ln (n ) + C + 1n, Euler constant value: C≈0.57721566490153286060651209. . . .

In short, the reason for the question is to know not to do too little, too little practice questions, I will be more reading, I will do a good question. . . .

Guess you like

Origin www.cnblogs.com/ylrwj/p/10990552.html