涉及到知识

1.普通筛选、埃拉托斯特尼筛选、欧拉筛选

Bi-shoe and Phi-shoe

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 

Sample Output

Case 1: 22 Xukha
Case 2: 88 Xukha
Case 3: 4 Xukha

题目大意:一个竹竿长度为p,它的score值就是比p长度小且与且与p互质的数字总数,比如9有1,2,4,5,7,8这六个数那它的score就是6。给你T组数据,每组n个学生,每个学生都有一个幸运数字,求出要求买n个竹子每个竹子的score都要大于或等于该学生的幸运数字,每个竹竿长度就是花费,求最小花费。

欧拉函数是指:对于一个正整数n,小于n且和n互质的正整数(包括1)的个数,记作φ(n) 。

通式:φ(x)=x*(1-1/p1)*(1-1/p2)*(1-1/p3)*(1-1/p4)…..(1-1/pn),其中p1, p2……pn为x的所有质因数,x是不为0的整数。φ(1)=1(唯一和1互质的数就是1本身)。

对于质数p,φ(p) = p - 1。注意φ(1)=1.

欧拉定理:对于互质的正整数a和n,有aφ(n) ≡ 1 mod n。

欧拉函数是积性函数——若m,n互质,φ(mn)=φ(m)φ(n)。

若n是质数p的k次幂,φ(n)=p^k-p^(k-1)=(p-1)p^(k-1),因为除了p的倍数外,其他数都跟n互质。

特殊性质:当n为奇数时,φ(2n)=φ(n)

欧拉函数还有这样的性质:

设a为N的质因数,若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N / a) * a;若(N % a == 0 && (N / a) % a != 0) 则有:E(N) = E(N / a) * (a - 1)。

看完上面的内容,我们就知道一根长度为p的竹竿它的score其实就是欧拉函数值φ(p)。又因为一个素数p的φ(p)=p-1,所以我们只需要从x+1(x是幸运数字)开始找第一个出现的素数,那就是最小花费。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+5;
/*
bool prime[N];
int n;
void is_prime(){for(int i=2;i<N;i++){prime[i]=true;}for(int i=2;i*i<N;i++){if(prime[i]){for(int j=i*i;j<=N;j+=i){prime[j]=false;}}}for(int i=1;i<=N;i++)printf("%d %d\n",i,prime[i]);
}
*/
bool vis[N];
int prime[N];
int cnt;
void Eulerprim()
{memset(vis,0,sizeof(vis));for(int i=2; i<=N; i++){if(!vis[i])prime[cnt++]=i;for(int j=0; j<cnt; j++){if(i*prime[j]>N)break;vis[i*prime[j]]=i;if(i%prime[j]==0)break;}}
}int main()
{//is_prime();int n;Eulerprim();int t;int CS=1;scanf("%d",&t);while(t--){scanf("%d",&n);ll sum=0;for(int i=0;i<n;i++){int x;scanf("%d",&x);for(int j=x+1;;j++)if(vis[j]==0){sum+=j;break;}}printf("Case %d: %lld Xukha\n",CS++,sum);}return 0;
}

Aladdin and the Flying Carpet

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 ≤ ) 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^k1)*(p2^k2)*...*(pn*kn).(分解方式唯一)      

n的约数个数为cnt(n)=(1+k1)*(1+k2)*...*(1+kn).

题解:

先求出a的约数个数cnt(a),再暴力枚举出[1,b)中a的约数c,因为题目说了不会存在c==d的情况,因此cnt要除2,去掉重复情况,然后枚举小于b的a的约数,拿cnt减掉就可以了。 所以  cnt/2-c即为答案。

#include<bits/stdc++.h>
#define MAXN 1000005
using namespace std;
typedef long long ll;int prime[MAXN];
bool vis[MAXN];
ll cnt;
ll n;
void isprime()
{cnt=0;for(int i=2;i<=MAXN;i++){if(!vis[i])prime[cnt++]=i;for(int j=0;j<cnt && i*prime[j]<=MAXN;j++){vis[i*prime[j]]=1;if(i%prime[j]==0)break;}}
}
ll solve()
{ll sum=n;ll ans=1;for(int i=0;i<cnt && prime[i]<sum;i++){int k=0;while(sum%prime[i]==0){sum/=prime[i];k++;}ans*=(1+k);}if(sum>1)ans*=(1+1);//多出一个素数来return ans;
}
int main()
{int t;int CS=1;isprime();ll d;scanf("%d",&t);ll ans;while(t--){ans=0;ll cnt=0;scanf("%lld%lld",&n,&d);if(d*d>n)ans=0;else{for(int i=1;i<d;i++)if(n%i==0)cnt++;ans=solve()/2-cnt;}printf("Case %d: %lld\n",CS++,ans);}return 0;
}

