( 数论专题 )【 唯一分解定理 】

( 数论专题 )【 唯一分解定理 】

唯一分解定理

一个数n肯定能被分解成  n = p1^{a1}*p2^{a2}*...*pn^{an}. 因为一个数肯定是由合数和质数构成的,合数又可以分解成质数和合数,最后递归下去就会变成质数的乘积。比如36 -> 2*2*3*3 -> 2^{2}*3^{2} .

最后化成了质数相乘的形式。

(2)中运用等比数列求和公式可化简为:

好,现在给出唯一分解定理的两个小应用

1. 求出数n的因子个数

n = p1^{a1}*p2^{a2}*...*pn^{an}有一个很简洁的公式: n的因子个数 = (1+a1​)(1+a2​)...(1+an​)

因此,我们可以用质因子分解的方法求出一个数的因子个数。

质因子分解

顾名思义,先要找出待分解整数n内的所有质数。然后不断扫描可整除n的质数,发现可整除的就不断除之。直到这个数为1为止。

找到所有质数的方法简单,就是埃拉托斯特尼筛法。见代码:

#include <bits/stdc++.h>
 
using namespace std;
 
const int maxn = 1e6+10;
typedef long long ll;
int primes[maxn],cnt;
bool isprime[maxn];
 
void get_prime() // 得到素数表
{
    for ( int i=0; i<=maxn; i++ ) isprime[i]=1;
    cnt = 0;
    isprime[0] = isprime[1] = 0;
    int i;
    for ( i=2; i*i<=maxn; i++ ) {
        if ( isprime[i]==1 ) {
            primes[cnt++] = i;
            for ( int j=i*i; j<=maxn; j+=i ) {
                isprime[j] = 0;
            }
        }
    }
    for ( ;i<=maxn; i+=2 ) {
        if ( isprime[i]==1 ) primes[cnt++] = i;
    }
}

然后,不断扫描这些质数,找到整除的就不断除。同时用公式求出因子个数

int solve( ll x ) // 返回x的因子个数
{
    int ans = 1;
    for ( int i=0; primes[i]<=sqrt(x)&&i<cnt; i++ ) { // 优化2
        int tot = 0;  // 这个primes[i]<=sqrt(x)必须优化, 如果是x>0直接T飞
        while ( x%primes[i]==0 ) {
            x /= primes[i];
            tot ++;
        }
        ans *= (tot+1);
    }
    if ( x>1 ) ans*=(1+1); // //如果x不能被整分,说明还有一个素数是它的约数,此时tot=1
    return ans;
}

 


 

例题1   Aladdin and the Flying Carpet   LightOJ - 1341

题意:一个长方形, 给出面积a , 限制其最短的边长必须大于等于b , 求有几种组合方式。

思路:根据唯一分解定理,先将a唯一分解,则a的所有正约数的个数为num = (1 + a1) * (1 + a2) *...(1 + ai),这里的ai是素因子的指数,见唯一分解定理,因为题目说了不会存在c==d的情况(是长方形),因此num要除2,去掉重复情况,然后枚举小于b的a的约数,拿num减掉就可以了

优化1:上面说了要枚举小于b又是a的约数的,那么b是1e12的肯定会超时, 这里需要提前判断 if ( a<b*b ) cout<<"0"<<endl; 意思是如果连长方形的边都是最小边都大于面积的话,一定是0. 这样b的范围就变成1e6了。

优化2:在质因子分解的时候会有一个边界条件,朴素的是 只要 x>0 我就让while继续,直到能除以一个素数就除。 可以优化成如果当前素数的平方大于x就结束,(这样可以从n的复杂度优化成根号n), 因为我们是从小忘大找素因子, 那么剩下的x一定可以分解成两个素数的乘积,如果连两个当前最小的乘积都无法分解成自然结束了, 这里在结束时需要判断,如果x>1,就是虽然x无法分解成2个数的乘积了; 但是还可以分解成1个数,所以需要注意一下。
代码:

#include <bits/stdc++.h>
 
using namespace std;
 
const int maxn = 1e6+10;
typedef long long ll;
int primes[maxn],cnt;
bool isprime[maxn];
 
void get_prime() // 得到素数表
{
    for ( int i=0; i<=maxn; i++ ) isprime[i]=1;
    cnt = 0;
    isprime[0] = isprime[1] = 0;
    int i;
    for ( i=2; i*i<=maxn; i++ ) {
        if ( isprime[i]==1 ) {
            primes[cnt++] = i;
            for ( int j=i*i; j<=maxn; j+=i ) {
                isprime[j] = 0;
            }
        }
    }
    for ( ;i<=maxn; i+=2 ) {
        if ( isprime[i]==1 ) primes[cnt++] = i;
    }
}
 
 
int solve( ll x ) // 返回x的因子个数
{
    int ans = 1;
    for ( int i=0; primes[i]<=sqrt(x)&&i<cnt; i++ ) { // 优化2
        int tot = 0;  // 这个primes[i]<=sqrt(x)必须优化, 如果是x>0直接T飞
        while ( x%primes[i]==0 ) {
            x /= primes[i];
            tot ++;
        }
        ans *= (tot+1);
    }
    if ( x>1 ) ans*=(1+1); // //如果x不能被整分,说明还有一个素数是它的约数,此时tot=1
    return ans;
}
 
int main()
{
    get_prime();
    ll listt,n,m,ji=1;
    cin >> listt;
    while ( listt-- ) {
        scanf("%lld %lld",&n, &m);
        if ( n<m*m ) {  // 这个条件必须加, 直接把下面m的for循环从1e12 变成了1e6
            printf("Case %lld: 0\n",ji++); // 优化1
        }
        else {
            int ans = solve(n);
            ans /= 2;
            for ( int i=1; i<m; i++ ) {
                if ( n%i==0 ) ans --;
            }
            printf("Case %lld: %lld\n",ji++,ans);
        }
    }
 
    return 0;
}

 

例题2   Sigma Function   LightOJ - 1336 

Discription

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

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因子和为偶数的数的个数。

思路:

数x的因子和 f(x)= (1+p1+p1^2+p1^3+...+p1^a1)*(1+p2+p2^2+...+p2^a2)*...*(1+pn+pn^2+...+pn^an);

因为偶数乘偶数还是偶数,奇数乘奇数还是奇数,奇数乘偶数是偶数,所有必须让每个括号内都是奇数,然后减去约数和为奇数的个数就是答案了。

1.当x有素因子2的时候,2所对应的括号内的和肯定是一个奇数,因为偶数加1一定是奇数。

2.除了2以外,所有的素数都是奇数,要使x得其他素因子对应的括号内的和为奇数,就必须保证x有偶数个该素因子,即ai必须为偶数。

3.满足上面两个条件的数,就是一个平方数,也就是说约数和为奇数的数x,它必定是一个平方数,当然这个平方数x乘上2也是满足2*x的约数和为奇数的

所以只要减去用n减去sqrt(n)和sqrt(n/2)就是答案了。也可以找n以内的平方数的个数,以及2*平方数不超过n的数的所有个数和,用n减完之后就是答案。

可以这样理解, 对于一个平方数x,先开方得到y, 根据唯一分解定理,y这个数一定可以化成y = p1^{a1}*p2^{a2}*...*pn^{an}的形式,这样再平方的话,所有指数乘二那么指数就全是偶数,这样x就符合2这个条件了, 再根据1这个条件括号里可以是2, 那么答案就是:平方数和平方数*2 的数因子和为奇数。

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long ji=1,i,n,listt;
    cin >> listt;
    while ( listt-- ) {
        cin >> n;
        long long ans = n - (int)sqrt(n) - (int)sqrt(n/2);
        cout << "Case " << ji++ << ": ";
        cout << ans << endl;
    }

    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值