数论练习题

1.Divisor SummationSPOJ - DIVSUM

题意:求n的所有因子和    (O(N*logN))调和级别

#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define eps 1e-8
#define zero(x) (((x) > 0 ? (x) : -(x)) < eps)
using namespace std;
const int maxn = 1e6 + 10;
int n, m,d[maxn];

signed main()
{
    // freopen(".in", "r", stdin);
    // freopen(".out", "w", stdout);
    //  ios::sync_with_stdio(false);
    for (int i = 1; i <= 500000; i++)
    {
        for (int j = 2 * i; j <= 500000; j += i)
        {
            d[j] += i;
        }
    }
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        cout << d[n] << endl;
    }
    return 0;
}

2.GCD LCM

UVA - 11388

题意:给出LCM与GCD,求最小符合的数a,b

 由GCD(a,b) | LCM(a,b)

#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define eps 1e-8
#define zero(x) (((x) > 0 ? (x) : -(x)) < eps)
using namespace std;
const int maxn = 1e6 + 10;
int n, m;

signed main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int a, b;
        cin >> a >> b;
        if (b % a != 0)
            cout << -1 << endl;
        else
            cout << a << ' ' << b << endl;
    }
    return 0;
}

3.Modified GCD

CodeForces - 75C

题意:给出a,b,给出区间l,r,求[l,r]中a,b的最大公约数

求出gcd(a,b)的所有因子,然后二分查找即可

#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
using namespace std;
const int maxn = 1e6 + 10;
vector<int> v;

signed main()
{
    int n, m;
    cin >> n >> m;
    int g = __gcd(n, m);
    for (int i = 1; i * i <= g; i++)
    {
        if (g % i == 0)
        {
            v.pb(i);
            if (i * i != g)
                v.pb(g / i);
        }
    }
    sort(all(v));
    int q;
    cin >> q;
    while (q--)
    {
        int l, r;
        cin >> l >> r;
        auto ans = upper_bound(v.begin(), v.end(), r) - 1;
        if ((*ans) >= l && (*ans) <= r)
            cout << *ans << endl;
        else
            cout << -1 << endl;
    }
    return 0;
}

4.Chef and KeyboardCodeChef - CHEFKEY 

题意:给出n,m,c。找出满足a<n,b<m,a*b==c的(a,b)对数

枚举c的所有因子

#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define eps 1e-8
#define zero(x) (((x) > 0 ? (x) : -(x)) < eps)
using namespace std;
const int maxn = 1e6 + 10;
signed main()
{
    // freopen(".in", "r", stdin);
    // freopen(".out", "w", stdout);
    //  ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--)
    {
        int n, m, c;
        cin >> n >> m >> c;
        int ans = 0;
        for (int i = 1; i < sqrt(c); i++)
        {
            if (c % i == 0 && i <= n && c / i <= m)
                ans++;
        }
        for (int i = 1; i < sqrt(c); i++)
        {
            if (c % i == 0 && i <= m && c / i <= n)
            {
                ans++;
            }
        }
        if ((int)sqrt(c) * (int)sqrt(c) == c && (int)sqrt(c) <= n && (int)sqrt(c) <= m)
        {
            ans++;
        }
        cout << ans << endl;
    }
    return 0;
}

5.LCM CardinalityUVA - 10892 

#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define eps 1e-8
#define zero(x) (((x) > 0 ? (x) : -(x)) < eps)
using namespace std;
const int maxn = 1e6 + 10;
int n, m;

signed main()
{
    while (cin >> n)
    {
        if (n == 0)
            return 0;
        cout << n << " ";
        int ans = 1;
        for (int i = 2; i <= n; i++)
        {
            int cnt = 0;
            while (n % i == 0)
            {
                cnt++;
                n /= i;
            }
            if (cnt)
            {
                ans *= (cnt * 2 + 1);
            }
        }
        if (n > 1)
            ans *= 3;
        cout << (ans + 1) / 2 << endl;
    }
    return 0;
}

6.H(n)

 UVA - 11526 

整除分块

