I won't tell you this is about number theory (结论题)

I won’t tell you this is about number theory

 To think of a beautiful problem description is so hard for me that let's just drop them off. :)
Given four integers a,m,n,k,and S = gcd(a^m-1,a^n-1)%k,calculate the S.

Input
The first line contain a t,then t cases followed.
Each case contain four integers a,m,n,k(1<=a,m,n,k<=10000).
Output
One line with a integer S.
Sample Input

1
1 1 1 1

Sample Output

0
题意:

求S

S=gcd(am1,an1) S = g c d ( a m − 1 , a n − 1 )

分析:

有结论fuck!

a>b,gcd(a,b)=1 a > b , g c d ( a , b ) = 1 gcd(ambm,anbn)=agcd(m,n)bgcd(m,n) g c d ( a m − b m , a n − b n ) = a g c d ( m , n ) − b g c d ( m , n )

特别的有 a>1,m,n>0 a > 1 , m , n > 0 ,则 gcd(am1,an1)=agcd(m,n)1 g c d ( a m − 1 , a n − 1 ) = a g c d ( m , n ) − 1

code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){
    return b ? gcd(b,a%b) : a;
}
ll q_pow(ll a,ll b,ll mod){
    ll ans = 1;
    while(b){
        if(b & 1)
            ans = ans * a % mod;
        b >>= 1;
        a = a * a % mod;
    }
    return ans;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        ll a,m,n,k;
        scanf("%lld%lld%lld%lld",&a,&m,&n,&k);
        ll g = gcd(m,n);
        printf("%lld\n",(q_pow(a,g,k) - 1 + k) % k);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值