Sigma Function

Sigma function is an interesting function in Number Theory. It is denoted by the Greek letter Sigma (σ). This function actually denotes the sum of all divisors of a number. For example σ(24) = 1+2+3+4+6+8+12+24=60. Sigma of small numbers is easy to find but for large numbers it is very difficult to find in a straight forward way. But mathematicians have discovered a formula to find sigma. If the prime power decomposition of an integer is

Then we can write,

For some n the value of σ(n) is odd and for others it is even. Given a value n, you will have to find how many integers from 1 to n have even value of σ.

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 ≤ ).

Output

For each case, print the case number and the result.

Sample Input

4
3
10
100
1000

Sample Output

Case 1: 1
Case 2: 5
Case 3: 83
Case 4: 947

题意:1~n (n:1~)中,因子和为偶数的有几个。

因子和 Sum=(p1^0+p1^1….p1^e1)*(p2^0+p2^1…p2^e2)……(pn^0+…pn^en); (1)

由(1)式整理得   

(p1^0+p1^1….p1^e1),(p2^0+p2^1…p2^e2),……(pn^0+…pn^en)中只要有一个是偶数,因子和sum就为偶数。

但是因为结果为偶数不好求所以求出结果为奇数的数量,然后用总的数量 n 减去结果为奇数的数量就为最后结果ans

因为每个()里面的就行都与 en 和 pi有关  

一共有四种情况   奇数(odd )偶数(even)

1. pi为偶数 en为奇数 表示 奇数个 底数为偶数 的指数相加 一定为偶数 再加上 1() 所以一定为 奇数

2. pi为偶数 en为偶数 表示 偶数个 底数为偶数 的指数相加 一定为偶数 再加上 1() 所以一定为 奇数

3.   pi为奇数 en为偶数 表示 偶数个 底数为奇数 的指数相加 一定为偶数 再加上 1() 所以一定为 奇数

4.    pi为奇数 en为奇数 表示 奇数个 底数为奇数 的指数相加 一定为奇数 再加上 1() 所以一定为 偶数

在所有质数中为偶数的只有 2

所以 结果若想为奇数   2 的指数(k1) 既可以为奇数也可以为偶数 但是其他的pi为奇数时 只能为偶数 所以指数都扩大两倍即可

1 2 3 可以整合为

(1)

让   = X

则(1)式等于 *  因为 k1={0,1,2 ......}

所以 当 k1=0 时 X=sqrt(n) 当 k1!=0 时 X=sqrt(n/2) (始终让k1=1 因为大于1其他的二可以提到X里面去 让其只剩下一个2)

#include<stdio.h>
#include<math.h>
typedef long long ll;
int main()
{int t;ll n;scanf("%d",&t);int CS=1;while(t--){scanf("%lld",&n);ll ans;ans=n-(ll)sqrt(n)-(ll)sqrt(n/2);printf("Case %d: %lld\n",CS++,ans);}return 0;
}

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 .

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 < ) and k (1 ≤ k ≤ ).

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 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 求 的前三位数和后三位数

题解:

后三位很好求,快速幂取模就好了,同样前三位我们也可以模拟快速幂来写,用double存储答案(因为在后续相乘过程中效数也可以向前进位如果用ll的话会把小数部分舍去造成精度不够),模拟一下快速幂,,注意输出为3位,如果后三位是022,那么必须输出022,不能输出22 (一个 %03d搞定)

#include<stdio.h>
#include<math.h>
typedef long long ll;
ll Pow(ll a,ll b,ll mod)
{ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod;b/=2;}return ans%mod;
}
double change(double x)
{while(x>=1000.0){x/=10.0;}return x;
}
int main()
{int t;int CS=1;ll n,k;scanf("%d",&t);while(t--){scanf("%lld%lld",&n,&k);int ans1=(int)Pow(n,k,1000);double ans2=1.0;double tmp=n*1.0;while(k){if(k&1){ans2=ans2*tmp;ans2=change(ans2);}tmp=tmp*tmp;tmp=change(tmp);k/=2;}printf("Case %d: %d %03d\n",CS++,(int)ans2,ans1);}return 0;}

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 .

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 ≤ , 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的对数。

