Day23

Day23

今天讲的是 算术基本原理 ,没咋学会。。。
有是自闭的一天
补题

数论解释

真的有必要好好看一下
数论

算术基本原理

解释

简介

算术基本定理,又称为正整数的唯一分解定理,即:每个大于1的自然数,要么本身就是质数,要么可以写为2个或以上的质数的积,而且这些质因子按大小排列之后,写法仅有一种方式。

例如: 6936 = 2^3 3 17^2, 1200 = 2^4 3 5^2

算术基本定理的内容由两部分构成:

分解的存在性:
分解的唯一性,即若不考虑排列的顺序,正整数分解为素数乘积的方式是唯一的。
算术基本定理是初等数论中一个基本的定理,也是许多其他定理的逻辑支撑点和出发点。

详情链接

假代码模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<stdio.h>
#include<algorithm>
#include<map>
using namespace std;
int a[1000];
map<int,int>ma;
int main()
{
int n;
int num=0;
scanf("%d",&n);
int m=n;
for(int i=2;i*i<=n;i++)
{
if(n%i==0) a[num++]=i;
while(n%i==0)
{
ma[i]++;
n=n/i;
}
}
if(n>1) a[num++]=n,ma[n]++;
for(int i=0;i<num;i++)
{
printf("%d %d\n",a[i],ma[a[i]]);
}
return 0;
}

Smith Numbers

Description:
While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh University,noticed that the telephone number of his brother-in-law H. Smith had the following peculiar property: The sum of the digits of that number was equal to the sum of the digits of the prime factors of that number. Got it? Smith’s telephone number was 493-7775. This number can be written as the product of its prime factors in the following way:
4937775= 355*65837

The sum of all digits of the telephone number is 4+9+3+7+7+7+5= 42,and the sum of the digits of its prime factors is equally 3+5+5+6+5+8+3+7=42. Wilansky was so amazed by his discovery that he named this kind of numbers after his brother-in-law: Smith numbers.
As this observation is also true for every prime number, Wilansky decided later that a (simple and unsophisticated) prime number is not worth being a Smith number, so he excluded them from the definition.
Wilansky published an article about Smith numbers in the Two Year College Mathematics Journal and was able to present a whole collection of different Smith numbers: For example, 9985 is a Smith number and so is 6036. However,Wilansky was not able to find a Smith number that was larger than the telephone number of his brother-in-law. It is your task to find Smith numbers that are larger than 4937775!
Input
The input file consists of a sequence of positive integers, one integer per line. Each integer will have at most 8 digits. The input is terminated by a line containing the number 0.
Output
For every number n > 0 in the input, you are to compute the smallest Smith number which is larger than n,and print it on a line by itself. You can assume that such a number exists.

1
2
3
4
5
Sample Input
4937774
0
Sample Output
4937775

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<stdio.h>
#include<algorithm>
#include<map>
#include<cmath>
#include<cstring>
#include<iostream>
using namespace std;
const int maxx = 1e5;
int a[1000];
int num;
map<int,int>ma;
bool isprime(int n)
{
for(int i = 2; i * i <= n; i ++)
{
if(n % i == 0)
return false;
}
return true;
}
int sum(int n)
{
int i, sum = 0;
while(n)
{
i = n % 10;
sum += i;
n /= 10;
}
return sum;
}

int cut(int n)
{
//分治的思想,如果是素数,就返回sum,否则,将该数分成两部分,再求各部分的质因子的sum
if(isprime(n))
return sum(n);
for(int i = (int)sqrt(n + 0.5); i > 1; i --)
{
if(n % i == 0)
return cut(i) + cut(n / i);
}
}
int main()
{
int n;
while(~scanf("%d",&n))
{
if(n == 0) break;
while(n ++)
{
if(!isprime(n) && sum(n) == cut(n))
break;
}
printf("%d\n",n);
}

return 0;
}

Pairs Forming LCM

Description:
Find the result of the following code:

1
2
3
4
5
6
7
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 multiple
return 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)’.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e7;
typedef long long ll;
int p[maxn/10],m;//素数的数组开始1e7会RE
bool vis[maxn];
void init()
{
m=0;
for(int i=2;i<maxn;i++)
{
if(!vis[i]) p[m++]=i;
for(int j=0;j<m&&p[j]*i<maxn;j++)
{
vis[p[j]*i]=1;
if(i%p[j]==0) break;
}
}
}
int main()
{
init();
ll n,ans,c;
int t,f=0;
cin>>t;
while(t--)
{
cin>>n;
ans=1;
for(int i=0;i<m;i++)
{
if(p[i]*p[i]>n) break;
c=0;
while(n%p[i]==0)
{
n/=p[i];
++c;
}
if(c) ans*=c*2+1;
}
if(n>1) ans*=1*2+1;
printf("Case %d: %lld\n",++f,ans/2+1);
}
}

