数论1

 分解质因数

给定 n 个正整数 ai,将每个数分解质因数,并按照质因数从小到大的顺序输出每个质因数的底数和指数。

输入格式
第一行包含整数 n。

接下来 n 行,每行包含一个正整数 ai。

输出格式
对于每个正整数 ai,按照从小到大的顺序输出其分解质因数后,每个质因数的底数和指数,每个底数和指数占一行。

每个正整数的质因数全部输出完毕后,输出一个空行。

数据范围
1≤n≤100,
1≤ai≤2×109
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

void zys(ll n){
    for(ll i=2;i<=n/i;++i){
        if(n%i==0){
            ll s=0;
            while(n%i==0){
                n/=i;
                s++;
            }
            cout<<i<<" "<<s<<endl;
        }
    }
    if(n>1) cout<<n<<" "<<"1"<<endl;
    puts("");
}

int main()
{
    ll t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        zys(n);
    }
    return 0;
}
868. 筛质数

给定一个正整数 n,请你求出 1∼n 中质数的个数。

输入格式
共一行,包含整数 n。

输出格式
共一行,包含一个整数,表示 1∼n 中质数的个数。

数据范围
1≤n≤106
输入样例:
8
输出样例:
4
//埃氏筛法
#include<bits/stdc++.h>

using namespace std;

const int maxn=1000010;
int primes[maxn],cnt;
bool st[maxn];

void get_primes(int n){
	for(int i=2;i<=n;++i){
		if(!st[i]){
			primes[cnt++]=n;
			for(int j=i+i;j<=n;j+=i) st[j]=true;
		}
	}
}

int main()
{
	int n;
	cin>>n;
	get_primes(n);
	cout<<cnt<<endl;
	return 0;
}
//线性筛
#include<bits/stdc++.h>

using namespace std;

const int maxn=1000010;
int primes[maxn],cnt;
bool st[maxn];

void get_primes(int n){
    for(int i=2;i<=n;++i){
        if(!st[i]) primes[cnt++]=i;
        for(int j=0;primes[j]<=n/i;++j){
            st[primes[j]*i]=true;
            if(i%primes[j]==0) break;
        }
    }
}

int main()
{
	int n;
	cin>>n;
	get_primes(n);
	cout<<cnt<<endl;
	return 0;
}
试除法求约数

给定 n 个正整数 ai,对于每个整数 ai,请你按照从小到大的顺序输出它的所有约数。

输入格式
第一行包含整数 n。

接下来 n 行,每行包含一个整数 ai。

输出格式
输出共 n 行,其中第 i 行输出第 i 个整数 ai 的所有约数。

数据范围
1≤n≤100,
2≤ai≤2×109
输入样例:
2
6
8
输出样例:
1 2 3 6 
1 2 4 8 
#include<bits/stdc++.h>

using namespace std;

vector<int> get_divisors(int n){
	vector<int> res;
	for(int i=1;i<=n/i;++i){
		if(n%i==0){
			res.push_back(i);
			if(i!=n/i) res.push_back(n/i);
		}
	}
	sort(res.begin(),res.end());
	return res;
}

int main()
{
	int t;
	cin>>t;
	while(t--){
		int x;
		cin>>x;
		auto res=get_divisiors(x);
		for(auto a:res) cout<<a<<" ";
		puts("");
	}
	return 0;
}
约数个数

给定 n 个正整数 ai,请你输出这些数的乘积的约数个数,答案对 109+7 取模。

输入格式
第一行包含整数 n。

接下来 n 行,每行包含一个整数 ai。

输出格式
输出一个整数,表示所给正整数的乘积的约数个数,答案需对 109+7 取模。

数据范围
1≤n≤100,
1≤ai≤2×109
输入样例:
3
2
6
8
输出样例:
12
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
ll mod=1e9+7;
int n;