题解; 把所有素数预处理一下 完事

if(prime[i]>=n/2+1) break; 一定要加上break 第一次T了 太菜了。。。

#include<stdio.h>
#include<math.h>
#define maxn 10000005
typedef long long ll;ll prime[666666];
bool vis[maxn];
ll cnt;
void isprime()
{cnt=0;vis[1]=1;for(int i=2; i<=10000000; i++){if(!vis[i]){prime[cnt++]=i;for(int j=i+i;j<=10000000;j+=i)vis[j]=1;}}
}int main()
{int t;int CS=1;ll n;scanf("%d",&t);isprime();while(t--){scanf("%lld",&n);ll ans=0;for(int i=0; i<cnt; i++){if(prime[i]>=n/2+1) break;if(!vis[n-prime[i]] && prime[i]*2<=n)ans++;}printf("Case %d: %lld\n",CS++,ans);}return 0;}

Harmonic Number (II)

I was trying to solve problem '1234 - Harmonic Number', I wrote the following code

long long H( int n ) {long long res = 0;for( int i = 1; i <= n; i++ )res = res + n / i;return res;
}

Yes, my error was that I was using the integer divisions only. However, you are given n, you have to find H(n) as in my code.

Input

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

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

Output

For each case, print the case number and H(n) calculated by the code.

Sample Input

11
1
2
3
4
5
6
7
8
9
10
2147483647

Sample Output

Case 1: 1
Case 2: 3
Case 3: 5
Case 4: 8
Case 5: 10
Case 6: 14
Case 7: 16
Case 8: 20
Case 9: 23
Case 10: 27
Case 11: 46475828386

题意:
求前n项和,每一项是n/i;    (数论分块之整数分块)

题解

因为是整除,所以会存在一些相同的序列,那么就将每个相同序列分为一块

  表示值相同的一块

得  

那么 每一块的值

 注意  i  的long long 会爆 int

#include<stdio.h>
#include<math.h>
#define maxn 10000005
typedef long long ll;
int main()
{int t;int CS=1;ll n;scanf("%d",&t);while(t--){scanf("%lld",&n);ll ans=0;ll i;ll r;for(i=1; i<=n; i=r+1){r=n/(n/i);ans+=(r-i+1)*(n/i);}printf("Case %d: %lld\n",CS++,ans);}return 0;}

Mysterious Bacteria

Dr. Mob has just discovered a Deathly Bacteria. He named it RC-01. RC-01 has a very strange reproduction system. RC-01 lives exactly x days. Now RC-01 produces exactly p new deadly Bacteria where x = b^p (where b, p are integers). More generally, x is a perfect p^th power. Given the lifetime x of a mother RC-01 you are to determine the maximum number of new RC-01 which can be produced by the mother RC-01.

Input

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

Each case starts with a line containing an integer x. You can assume that x will have magnitude at least 2 and be within the range of a 32 bit signed integer.

Output

For each case, print the case number and the largest integer p such that x is a perfect p^th power.

Sample Input

3
17
1073741824
25

Sample Output

Case 1: 1
Case 2: 30
Case 3: 2

题意:

给你一个x,求满足x = b^p,p最大是多少?

题解:

x可以表示为:x = p1^e1 * p2^e2 * p3^e3 ....... * pn^en。

如果x = 12 = 2^2*3^1,要让x = b^p,及12应该是12 = 12^1

所以p = gcd (e1,e2,.......en);

比如:24 = 2^3*3^1,p应该是gcd(3, 1) = 1,即24 = 24^1

324 = 3^4*2^2,p应该是gcd(4, 2) = 2,即324 = 18^2

本题有一个坑,就是x可能为负数,如果x为负数的话,x = b^q, q必须使奇数,所以将x转化为正数求得的解如果是偶数的话必须将其一直除2转化为奇数

因为 p = gcd (e1,e2,.......en); 所以可以

