首页 > 其他分享 >hdu:gcd(欧拉函数)

hdu:gcd(欧拉函数)

时间:2023-05-19 17:15:41浏览次数:43  
标签:hdu gcd int res phi ans 欧拉

Problem Description
The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the largest divisor common to a and b,For example,(1,2)=1,(12,18)=6.
(a,b) can be easily found by the Euclidean algorithm. Now Carp is considering a little more difficult problem:
Given integers N and M, how many integer X satisfies 1<=X<=N and (X,N)>=M.

Input
The first line of input is an integer T(T<=100) representing the number of test cases. The following T lines each contains two numbers N and M (2<=N<=1000000000, 1<=M<=N), representing a test case.

Output
For each test case,output the answer on a single line.

Sample input
3
1 1
10 2
10000 72

Sample output
1
6
260

数学-欧拉函数

设X=a\(\times\)m,N=b\(\times\)m,则有gcd(a,b)=1,又a<=b故结果为phi(b),只需枚举合适的m即可

点击查看代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int phi(int x)
{
	int res=x;
	for(int i=2;i*i<=x;++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;
}
void solve()
{
	int n,m;
	cin>>n>>m;
	int ans=0;
	for(int i=1;i*i<=n;++i)
	{
		if(n%i==0)
		{
			if(i>=m) ans+=phi(n/i);
			if(i*i!=n&&n>=(ll)i*m) ans+=phi(i);
		}
		//cout<<i<<' '<<ans<<"\n";
	}
	cout<<ans<<'\n';
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin>>T;
    while(T--)
    {
    	solve();
    }
    return 0;
}

标签:hdu,gcd,int,res,phi,ans,欧拉
From: https://www.cnblogs.com/ruoye123456/p/17415769.html

相关文章

  • The Euler function(欧拉函数)
    ProblemDescriptionTheEulerfunctionphiisanimportantkindoffunctioninnumbertheory,(n)representstheamountofthenumberswhicharesmallerthannandcoprimeton,andthisfunctionhasalotofbeautifulcharacteristics.Herecomesaverye......
  • 初等数论——素数,逆元,EXGCD有关
    初等数论素数定义设整数\(p\ne0,\pm1\)。如果\(p\)除了平凡约数以外没有其他约数,那么称\(p\)为素数(不可约数)。若整数\(a\ne0,\pm1\)且\(a\)不是素数,则称\(a\)为合数。——————OIWiki素数的判定(素性测试)如何判断一个数\(x\)是不是质数?很显然我们可......
  • 2654. 使数组所有元素变成 1 的最少操作次数(c++,gcd性质)
    题目链接:2654.使数组所有元素变成1的最少操作次数方法一:计算最短的gcd为1的子数组解题思路本题目标:使得所有的数组元素都变为\(1\),通过求相邻元素\(gcd\)将其赋值给一方的方式;思路:若想操作数最少,那么就是不为\(1\)的数\(x\)和1求\(gcd\),即\(x=gcd(x,1)\),......
  • CF1750D Count GCD
    Problem多组数据。每组数据给定两个整数\(n\),\(m\)和一个数列\(b\),问有多少种方案构造一个长度为\(n\)的序列\(a\),满足\(1\lea_i\lem\)求\(gcd(a_1,a_2,\cdots,a_i)=b_i\),答案对\(998244353\)取模。Input第一行输入\(t\),表示数据组数。每组数据第一行是两......
  • hdu:LCIS(线段树+区间合并)
    ProblemDescriptionGivennintegers.Youhavetwooperations:UAB:replacetheAthnumberbyB.(indexcountingfrom0)QAB:outputthelengthofthelongestconsecutiveincreasingsubsequence(LCIS)in[a,b].InputTinthefirstline,indicatingt......
  • hdu:这是真正的水题(RMQ)
    ProblemDescription在缺水的地方,水是非常有限的资源,所以人们常常为争夺最大的水源而战。给定一系列水源,用a1,a2,a3,…,an代表水源的大小。给定一组查询,每个查询包含2整数L和R,请找出L和R之间最大的水源。Input输入数据首先给定一个整数T(T≤10)表示测试用例的数量。......
  • hdu:Party(2-SAT)
    ProblemDescription有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以列席。在2n个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛盾的2个人是不会同时出现在聚会上的。有没有可能会有n个人同时列席?Inputn:表示有n对夫妻被邀请(n<=1000)m:表......
  • hdu:Let's go home(2-SAT)
    ProblemDescription小时候,乡愁是一枚小小的邮票,我在这头,母亲在那头。——余光中集训是辛苦的,道路是坎坷的,休息还是必须的。经过一段时间的训练,lcy决定让大家回家放松一下,但是训练还是得照常进行,lcy想出了如下回家规定,每一个队(三人一队)或者队长留下或者其余两名队员同时留下;每......
  • hdu:How far away ?(树链剖分)
    ProblemDescriptionTherearenhousesinthevillageandsomebidirectionalroadsconnectingthem.Everydaypeolealwaysliketoasklikethis“HowfarisitifIwanttogofromhouseAtohouseB”?Usuallyithardtoanswer.Butluckilyintthisvilla......
  • hdu:Arbitrage(最短路变形)
    ProblemDescriptionArbitrageistheuseofdiscrepanciesincurrencyexchangeratestotransformoneunitofacurrencyintomorethanoneunitofthesamecurrency.Forexample,supposethat1USDollarbuys0.5Britishpound,1Britishpoundbuys10.0......