寒假算法训练之欧拉,欧几里德(2)

本文介绍了两个与欧拉算法相关的数论问题:HDU-2588 GCD问题,要求计算在一定范围内与N互质且大于等于M的整数个数;以及HDU-5584 LCM-WALK问题,涉及寻找能通过特定跳跃方式到达指定终点的起点数量。通过对欧拉函数的理解,解析了求解这两个问题的关键思路。
摘要由CSDN通过智能技术生成

hdu -2588 GCD

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

思路解析:
依照题目就可以知道这是一道关于欧拉函数的题目,欧拉函数是求解小于等于n且与n互质的数的个数。
如果k是n的约数,那么满足gcd(i,n)==p的i的个数是Φ(n/k)

#include<iostream>
using namespace std;
typedef long long ll;
ll N,M,T;
ll phi(ll n){
	ll ans = n;
	for (ll i = 2; i * i <= n; i++)
		if (n % i == 0){
			ans = ans / i * (i - 1);
			do
				n /= i;
			while (n % i == 0);
		}
	if (n > 1)
		ans = ans / n * (n - 1);
	return ans;
}

int main(){
	cin >> T;
	while (T--){
		cin >> N >> M;
		ll res = 0;		
		for (ll i = 1; i * i <= N; i++)	
			if (N % i == 0){
				if (i >= M)
					res += phi(N / i);
				if (i * i != N && N / i >= M)
					res += phi(i);						
			}		
		cout << res << endl;
	}
	return 0;
}

hdu-5584 LCM-WALK

Problem Description
A frog has just learned some number theory, and can’t wait to show his ability to his girlfriend.

Now the frog is sitting on a grid map of infinite rows and columns. Rows are numbered 1,2,⋯ from the bottom, so are the columns. At first the frog is sitting at grid (sx,sy), and begins his journey.

To show his girlfriend his talents in math, he uses a special way of jump. If currently the frog is at the grid (x,y), first of all, he will find the minimum z that can be divided by both x and y, and jump exactly z steps to the up, or to the right. So the next possible grid will be (x+z,y), or (x,y+z).

After a finite number of steps (perhaps zero), he finally finishes at grid (ex,ey). However, he is too tired and he forgets the position of his starting grid!

It will be too stupid to check each grid one by one, so please tell the frog the number of possible starting grids that can reach (ex,ey)!

Input
First line contains an integer T, which indicates the number of test cases.

Every test case contains two integers ex and ey, which is the destination grid.

⋅ 1≤T≤1000.
⋅ 1≤ex,ey≤109.

Output
For every test case, you should output “Case #x: y”, where x indicates the case number and counts from 1 and y is the number of possible starting grids.

Sample Input
3
6 10
6 8
2 8

Sample Output
Case #1: 1
Case #2: 2
Case #3: 3

思路解析:
题目要求解可到达该终点的起点个数。
假设终点为(x,y),当x>y时,说明前面那个点到该点的z是加在了x上。
所以我们直接选取x>y的情况,如果x<y那么就交换。对每一步的坐标进行除以k,得到上一步的x。直到x对y+k求余不等于0时说明该点为起点

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int gcd(int a,int b){
	return !b ? a : gcd(b,a % b);
}
int main(){
    int T;
    cin >> T;
    for(int i=1;i<=T;i++){
    	int x,y;
        cin >> x >> y;
        if(x<y) 
			swap(x,y);
        int k=gcd(x,y),count=1;
        while (x % (y + k) == 0){
            count++;
            x=x/(y/k+1);
            if(x<y) 
				swap(x,y);
        }
        printf("Case #%d: %d\n",i,count);
    }
    return 0;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值