int main()
{
	cin>>n;
	unordered_map<int,int>primes;
	while(n--){
		int x;
		cin>>x;
		for(int i=2;i<=x/i;++i){
			while(x%i==0){
				x/=i;
				primes[i]++;
			}
		}
		if(x>1) primes[x]++;
	}
	ll res=1;
	for(auto i: primes) res=res*(i.second+1)%mod;
	cout<<res<<endl;
	return 0;
}
约数之和

给定 n 个正整数 ai,请你输出这些数的乘积的约数之和,答案对 109+7 取模。

输入格式
第一行包含整数 n。

接下来 n 行,每行包含一个整数 ai。

输出格式
输出一个整数,表示所给正整数的乘积的约数之和,答案需对 109+7 取模。

数据范围
1≤n≤100,
1≤ai≤2×109
输入样例:
3
2
6
8
输出样例:
252
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
ll mod=1e9+7;
int n;

int main()
{
	cin>>n;
	unordered_map<int,int>primes;
	while(n--){
		int x;
		cin>>x;
		for(int i=2;i<=x/i;++i){
			while(x%i==0){
				x/=i;
				primes[i]++;
			}
		}
		if(x>1) primes[x]++;
	}
	ll res=1;
	for(auto i: primes){
		int p=i.first,a=i.second;
		ll t=1;
		while(a--) t=(t*p+1)%mod;
		res=res*t%mod;
	}
	cout<<res<<endl;
	return 0;
}
欧拉函数

给定 n 个正整数 ai,请你求出每个数的欧拉函数。

欧拉函数的定义
1∼N 中与 N 互质的数的个数被称为欧拉函数,记为 ϕ(N)。
若在算数基本定理中,N=pa11pa22…pamm,则:
ϕ(N) = N×p1−1p1×p2−1p2×…×pm−1pm
输入格式
第一行包含整数 n。

接下来 n 行,每行包含一个正整数 ai。

输出格式
输出共 n 行,每行输出一个正整数 ai 的欧拉函数。

数据范围
1≤n≤100,
1≤ai≤2×109
输入样例:
3
3
6
8
输出样例:
2
2
4
//公式法求欧拉函数
#include<bits/stdc++.h>

using namespace std;

int main()
{
	//n=p1^a1*p2^a2...pk^ak;
	//欧拉函数=n*(1-1/p1)(1-1/p2)(1-1/p3)...(1-1/pk)
	//容斥原理
	int n;
	cin>>n;
	while(n--){
		int a;
		cin>>a;
		int ans=a;
		for(int i=2;i<=a/i;++i){
			if(a%i==0){
				ans=ans/i*(i-1);
				while(a%i==0) a/=i;
			}
		}
		if(a>1) ans=ans/a*(a-1);
		cout<<ans<<endl;
	}
	return 0;
 } 
筛法求欧拉函数

给定一个正整数 n,求 1∼n 中每个数的欧拉函数之和。

输入格式
共一行,包含一个整数 n。

输出格式
共一行,包含一个整数,表示 1∼n 中每个数的欧拉函数之和。

数据范围
1≤n≤106
输入样例:
6
输出样例:
12
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int maxn=1000010;

int primes[maxn],cnt;
int phi[maxn];//欧拉函数
bool st[maxn]; 

ll get_eulers(int n){
	phi[1]=1;
	for(int i=2;i<=n;++i){
		if(!st[i]){
		    primes[cnt++]=i;
			phi[i]=i-1;	
		}
		for(int j=0;primes[j]<=n/i;++j){
			st[primes[j]*i]=true;
			if(i%primes[j]==0){
				phi[primes[j]*i]=phi[i]*primes[j];
				break;
			}
			phi[primes[j]*i]=phi[i]*(primes[j]-1);
		}
	}
	ll res=0;
	for(int i=1;i<=n;++i) res+=phi[i];
	return res;
}

int main()
{
	int n;
	cin>>n;
	cout<<get_eulers(n)<<endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值