Ekka Dokka

Description:
Ekka and his friend Dokka decided to buy a cake. They both love cakes and that’s why they want to share the cake after buying it. As the name suggested that Ekka is very fond of odd numbers and Dokka is very fond of even numbers, they want to divide the cake such that Ekka gets a share of N square centimeters and Dokka gets a share of M square centimeters where N is odd and M is even. Both N and M are positive integers.

They want to divide the cake such that N * M = W, where W is the dashing factor set by them. Now you know their dashing factor, you have to find whether they can buy the desired cake or not.

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

Each case contains an integer W (2 ≤ W < 263). And W will not be a power of 2.

Output
For each case, print the case number first. After that print “Impossible” if they can’t buy their desired cake. If they can buy such a cake, you have to print N and M. If there are multiple solutions, then print the result where M is as small as possible.

1
2
3
4
5
6
7
8
9
Sample Input
3
10
5
12
Sample Output
Case 1: 5 2
Case 2: Impossible
Case 3: 3 4

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxx = 1e5;

int main()
{
int T;
ll w;
scanf("%d",&T);
int k = 0;
while(T --)
{
scanf("%lld",&w);
ll n = 1, m = 1;

if(w % 2 == 0)
{
while(w % 2 == 0)
{
n = n * 2;
w = w / 2;
}
printf("Case %d: %lld %lld\n", ++ k, w, n);
}
else
printf("Case %d: Impossible\n",++ k);
}

return 0;
}

Sum of Consecutive Integers

Description:
Given an integer N, you have to find the number of ways you can express N as sum of consecutive integers. You have to use at least two integers.

For example, N = 15 has three solutions, (1+2+3+4+5), (4+5+6), (7+8).

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 number of ways to express N as sum of consecutive integers.

1
2
3
4
5
6
7
8
9
10
11
12
13
Sample Input
5
10
15
12
36
828495
Sample Output
Case 1: 1
Case 2: 3
Case 3: 1
Case 4: 2
Case 5: 47

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e7+10;
ll n,p[maxn/10];
bool is[maxn];
int t,ca,pos;
void init()
{
for(int i=2;i<maxn;i++)
{
if(!is[i]) p[pos++]=i;
for(int j=0;j<pos&&i*p[j]<maxn;j++)
{
is[i*p[j]]=1;
if(i%p[j]==0) break;
}
}
}
int main()
{
cin>>t;
init();
while(t--)
{
ll ans=1;
cin>>n;
for(int i=0;i<pos&&p[i]*p[i]<=n;i++)
{
ll cnt=0;a
while(n%p[i]==0) cnt++,n/=p[i];
if(i) ans*=cnt+1;
}
if(n>2) ans*=2;
printf("Case %d: %lld\n",++ca,ans-1);
}
}

Aladdin and the Flying Carpet

Description:
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.

1
2
3
4
5
6
7
Sample Input
2
10 2
12 2
Sample Output
Case 1: 1
Case 2: 2

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6;
typedef long long ll;
ll p[maxn],pos,vis[maxn];
void init()
{
pos=0;
for(int i=2;i<maxn;i++)
{
if(!vis[i]) p[pos++]=i;
for(int j=0;j<pos&&p[j]*i<maxn;j++)
{
vis[p[j]*i]=1;
if(i%p[j]==0) break;
}
}
}
int main()
{
ll t,a,b,l=0;
scanf("%lld",&t);
init();
while(t--)
{
ll now=0,ans=1,aa,net=0;
scanf("%lld %lld",&a,&b);
if(b*b>=a)
{
printf("Case %d: 0\n",++l);
continue;
}
aa=a;
for(int i=0;i<pos&&p[i]<=a;i++)
{
int c=0;
while(a%p[i]==0)
{
a/=p[i];
c++;
}
ans*=(c+1);
}
if(a>1) ans*=2;
ans/=2;
for(ll i=1;i<b;i++)
{
if(aa%i==0)
ans--;
}
printf("Case %lld: %lld\n",++l,ans);
}
}

Minimum Sum LCM