#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define eps 1e-8
#define zero(x) (((x) > 0 ? (x) : -(x)) < eps)
using namespace std;
const int maxn = 1e6 + 10;
int n, m;

signed main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        int ans = 0;
        for (int l = 1, r; l <= n; l = r + 1)
        {
            r = n / (n / l);
            ans += n / l * (r - l + 1);
        }
        cout << ans << endl;
    }
    return 0;
}

7.Calculation 2

HDU - 3501

题意 :求小于n且与n不互质的数的和

首先欧拉函数Euler(n)是求小于n且与n互质的数的个数,再有gcd的性质:如果gcd(n,i)=1,则gcd(n,n-i)=1

那么,可以看做在[1,n-1]中与n互质的数是成对出现的,即如果i与n互质,则(n-i)也与n互质(Euler(n)为偶数)。而且可以发现这对数(i与n-i)的和为n。

进一步得到结论:小于n且与n互质的数的和为n*Euler(n)/2;

那么对于此题课先求1~n的和,再减去n*Euler(n)/2

原创:

hdu - 3501 - Calculation 2-(欧拉函数求互质数的和)_菜圾的博客-CSDN博客

#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define eps 1e-8
#define zero(x) (((x) > 0 ? (x) : -(x)) < eps)
using namespace std;
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;

int st[maxn], pri[maxn], phi[maxn], cnt;
int a[maxn], b[maxn];

void init()
{
    for (int i = 2; i < maxn; i++)
    {
        if (!st[i])
        {
            pri[cnt++] = i;
            phi[i] = i - 1;
        }
        for (int j = 0; pri[j] * i < maxn; j++)
        {
            st[pri[j] * i] = 1;
            if (i % pri[j] == 0)
            {
                phi[i * pri[j]] = phi[i] * pri[j];
                break;
            }
            phi[i * pri[j]] = phi[i] * (pri[j] - 1);
        }
    }
}

int eular(int x)
{
    int res = x;
    for (int i = 2; i <= x / i; i++)
        if (x % i == 0)
        {
            res = res / i * (i - 1);
            while (x % i == 0)
                x /= i;
        }
    if (x > 1)
        res = res / x * (x - 1);

    return res;
}

signed main()
{
    int n;
    while (cin >> n)
    {
        if (n == 0)
            break;
        int x = eular(n);
        int ans = (n * (n - 1) / 2) % mod;
        ans = (ans - (n * eular(n) / 2) % mod + mod) % mod;
        cout << ans << endl;
    }
    system("pause");
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
由于题目没有具体说明是哪个作业的练习题,因此无法提供准确的答案。数论是关于整数性质与结构的研究,涉及到整数的基本性质,因此答案需要根据具体的题目来给出。以下是一些数论导引练习题的一般解答方法: 1. 证明素数无穷多个: 答案:假设素数只有有限个,标记为p1, p2, ..., pn。然后构造一个新的数q,q = p1p2...pn +1。由于1不是素数,所以q一定是一个素数。这样我们得到了比已知的所有素数都大的素数q,与假设矛盾,因此素数无穷多个。 2.证明方程x^2 + y^2 = z^2 在正整数解中有无穷多个: 答案:首先,我们可以构造一个简单的解(x,y,z) = (3,4,5)。然后考虑将这个解乘以一个正整数k得到新的解(x',y',z')=(3k,4k,5k)。由于k是任意的正整数,所以可以构造出无穷多个解。因此,方程在正整数解中有无穷多个。 3.证明质数的乘积加一不是素数: 答案:假设质数的乘积加一是一个素数,标记为p。然后考虑将p减去1,得到p-1。根据欧拉定理,如果p是一个质数,那么p-1一定能被p的某一个质因数整除。但由于p-1是p的倍数,所以p也能整除p-1,这与p是一个素数矛盾。因此,质数的乘积加一不是素数。 总之,数论是一个广泛而深入的领域,需要具体问题具体分析,根据题目中给出的具体条件进行推导和证明。以上是一些常见的解答方法,但无法确定具体的题目,所以答案可能不是完整的或不适用于特定的练习题

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值