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= 3*5*5*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.
Sample Input
4937774
0
Sample Output
4937775
Problem solving:
这道题的意思就是如果一个数的每一位的和跟它所有的质因子的每一位的和相等就叫这个Smith Numbers。并且Smith Numbers不能是质数。给你一个数,问你第一个大于它的Smith Number。

直接暴力就行了,需要用的质因子的分解,这个是有模板的,可以直接套。

Code:

#include<iostream>
#include<map>
#include<cmath>
using namespace std;
typedef long long ll;
ll a[10005];

bool ju(ll x)
{
    for(ll i=2;i<=sqrt(x);i++)
        if(x%i==0)    return 0;
    return 1;
}
bool judge(ll x)
{
    map<ll,ll> ma;
    ll mid,miid,ans=0;
    mid=x,miid=x;
    while(mid)
    {
        ans+=mid%10;
        mid/=10;
    }
    int pos=0;
    for(ll i=2;i*i<=miid;i++)
    {
        if(miid%i==0)    a[pos++]=i;
        while(miid%i==0)
        {
            ma[i]++;
            miid/=i;
        }
    }
    if(miid>1)    a[pos++]=miid,ma[miid]++;
    int ans2=0;
    mid=0;
    for(int i=0;i<pos;i++)
    {
        int sijia,xiaozhu=0;    sijia=ma[a[i]];
        while(a[i])
        {
            xiaozhu+=a[i]%10;
            a[i]/=10;
        }
        ans2+=(xiaozhu*sijia);
    }
    return ans==ans2;
}
int main()
{
    ll n;
    while(cin>>n&&n)
    {
        for(int i=n+1;;i++)
        {
            if(judge(i)&&!ju(i))
            {
                cout<<i<<endl;
                break;
            }
        }
    }
}

Pairs Forming LCM

Description:
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 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 ≤ 1e14).

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

Problem solving:
这道题就是让你输出n以内的两个数最小公倍数为n的对数。我已开始以为是找规律,然后找了半个上午也没找到。还是百度大法好。

网上代码的思路就是也是用到了求两个数的lcm的一个公式。

注意还要用到素数打表。这里也是见到了一种新的打表方式,很强。直接筛的时候就存。

关于这道题的具体解法我也说不清,还有很复杂的证明啊什么的,可以参考一下这篇博客。
virgoDd

Code:

#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 < 2^63). 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.

Sample Input
3
10
5
12

Sample Output
Case 1: 5 2
Case 2: Impossible
Case 3: 3 4
Problem solving:
这道题还是很轻松的。意思就是给你一个数问你能不能把它分成一个奇数和一个偶数的乘积的形式。如果答案有多种输出奇数最小的形式。

偶数是很好找的,我们对这个数一直除以2直到除不尽的时候,剩下的那个数就是最小的奇数,因为此时分出来的偶数是最大的。如果这个数本来就是个奇数,就是输出’Impossible’就行了。

Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6;
ll a[maxn];
int main()
{
    int t;
    cin>>t;
    for(int i=1;i<=t;i++)
    {
        ll n,pos=0,a=1;
        cin>>n;
            printf("Case %d: ",i);
        if(n%2!=0)    puts("Impossible");
        else
        {
            while(n%2==0)
            {
                n/=2;
                a*=2;
            }
            cout<<n<<" "<<a<<endl;            
        }

    }
}

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

Output
For each case, print the case number and the number of ways to express N as sum of consecutive integers.

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
Problem solving:
这道题好像需要求得是质数因子的个数。
证明太麻烦,还是直接上链接啊。oliveQ

Code:

#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 ≤ 1e12) 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
Problem solving:
这道题的意思就是输入两个数a,b。问你a分解成两个数相乘的形式且每个数都不小于b的情况有多少种。

可以先求b的,然后减去a的。
我是真的说不清:莫若诩殇

Code:

#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

PDF题面: 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.
Sample Input
12
10
5
0
Sample Output
Case 1: 7
Case 2: 7
Case 3: 6
Problem solving:
这道题就是给你一个n,问你以n为最小公倍数的几个数的和的最小值。

答案就是n分解质因数之后的幂级数的和。

如何证明:Baoli1008

Code:

#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.
Sample Input
2
6 72
7 33
Sample Output
72
0
Problem solving:
这道题就是给你两个数a,b,问你以a为最大公约数并且以b为最小公倍数的有多少种情况。

Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll solve(ll n)
{
    ll ans=1;
    for(int i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            int t=0;
            while(n%i==0)
            {
                t++;
                n/=i;
            }
            ans*=t*6;
        }
    }
    if(n>1)    ans*=6;
    return ans;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll n,m;
        cin>>n>>m;
        if(m%n)
        {
            puts("0");
            continue;
        }
        ll t=m/n;
        cout<<solve(t)<<endl;
    }
}