Description:
LCM (Least Common Multiple) of a set of integers is defined as the minimum number, which is a
multiple of all integers of that set. It is interesting to note that any positive integer can be expressed
as the LCM of a set of positive integers. For example 12 can be expressed as the LCM of 1, 12 or 12, 12 or 3, 4 or 4, 6 or 1, 2, 3, 4 etc.
In this problem, you will be given a positive integer N. You have to find out a set of at least two positive integers whose LCM is N. As infinite such sequences are possible, you have to pick the sequence whose summation of elements is minimum. We will be quite happy
if you just print the summation of the elements of this set. So, for N = 12, you should print 4+3 = 7 as LCM of 4 and 3 is 12 and 7 is the minimum possible summation.
Input
The input file contains at most 100 test cases. Each test case consists of a positive integer N (1 ≤ N ≤ 2^31 − 1).
Input is terminated by a case where N = 0. This case should not be processed. There can be at most 100 test cases.
Output
Output of each test case should consist of a line starting with ‘Case #: ’ where # is the test case number. It should be followed by the summation as specified in the problem statement. Look at the output for sample input for details.

1
2
3
4
5
6
7
8
9
Sample Input
12
10
5
0
Sample Output
Case 1: 7
Case 2: 7
Case 3: 6

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll n,cas=1;
while(cin>>n&&n)
{
ll ans=0,cnt=0,x=n;
for(ll i=2;i*i<=n;i++)
{
ll mid=1;
if(n%i==0)
{
cnt++;
while(n%i==0)
{
n/=i;
mid*=i;
}
ans+=mid;
}
}
if(n==x) ans=x+1;
else if(n!=1 || cnt == 1) ans+=n;
printf("Case %lld: %lld\n",cas++,ans);
}
}

GCD and LCM

Description:
Given two positive integers G and L, could you tell me how many solutions of (x, y, z) there are, satisfying that gcd(x, y, z) = G and lcm(x, y, z) = L?
Note, gcd(x, y, z) means the greatest common divisor of x, y and z, while lcm(x, y, z) means the least common multiple of x, y and z.
Note 2, (1, 2, 3) and (1, 3, 2) are two different solutions.
Input
First line comes an integer T (T <= 12), telling the number of test cases.
The next T lines, each contains two positive 32-bit signed integers, G and L.
It’s guaranteed that each answer will fit in a 32-bit signed integer.
Output
For each test case, print one line with the number of solutions satisfying the conditions above.

1
2
3
4
5
6
7
Sample Input
2
6 72
7 33
Sample Output
72
0

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
#include<stdio.h>
using namespace std;
typedef long long ll;

int main()
{
int T;
scanf("%d",&T);
while(T --)
{
ll L, G;
scanf("%lld %lld",&G, &L);
ll n = L / G;
//L % G != 0 无解
ll ans = L % G ? 0 : 1;
for(ll i = 2; i * i <= n; i ++)
{
//m是n某一个素因子的幂级数
ll m = 0;
if(n % i != 0) continue;
while(n % i == 0)
{
m ++;
n = n / i;
}
ans = ans * 6 * m;
}
//n仍然不等于1说明此时n是一个大素数
if(n != 1)
ans *= 6;
printf("%lld\n",ans);
}
return 0;
}
/*
显然若lcm%gcd!=0时无解,令n=lcm/gcd,对n质因数分解后得到n=p1^k1*p2^k2*…*pm^km,那么必然有
a/g=p1^a1*p2^a2*…*pm^am
b/g=p1^b1*p2^b2*…*pm^bm
c/g=p1^c1*p2^c2*…*pm^cm
所以对于任意i(1<=i<=m),都有min(ai,bi,ci)=0,max(ai,bi,ci)=ki,当ai,bi,ci三者之中居中者取1~ki-1时,总共有6*(ki-1)种情况,当取0或者ki时,有2*3=6种情况,所以对于每个i,都有6*(ki-1)+6=6*ki种情况,所以枚举n的所有质因子幂级数k每次累乘6*k即可
*/


//另一种代码
#include<cstdio>
typedef long long ll;
int main()
{
int t;
long long m, n, ans, i, count;
scanf("%d", &t);
while (t--)
{
scanf("%lld%lld", &m, &n);
if (n % m) puts("0");///注意特判
else
{
n /= m;
ans = 1;
for (i = 2; i * i <= n; i += 2)///不用求素数,因为范围很小(注意n在不断减小)
{
if (n % i == 0)
{
count = 0;
while (n % i == 0)
{
n /= i;
++count;
}
ans *= 6 * count;
}
if (i == 2)
--i;///小技巧
}
if (n > 1) ans *= 6;
printf("%lld\n", ans);
}
}
return 0;
}

---------------- The End ----------------
0%