约数的例题

点击这里^ - ^,有奇迹哟

869.试除法求约数

给定 n n n个正整数,对于每个整数 a i a_i ai,请你按照从小到大的顺序输出它的所有约数。
输入格式
第一行包含整数 n n n
接下来,每行包含一个整数 a i a_i ai
输出格式
输出共 n n n行,其中第 i i i行输出第 i i i个整数 a i a_i ai的所有约数。
数据范围 1 ≤ n ≤ 100 , 2 ≤ a i ≤ 2 × 1 0 9 1\leq n\leq100,2\leq a_i\leq 2\times10^9 1n100,2ai2×109

思路:直接遍历筛即可。

#include <iostream>
#include <algorithm>
#include <cstring> 
#include <cstdio>
#include <ctime>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <bitset>
#include <map>
#include <set>
using namespace std;
#define ICO std::ios::sync_with_stdio(false);std::cin.tie(0)
#define mm(a,x) memset(a,x,sizeof(a))
#define lowbit(x) (x & (-x))
#define endl '\n'
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll; 
typedef vector<int> vet;
typedef vector<ll> vell;
typedef queue<int> qut;
typedef queue<ll> qull;
const double eps = 1e-9;
const float pi = acos(-1.0);
const int inf_int = 0x3f3f3f3f;
const ll inf_ll = 0x7f7f7f7f7f7f7f7f;
const int mod = 1e9+7;
const int N = 2e5 + 10;
const int M = 1e6+10;
inline ll read() {
    ll x = 0, fl = 1; char c = getchar();
    while (c < '0' || c > '9') { if(c == '-') fl = -1; c = getchar(); }
    while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
    return x * fl;
}
inline ll gcd(ll a, ll b){return b == 0? a : gcd(b, a % b);}

int n;
ll a;

signed main(){
    IOS;
    cin >> n;
    while(n--){
        int t = 0;
        cin >> a;
        for(int i = 1; i <= sqrt(a); i++){
            if(a % i == 0) cout << i << " ";
        }
        int res = sqrt(a);
        if(res * res == a) t = res - 1;
        else t = res;
        for(int i = t; i >= 1; i--){
            if(a % i == 0) cout << a/i << " ";
        }
        cout << endl;
    }
    return 0;
}

872.最大公约数

给定 n n n对正整数 a i , b i a_i,b_i ai,bi,请你求出每对数的最大公约数
输入格式
第一行包含整数 n , 1 ≤ n ≤ 1 0 5 n,1\leq n \leq 10^5 n,1n105
接下来 n n n行,每行包含一个整数对 a i , b i , 1 ≤ a i , b i ≤ 2 × 1 0 9 a_i,b_i,1\leq a_i,b_i\leq2\times10^9 ai,bi,1ai,bi2×109,。
输出格式
输出 n n n行,每行输出一个整数对的最大公约数。

#include <iostream>
#include <algorithm>
#include <cstring> 
#include <cstdio>
#include <ctime>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <bitset>
#include <map>
#include <set>
using namespace std;
#define ICO std::ios::sync_with_stdio(false);std::cin.tie(0)
#define mm(a,x) memset(a,x,sizeof(a))
#define lowbit(x) (x & (-x))
#define endl '\n'
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll; 
typedef vector<int> vet;
typedef vector<ll> vell;
typedef queue<int> qut;
typedef queue<ll> qull;
const double eps = 1e-9;
const float pi = acos(-1.0);
const int inf_int = 0x3f3f3f3f;
const ll inf_ll = 0x7f7f7f7f7f7f7f7f;
const int mod = 1e9+7;
const int N = 2e5 + 10;
const int M = 1e6+10;
inline ll read() {
    ll x = 0, fl = 1; char c = getchar();
    while (c < '0' || c > '9') { if(c == '-') fl = -1; c = getchar(); }
    while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
    return x * fl;
}
inline ll gcd(ll a, ll b){return b == 0? a : gcd(b, a % b);}

int n, res;
ll a, b;

signed main(){
    cin >> n;
    while(n--){
        cin >> a >> b;
        cout << gcd(a,b) << endl;
    }
    return 0;
}

870.约数个数

给定 n n n的正整数 a i a_i ai,请你输出这些数的乘积的约数个数,答案对 1 0 9 + 7 10^9+7 109+7取模。
输入格式
第一行包含整数 n n n
接下来 n n n行,每行包含一个整数 a i a_i ai
输出格式
输出一个整数,表示所给正整数的乘积的约数个数,答案对 1 0 9 + 7 10^9+7 109+7取模。
数据范围 1 ≤ n ≤ 100 , 2 ≤ a i ≤ 2 × 1 0 9 1\leq n\leq100,2\leq a_i\leq 2\times10^9 1n100,2ai2×109