所以可以一致提出2放在里面知道p变成奇数

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e6+7;
int prime[MAXN];
bool vis[MAXN];
ll cnt;
ll n;
ll summ[MAXN];
ll cntt;
int flag;
void isprime()
{cnt=0;for(int i=2;i<=MAXN;i++){if(!vis[i])prime[cnt++]=i;for(int j=0;j<cnt && i*prime[j]<=MAXN;j++){vis[i*prime[j]]=1;if(i%prime[j]==0)break;}}
}
ll solve()
{ll sum=n;ll ans=1;flag=0;for(int i=0;i<cnt && prime[i]<sum;i++){int k=0;while(sum%prime[i]==0){sum/=prime[i];k++;}ans*=(1+k);summ[cntt++]=k;}if(sum>1)ans*=(1+1),flag=1;//多出一个素数来//return ans;
}
int main()
{isprime();int t;int CS=1;scanf("%d",&t);while(t--){int flag1=0;scanf("%lld",&n);if(n<0){n=-n,flag1=1;}memset(summ,0,sizeof(summ));cntt=0;solve();if(cntt==0)printf("Case %d: 1\n",CS++);//n为素数else{ll gcd=summ[0];for(int i=1;i<cntt;i++)gcd=__gcd(gcd,summ[i]);if(flag) gcd=__gcd(gcd,1LL);if(flag1)//负数时p不能为偶数{while(gcd%2==0)gcd/=2;}printf("Case %d: %lld\n",CS++,gcd);}}return 0;
}

LCM

Ivan has number bb. He is sorting through the numbers aa from 1 to 10^18, and for every aa writes on blackboard. Here [a,b] stands for least common multiple of aa and bb. Ivan is very lazy, that's why this task bored him soon. But he is interested in how many different numbers he would write on the board if he would finish the task. Help him to find the quantity of different numbers he would write on the board.

Input

The only line contains one integer — b (1≤b≤10^10)

Output

Print one number — answer for the problem.

Input

1

Output

1

Input

2

Output

2

Note

In the first example [a,1]=a, therefore is always equal to 1.

In the second example [a,2] can be equal to a or 2⋅a depending on parity of a. can be equal to 1 and 2.

题意 :

给定一个b 求  [a,b]为a,b的最小公倍数

题解:

找b的公因数即可

太菜了。。。。。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{ll b;scanf("%lld",&b);ll cnt=0;for(ll i=1;i*i<=b;i++){// printf("==\n");if(b%i==0)cnt+=2;}ll x=sqrt(b);if(x*x==b)cnt--;printf("%lld\n",cnt);return 0;
}

Pairs Forming LCM

Find the result of the following code:

long long pairsFormLCM( int n ) {long long res = 0;for( int i = 1; i <= n; i++ )for( int j = i; j <= n; j++ )if( lcm(i, j) == n ) res++; // lcm means least common multiplereturn res;
}

A straight forward implementation of the code may time out. If you analyze the code, you will find that the code actually counts the number of pairs (i, j) for which lcm(i, j) = n and (i ≤ j).

Input

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

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

Output

For each case, print the case number and the value returned by the function 'pairsFormLCM(n)'.

Sample Input

15
2
3
4
6
8
10
12
15
18
20
21
24
25
27
29

Sample Output

Case 1: 2
Case 2: 2
Case 3: 3
Case 4: 5
Case 5: 4
Case 6: 5
Case 7: 8
Case 8: 5
Case 9: 8
Case 10: 8
Case 11: 5
Case 12: 11
Case 13: 3
Case 14: 4
Case 15: 2

题意:在a,b中(a,b<=n)(1 ≤ n ≤ 1014),有多少组(a,b)  (a<b)满足lcm(a,b)==n;

先来看个知识点:

素因子分解:n = p1 ^ e1 * p2 ^ e2 *..........*pn ^ en

for i in range(1,n):

ei 从0取到ei的所有组合

必能包含所有n的因子。

现在取n的两个因子a,b

a=p1 ^ a1 * p2 ^ a2 *..........*pn ^ an

b=p1 ^ b1 * p2 ^ b2 *..........*pn ^ bn

gcd(a,b)=p1 ^ min(a1,b1) * p2 ^ min(a2,b2) *..........*pn ^ min(an,bn)

lcm(a,b)=p1 ^ max(a1,b1) * p2 ^ max(a2,b2) *..........*pn ^ max(an,bn)

'

先对n素因子分解,n = p1 ^ e1 * p2 ^ e2 *..........*pk ^ ek,

lcm(a,b)=p1 ^ max(a1,b1) * p2 ^ max(a2,b2) *..........*pk ^ max(ak,bk)

所以,当lcm(a,b)==n时,max(a1,b1)==e1,max(a2,b2)==e2,…max(ak,bk)==ek

当ai == ei时,bi可取 [0, ei] 中的所有数  有 ei+1 种情况,bi==ei时同理。

那么就有2(ei+1)种取法,但是当ai = bi = ei 时有重复,所以取法数为2(ei+1)-1=2*ei+1。
除了 (n, n) 所有的情况都出现了两次  那么满足a<=b的有 (2*ei + 1)+1) / 2  个

注意 vis 要用bool类型 prime 用 int 否则会爆内存

#include<bits/stdc++.h>
const int MAXN=1e7+7;
using namespace std;
typedef long long ll;
int prime[1000005];
bool vis[MAXN];
ll cnt;
ll n;
void isprime()
{cnt=0;for(int i=2;i<MAXN;i++){if(!vis[i])prime[cnt++]=i;for(int j=0;j<cnt && i*prime[j]<MAXN;j++){vis[i*prime[j]]=1;if(i%prime[j]==0)break;}}//printf("%lld\n",cnt);
}ll solve()
{ll sum=n;ll ans=1;for(int i=0;i<cnt && prime[i]*prime[i]<=sum;i++){ll k=0;while(sum%prime[i]==0){sum/=prime[i];k++;}ans*=(2*k+1);}if(sum>1)ans*=(2*1+1);//多出一个素数来return ans;
}int main()
{int t;int CS=1;isprime();scanf("%d",&t);while(t--){scanf("%lld",&n);ll anss=solve();printf("Case %d: %lld\n",CS++,(anss+1)/2);}return 0;
}

Harmonic Number

In mathematics, the n^th 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 ≤ 10^8).

