HDU 4910 Problem about GCD(米勒拉賓),hdu4910

來源:互聯網
上載者:User

HDU 4910 Problem about GCD(米勒拉賓),hdu4910
HDU 4910 Problem about GCD

題目連結

題意:給定一個數字,求出1 - n之間與他互質的數的乘積mod n

思路:看了網上別人找出來的規律,原文連結
然後由於這題的n很大,也沒法直接判定,可以這樣搞,先去試10^6以內的素數,判斷可不可以,如果不行,再利用米勒拉賓判下是否是素數,如果不是的話,把這個數字開根在平方,判斷是不是完全平方數,這樣做的原因是數字最大10^18,如果沒有10^6以內的質因子,又不是質數的話,那麼他最多隻能包含2個質因子了,那麼如果他不是一個完全平方數的話,那麼就肯定不是了

代碼:

#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>typedef long long ll;const int N = 1000005;ll n, prime[N];int vis[N], pn;ll mul(ll a, ll b, ll mod) {    ll ret = 0;    while (b > 0) {if (b&1) ret = (ret + a) % mod;b >>= 1;a = (a<<1) % mod;    }    return ret;}ll pow_mod(ll x, ll k, ll mod) {    ll ans = 1;    while (k) {if (k&1) ans = mul(ans, x, mod);x = mul(x, x, mod);k >>= 1;    }    return ans;}bool mlrb (ll n) {    if (n < 2) return false;    if (n == 2) return true;    if (n % 2 == 0) return false;    for (int i = 0; i < 20; i++) {ll a = rand() % (n - 1) + 1;if (pow_mod(a, n - 1, n) != 1)    return false;    }    return true;}void get_table() {    pn = 0;    for (ll i = 2; i < N; i++) {if (vis[i]) continue;prime[pn++] = i;for (ll j = i * i; j < N; j += i)    vis[j] = 1;    }}bool check(ll n) {    for (int i = 1; i < pn; i++) {if (n % prime[i] == 0) {    ll tmp = n;    while (tmp % prime[i] == 0)tmp /= prime[i];    if (tmp == 1) return true;    elsebreak;}    }    if (mlrb(n)) return true;    ll m = (ll)sqrt(n * 1.0);    if (m * m == n) return true;    return false;}bool judge(ll n) {    if (n == 1 || n == 2 || n == 4) return true;    if (n % 4 == 0) return false;    if (n % 2 && check(n)) return true;    if (n % 2 == 0 && check(n / 2)) return true;    return false;}int main() {    get_table();    while (~scanf("%I64d", &n) && n != -1) {if (judge(n)) printf("%I64d\n", n - 1);else printf("1\n");    }    return 0;}



(c++)n個數的最小公倍數,為何這個程式不對? http://acmhdueducn/showproblemphp?pid=2028

#include<iostream>
using namespace std;
int gcd(int a,int b)
{
if(a<b)
swap(a,b);
if(b==0)
return a;
else
return gcd(b,a%b);
}
int main()
{
int n;

while(cin>>n)
{
int a[100];
int t=0,T=1;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
if(n==1)
cout<<a[0]<<endl;
else
{
t=gcd(a[0],a[1]);
T=a[1]/t*a[0];
for(int j=2;j<n;j++)
{
t=gcd(a[j],T);
T=T/t*a[j];
}
cout<<T<<endl;
}
}
return 0;
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.