Problem Description
小Y最喜欢数学了,他有很多关于数学的问题想问你。
作为一个ACMer,你为了能尽快解决问题,当然是编写一个程序来满足小Y的要求。
小Y的问题大概有以下几类:
给定若干个数,求他们的最大公约数。
给定两个数a,b,求第一个数a关于第二个数b的逆元,逆元定义为,ax%b=1的最小正整数x。
给定一个整数a,求不超过这个数的所有数中(1~a-1),与这个数互质的数的个数。
请你来帮帮小Y吧。
Input
多组数据,每组数据首先是一个整数p(=1,2,3)表示小Y问题的种类。
若p=1,则接下来是一个整数n,表示要求的数的个数(2<=n<=100),然后是n个数ai。
若p=2,则接下来是两个整数a,b(1<=a,b<=10^9)(保证a,b互质)
若p=3,则接下来是一个整数a(1<=a<=10^9)
Output
对于小Y的每一个问题,请分别作出回答。
Sample Input
1 2 56 78
2 2 5
3 8
1 3 10 100 125
2 14 83
3 100
Sample Output
2
3
4
5
6
40
一个GCD,一个求逆元,一个欧拉函数,恩,直接搞。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
ll gcd(ll a,ll b)
{
return b == 0?a:gcd(b,a%b);
}
ll euler_phi(ll n)
{
ll m = sqrt(n + 0.5);
ll ans = n;
for(ll i = 2;i <= m;i++)
if(n%i == 0)
{
ans = ans/i*(i - 1);
while(n%i == 0)n /= i;
}
if(n > 1)ans = ans/n*(n - 1);
return ans;
}
void GCD(ll a,ll b,ll& d,ll& x,ll& y)
{
if(b == 0){x = 1;y = 0;d = a;}
else{GCD(b,a%b,d,y,x);y -= x*(a/b);}
}
int main()
{
#ifdef LOCAL
freopen("C:\\Users\\ΡΡ\\Desktop\\in.txt","r",stdin);
//freopen("C:\\Users\\ΡΡ\\Desktop\\out.txt","w",stdout);
#endif // LOCAL
int p;
while(scanf("%d",&p) != EOF)
{
if(p == 1)
{
int n;
scanf("%d",&n);
ll pre1,pre2;
scanf("%lld%lld",&pre1,&pre2);
pre1 = gcd(pre1,pre2);
for(int i = 3;i <= n;i++)
{
scanf("%lld",&pre2);
pre1 = gcd(pre1,pre2);
}
printf("%lld\n",pre1);
}
else if(p == 2)
{
ll a,b;
scanf("%lld%lld",&a,&b);
ll d,x,y;
GCD(a,b,d,x,y);
if(x < 0)x += b;
printf("%lld\n",x);
}
else if(p == 3)
{
ll a;
scanf("%lld",&a);
printf("%lld\n",euler_phi(a));
}
}
return 0;
}