思路:对整数 a i a_i ai让其始终被其最小的素数整除,并记录这个素数的个数,(最好使用 m a p map map存储,方便)然后代入公式计算即可。

#include <iostream>
#include <algorithm>
#include <cstring> 
#include <cstdio>
#include <ctime>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <bitset>
#include <map>
#include <set>
using namespace std;
#define ICO std::ios::sync_with_stdio(false);std::cin.tie(0)
#define mm(a,x) memset(a,x,sizeof(a))
#define lowbit(x) (x & (-x))
#define endl '\n'
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll; 
typedef vector<int> vet;
typedef vector<ll> vell;
typedef queue<int> qut;
typedef queue<ll> qull;
const double eps = 1e-9;
const float pi = acos(-1.0);
const int inf_int = 0x3f3f3f3f;
const ll inf_ll = 0x7f7f7f7f7f7f7f7f;
const int mod = 1e9+7;
const int N = 2e5 + 10;
const int M = 1e6+10;
inline ll read() {
    ll x = 0, fl = 1; char c = getchar();
    while (c < '0' || c > '9') { if(c == '-') fl = -1; c = getchar(); }
    while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
    return x * fl;
}
inline ll gcd(ll a, ll b){return b == 0? a : gcd(b, a % b);}

signed main(){
    int n, x;
    ll res = 1;
    cin >> n;
    map<int,int>ma;
    map<int,int>::iterator iter;
    while(n--){
        cin >> x;
        for(int i = 2;  i <= x / i; i++){
            while(x % i == 0){
                x /= i;
                ma[i]++;
            }
        }
        if(x > 1) ma[x]++;
    }
    for(iter = ma.begin(); iter != ma.end(); iter++){
        res = res * (iter->second + 1) % mod;
    }
    cout << res;
    return 0;
}

871.约数之和

给定 n n n的正整数 a i a_i ai,请你输出这些数的乘积的约数之和,答案对 1 0 9 + 7 10^9+7 109+7取模。
输入格式
第一行包含整数 n n n
接下来 n n n行,每行包含一个整数 a i a_i ai
输出格式
输出一个整数,表示所给正整数的乘积的约数个数,答案对 1 0 9 + 7 10^9+7 109+7取模。
数据范围 1 ≤ n ≤ 100 , 2 ≤ a i ≤ 2 × 1 0 9 1\leq n\leq100,2\leq a_i\leq 2\times10^9 1n100,2ai2×109

思路:对整数 a i a_i ai让其始终被其最小的素数整除,并记录这个素数的个数,(最好使用 m a p map map存储,方便)然后代入公式计算即可。

#include <iostream>
#include <algorithm>
#include <cstring> 
#include <cstdio>
#include <ctime>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <bitset>
#include <map>
#include <set>
using namespace std;
#define ICO std::ios::sync_with_stdio(false);std::cin.tie(0)
#define mm(a,x) memset(a,x,sizeof(a))
#define lowbit(x) (x & (-x))
#define endl '\n'
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll; 
typedef vector<int> vet;
typedef vector<ll> vell;
typedef queue<int> qut;
typedef queue<ll> qull;
const double eps = 1e-9;
const float pi = acos(-1.0);
const int inf_int = 0x3f3f3f3f;
const ll inf_ll = 0x7f7f7f7f7f7f7f7f;
const int mod = 1e9+7;
const int N = 2e5 + 10;
const int M = 1e6+10;
inline ll read() {
    ll x = 0, fl = 1; char c = getchar();
    while (c < '0' || c > '9') { if(c == '-') fl = -1; c = getchar(); }
    while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
    return x * fl;
}
inline ll gcd(ll a, ll b){return b == 0? a : gcd(b, a % b);}

ll fastpow(int a, int n){
    ll res = 1;
    while(n){
        if(n & 1) res = ((res % mod) * (a % mod)) % mod;
        a = ((a % mod) * (a % mod)) % mod;
        n >>= 1;
    }
    return res % mod;
}

sigend main(){
    int n, x;
    ll res = 0, ans = 1;
    cin >> n;
    map<int,int>ma;
    map<int,int>::iterator iter;
    while(n--){
        cin >> x;
        for(int i = 2;  i <= x / i; i++){
            while(x % i == 0){
                x /= i;
                ma[i]++;
            }
        }
        if(x > 1) ma[x]++;
    }
    for(iter = ma.begin(); iter != ma.end(); iter++){
        res = 0;
        for(int i = 0; i <= iter->second; i++){
            res = ((res % mod) + (fastpow(iter->first,i) % mod)) % mod;
        }
        ans = ((ans % mod) * (res % mod) % mod);
    }
    cout << ans % mod;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星*湖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值