Output

For each case, print the case number and the n^th 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

题意:

 求Hn

题解

1.精度能保证在10 ^-8就没问题,直接用%.10lf 即可;2. n的范围是10^8,肯定不能正常跑,但是可以用公式,前面10000个可以正常打表,后面的我们就用公式,因为,这个公式成立的 条件,是在n比较大的时候,公式如下: 
r为常数,r=0.57721566490153286060651209(r就是欧拉常数)。

由于题目要求精度为10^-8,常数r也是前人推导出来的,然而也只推导了有限位数,所以正常利用这个公式,并不能达到精度要求,我们只好比较样例和我们自己输出的数据,增添一些可行的项,经尝试,在原公式的基础上,再减去一个1.0/(2*n)恰好可以满足精度

c++ math库中,log即为ln。

所以就有两个公式

Hn=log(n+1)+r-1.0/(2*n);

Hn=log(n)+r+1.0/(2*n); (亲测有效)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e7+7;
const double c=0.57721566490153286060651209;
double a[10010];
int main()
{int t;int n;int CS=1;a[1]=1;for(int i=2;i<=10000;i++)a[i]=a[i-1]+1.0/i;scanf("%d",&t);while(t--){scanf("%d",&n);printf("Case %d: ",CS++);if(n<=10000){printf("%.10f\n",a[n]);}else{//double ans=log(n+1)+c-1.0/(2*n);double ans=log(n)+c+1.0/(2*n);printf("%.10f\n",ans);}}return 0;
}

Large Division

Given two integers, a and b, you should check whether a is divisible by b or not. We know that an integer a is divisible by an integer b if and only if there exists an integer c such that a = b * c.

Input

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

Each case starts with a line containing two integers a (-10^200 ≤ a ≤ 10^200) and b (|b| > 0, b fits into a 32 bit signed integer). Numbers will not contain leading zeroes.

Output

For each case, print the case number first. Then print 'divisible' if a is divisible by b. Otherwise print 'not divisible'.

Sample Input

6
101 101
0 67
-101 101
7678123668327637674887634 101
11010000000000000000 256
-202202202202000202202202 -101

Sample Output

Case 1: divisible
Case 2: divisible
Case 3: divisible
Case 4: not divisible
Case 5: divisible
Case 6: divisible

题意:

给一个a(大数)和一个b判断a是否能整除b

题解:

因为a是大数所以用字符串存,for循环跑字符长度每次都对b取模,看最后ans是否为0

ans=((ans*10)%b+(a[i]-'0')%b)%b;

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e6+7;
char a[MAXN];
int main()
{int t;ll b;int CS=1;scanf("%d",&t);while(t--){scanf("%s%lld",a,&b);int l=strlen(a);if(b<0)b=-b;ll ans=0;for(int i=0;i<l;i++){if(a[i]=='-')continue;ans=((ans*10)%b+(a[i]-'0')%b)%b;}ans%=b;printf("Case %d: ",CS++);if(ans)printf("not divisible\n");else printf("divisible\n");}return 0;
}

Trailing Zeroes (III)

You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. For example, 5! = 120, 120 contains one zero on the trail.

Input

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

Each case contains an integer Q (1 ≤ Q ≤ 108) in a line.

Output

For each case, print the case number and N. If no solution is found then print 'impossible'.

Sample Input

3
1
2
5

Sample Output

Case 1: 5
Case 2: 10
Case 3: impossible

题意:

给你一个数字,这个数字代表N!后面0的个数。给出这个数字,求N的值。

题解:

有算数基本定理可知 N!可划分为 质因数相乘的形式  N!=2^a*3^b*5^c*7^d........
因为只有2*5 才会出现 0   又因为2的数量肯定比5的多  所以计算阶乘中5的数量就可以得到该阶乘后有几个0。
50/5=10  10/5=2  所以50!后有10+2=12个0。

二分N的值即可

注意二分的位置

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define linf 0x3f3f3f3f3f3f3f3f
ll sum(ll x)
{ll sum=0;while(x){sum+=x/5;x/=5;}return sum;
}
int main()
{int t;ll q;int CS=1;scanf("%d",&t);while(t--){scanf("%lld",&q);ll l=0;ll r=linf;ll ans;while(l<=r){ll mid=(l+r)/2;if(sum(mid)>=q)ans=mid,r=mid-1;//因为此位置是r=mid+1所以最会准确值为r-1,如果为l=mid-1则最后结果为l+1else l=mid+1;}if(sum(l+1)!=q)printf("Case %d: impossible\n",CS++);else printf("Case %d: %lld\n",CS++,ans);}return 0;
}

Farey Sequence

The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 < a < b <= n and gcd(a,b) = 1 arranged in increasing order. The first few are
F2 = {1/2}
F3 = {1/3, 1/2, 2/3}
F4 = {1/4, 1/3, 1/2, 2/3, 3/4}
F5 = {1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5}

You task is to calculate the number of terms in the Farey sequence Fn.

Input

There are several test cases. Each test case has only one line, which contains a positive integer n (2 <= n <= 10^6). There are no blank lines between cases. A line with a single 0 terminates the input.

Output

For each test case, you should output one line, which contains N(n) ---- the number of terms in the Farey sequence Fn.

Sample Input

2
3
4
5
0

Sample Output

1
3
5
9

题意:

给出式子F F中分子分母互质,且分子小于分母 求解 Fn的元素个数

题解:

已知 a / b,gcd(a,b)=1,并且分母比分子大,那么Fn的个数一定是比分母小并且与分母互质的数的个数 即求欧拉函数

直接暴力跑欧拉函数会超时,所以需要用前缀和维护一下

#include<stdio.h>
typedef long long ll;
int phi[1000005];
ll sum[1000005];
void euler()
{for(int i=1; i<1000005; i++)phi[i]=i;for(int i=2; i<1000005; i++){if(phi[i]==i){for(int j=i; j<=1000005; j+=i){phi[j]=phi[j]/i*(i-1);}}}
}
int main()
{int n;euler();for(int i=2; i<=1000005; i++)sum[i]=sum[i-1]+phi[i];while(~scanf("%d",&n)){if(n==0)break;printf("%lld\n",sum[n]);}return 0;
}

查找特定的合数

时间限制: 1 Sec  内存限制: 128 MB
提交: 320  解决: 37
[提交] [状态] [讨论版] [命题人:admin]

题目描述

自然数中除了能被1和本身整除外,还能被其他数整除的数叫合数。每个合数都可以写成几个质数相乘的形式,这几个质数都叫做这个合数的质因数。比如8=2×2×2,2就是8的质因数。在1—N(N≤200000)按从小到大顺序排列的自然数序列中,查找第M个有X(2≤X≤6)个不同质因数的合数。例如,第3个有2个不同质因数的合数是12(12只有2、3两个不同的质因数,在12之前有2个不同质因数的合数分别为6和10)。

输入

共1行,分别为M,X。

输出

共1行,为第M个有X个不同质因数的合数。

样例输入

3 2

样例输出

12

[提交] [状态]

题意:

求第m个有x个不同的质因子的合数

题解:

直接欧拉筛选打素数表,然后从4跑到2e5,判断是否符合条件即可

#include <algorithm>
#include<cstdio>
#include<math.h>
const int maxn=2e5;
using namespace std;
int vis[maxn+5];
int prime[maxn+5];
void primeall()//普通筛选
{int cnt=0;for(int i=2;i<=maxn;i++){if(!vis[i])prime[cnt++]=i;for(int j=j*2;j<=maxn;j+=i)vis[j]=1;}
}
void primeall1()//埃拉托斯特尼筛选
{int cnt=0;for(int i=2;i<=maxn;i++){if(!vis[i]){prime[cnt++]=i;for(int j=j*2;j<=maxn;j+=i)vis[j]=1;}}
}
void isprime()//欧拉筛选
{int cnt=0;for(int i=2;i<=maxn;i++){if(!vis[i])prime[cnt++]=i;for(int j=0;j<cnt && i*prime[j]<=maxn;j++){vis[i*prime[j]]=1;if(i%prime[j]==0)break;}}
}int main()
{isprime();int m,x;scanf("%d%d",&m,&x);int ans=0;for(int i=2;i<=maxn;i++){if(!vis[i])continue;int cnt=0;int t=sqrt(i);for(int j=2;j<=t;j++){if(i%j==0){if(!vis[j])cnt++;if(!vis[i/j] && (j*j!=i))cnt++;}if(cnt>x)break;}if(cnt==x)ans++;if(ans==m){printf("%d\n",i);break;}}return 0;
}

普通埃拉托斯特尼筛选

由于一个合数总是可以分解成若干个质数的乘积,那么如果把质数的倍数都去掉,那么剩下的就是质数了.Eratosthenes筛选法的思想特别简单:对于不超过n的每个非负整数p,删除2p,3p,4p,...,当处理完所有数之后,还没有被删除的就是素数.如果用vis[i]表示i已经被删除,则筛选法的代码可以写成:

void isprime()//普通埃拉托斯特尼筛选
{int cnt=0;for(int i=2;i<=maxn;i++){if(!vis[i])prime[cnt++]=i;for(int j=i*2;j<=maxn;j+=i)vis[j]=1;}
}

尽管代码已经相当高效了,但仍然可以进行改进.首先,在"对于不超过n的每个非负数p"中,p可以限定为素数--只需在第二重循环前加一个判断if(!vis[i])即可.另外,内层循环也不必从* 2开始--它已经在= 2时被筛选了.改进后代码如下:

void isprime()//进化埃拉托斯特尼筛选
{int cnt=0;for(int i=2;i<=maxn;i++){if(!vis[i]){prime[cnt++]=i;for(int j=i*2;j<=maxn;j+=i)vis[j]=1;}}
}

Eratosthenes筛选法虽然效率高,但是Eratosthenes筛选法做了许多无用功,一个数会被筛到好几次,最后的时间复杂度是O(nloglogn),对于普通素数算法而言已经非常高效了,但欧拉筛选法的时间复杂度仅仅为O(n).

欧拉筛选法的思想就是不做无用功,原本Eratosthenes筛法的第一重循环是用来找素数,然后把素数的倍数标记,而欧拉筛法换了一个角度,第一位是找素数没有问题,但是标记的时候用的是所有数.欧拉筛选法在数据小的时候不如Eratosthenes筛选法快,反而是数据变大以后,两者差距变得越来越明显,欧拉筛选法明显快于Eratosthenes筛选法.欧拉筛选法代码如下:

void isprime()//欧拉筛选
{int cnt=0;for(int i=2;i<=maxn;i++){if(!vis[i])prime[cnt++]=i;for(int j=0;j<cnt && i*prime[j]<=maxn;j++){vis[i*prime[j]]=1;if(i%prime[j]==0)break;}}
}

永远不可能学会的数论之基础数论(例题)相关推荐

  1. 【数论】基础数论概念

    基础数论概念 首先我们来回顾一下基础数论中关于整数集Z={-,-2,-1,0,1,2,-}和自然数集N={0,1,2,3,4,-}的一些概念. 整除性与约数 一个整数可以被另一个整数整除是数论中的一个 ...

  2. 算法之基础数论应用篇(一)

    基础数论应用篇 子集和 题目描述 筛质数 筛质数模板 欧拉筛 线性筛 哥德巴赫猜想 夏洛克和他的女朋友 二次筛法 分解质因数 试除法分解质因数 分解阶乘质因子 快速幂 模板 快速幂 快速乘法 序列的第 ...

  3. RSA算法原理——(2)RSA简介及基础数论知识

    上期为大家介绍了目前常见加密算法,相信阅读过的同学们对目前的加密算法也算是有了一个大概的了解.如果你对这些解密算法概念及特点还不是很清晰的话,昌昌非常推荐大家可以看看HTTPS的加密通信原理,因为HT ...

  4. nssl1174-阶乘【!基础!数论】

    前言 比赛时xjq说这道题很水,是个基础数论. 然后- 就连交都没交 正题 给出n个数,求一个最小的mmm使得 m!∏i=1nai=q(q∈N+)\frac{m!}{\prod_{i=1}^na_i} ...

  5. 数学/基础数论——从LeetCode题海中总结常见套路

    今天是大年初一,祝大家新年快乐! 目录 基础数论求质数:LeetCode204.计数质数 常规解法: 娱乐一下:偷鸡式解法 埃拉托色尼筛选法 统计5因子的个数:LeetCode172.阶乘后的零 暴力 ...

  6. 欧几里得定理 java,每个程序员都应该知道的基础数论

    原标题:每个程序员都应该知道的基础数论 这篇文章讨论了数论中每个程序员都应该知道的几个重要概念.本文的内容既不是对数论的入门介绍,也不是针对数论中任何特定算法的讨论,而只是做为数论的一篇参考. 0. ...

  7. 带瀑布流的电钢琴_让人人都能学会弹钢琴——零基础入门的智能电钢琴 POP Piano...

    原标题:让人人都能学会弹钢琴--零基础入门的智能电钢琴 POP Piano 前言 其实你一直希望成为一个会弹琴的人,只是一直没有机会开始. 如果是20世纪是网络纪元,那么21世纪就是AI纪元.当人类创 ...

  8. 数论 - 约数基础 【 试除法求所有约数 + 约数个数和约数之和 + 欧几里得算法-求解最大公约数 】

    数论-约数基础 1.约数定义 约数,又称因数.整数a除以整数b(b≠0) 除得的商正好是整数而没有余数,我们就说a能被b整除,或b能整除a.a称为b的倍数,b称为a的约数.在大学之前,"约数 ...

  9. Python基础知识例题

    Python基础知识例题 1.任务:一小球以 5 米/秒的水平速度平抛,重力加速度取 9.8 米每秒的平方,在忽略空气阻力的情况下,求经过时间 t 秒后,小球所在位置与抛出点之间的距离 (假设小球距地 ...

最新文章

  1. pdf阅读器下载支持win7_福昕PDF阅读器(Foxit Reader)下载|福昕PDF阅读器(Foxit Reader) 9.6.1.25160 官方版...
  2. HOG(方向梯度直方图)
  3. linux进程调度之 FIFO 和 RR 调度策略
  4. 基于Python实现自动慢查询分析,邮件自动发送
  5. 思科6509的详细配置---加注释
  6. ArcGIS API for Silverlight开发入门(0):为什么要用Silverlight API?
  7. 不确定性原理的前世今生 · 数学篇(完)
  8. 数据挖掘导论课后习题答案-第四章
  9. STM32芯片命名规则及含义
  10. [转]420个JS网页特效
  11. oracle chr(10)用法,chr(10) chr(13)
  12. 杨忠国老师专家点评:全球5G手机销量榜出炉
  13. sql 闩锁 原因_关于SQL Server中的闩锁
  14. 音视频基本概念和FFmpeg的简单入门(新手友好+FFmpeg资料分享)
  15. matlab 更换坐标轴_科学网—【Matlab】坐标轴的设置 - 叶瑞杰的博文
  16. ffmpeg+dxva2 +D3D9显示 学习笔记
  17. 精彩回顾 |风丘科技亮相广州国际汽车测试技术展
  18. upperbound找不到_lower_bound()函数和upper_bound()函数,以及二分查找
  19. 遇到oracle错误1445,sql附加数据库失败
  20. Java 打印PPT幻灯片

热门文章

  1. 用JavaScript写一个类似于windows的气泡屏保效果
  2. 2011年1月13日抓取的QQ空间报错信息
  3. ns-3网络仿真(简易脚本解析)
  4. 图像融合遇见Transformer
  5. Excel输出随机小数的方法
  6. 电脑安装android办公,办公室上班族的福利,在电脑上玩安卓系统
  7. 初识Openlayers
  8. SAS学习笔记4:实战
  9. MOS管驱动电路分析及详解-KIA MOS管
  10. 用IAR下载出现 Stack pointer is setup to incorrect alignment. Stack addr = 0xFFFFFFFF 如何解决?