当前位置: 首页 > article >正文

C - GCD(因子分解+欧拉函数)

传送阵:NEFU2022-Euler's totient function - Virtual Judge

思路:

枚举因子i(1,n),对于每个因子i(i>=m),求有多少个x(1,n)使得gcd(x,n)=i

gcd(x,n)=i <=> gcd(x/i,n/i)=1 <=> 求n/i的欧拉函数的值。

代码:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
#define per(i,a,b) for(int i=a;i<=b;i++)
#define ber(i,a,b) for(int i=a;i>=b;i--)
const int N = 1e5 + 3;
const long long mod = 10000000033;
LL n, m;
LL phi(LL x)
{
    if (x == 1)
        return 1;
    LL ans = x;
    for (int i = 2; i * i <= x; i++)
    {
        if (x % i == 0)
            ans = ans / i * (i - 1);
        while (x % i == 0)
            x /= i;
    }
    if (x > 1)
        ans = ans / x * (x - 1);
    return ans;
}
int main()
{
    ios::sync_with_stdio(false);
    int T;
    cin >> T;
    while (T--)
    {
        LL ans = 0;
        cin >> n >> m;
        if (m == 1)
        {
            cout << n << endl;
            continue;
        }
        for (int i = 1; i * i <= n; i++)
        {
            if (n % i == 0)
            {
                if (n / i >= m)  ans += phi(i);
                if (i >= m && i * i != n) ans += phi(n / i);//防止i*i==n时算两遍
            }
        }
        cout << ans << endl;
    }
    return 0;
}


http://www.kler.cn/a/153166.html

相关文章:

  • Android Studio新版UI介绍
  • 第四阶|自在行草 暄桐教室,林曦书法 从书法之美到生活之美
  • 计算机二级考试题库(答案)
  • [ZJCTF 2019]NiZhuanSiWei
  • 代码随想录算法训练营第三十六天 _ 贪心算法_435.无重叠区间、763.划分字母区间、56.合并区间。
  • 2023.11.26 关于 Spring Boot 单元测试
  • vscode注释插件「koroFileHeader」
  • 触控板窗口管理软件Swish mac中文版
  • Day62.算法训练
  • 利用Nginx与php处理方式不同绕过Nginx_host实现SQL注入
  • transformer没有BN层
  • GPT带我学Openpyxl操作Excel
  • LeetCode105.从前序和中序遍历序列构造二叉树
  • 脱掉白大褂后,还救死扶伤吗?——活在心安处
  • Javase | 多线程
  • 传统算法: Pygame 实现一个简单的二阶马尔可夫链的状态转移过程
  • Unity 使用Horizontal Layout Group和Toggle制作多个水平开关按钮实现自动排列和单个点击放大后的自动排列。
  • 酒店 KPI绩效考核指标及应用
  • 【开发PaaS】基于Postgresql的开发平台Supabase
  • 运算放大器和常见运放电路