HDU 5478 2015 ACM/ICPC 上海赛区网络赛1011 模运算+快速幂

Can you find it
Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 584 Accepted Submission(s): 271

Problem Description
Given a prime number C(1≤C≤2×105), and three integers k1, b1, k2 (1≤k1,k2,b1≤109). Please find all pairs (a, b) which satisfied the equation ak1⋅n+b1 + bk2⋅n−k2+1 = 0 (mod C)(n = 1, 2, 3, …).

Input
There are multiple test cases (no more than 30). For each test, a single line contains four integers C, k1, b1, k2.

Output
First, please output “Case #k: “, k is the number of test case. See sample output for more detail.
Please output all pairs (a, b) in lexicographical order. (1≤a,b < C). If there is not a pair (a, b), please output -1.

Sample Input
23 1 1 2

Sample Output
Case #1:
1 22

算法导论上说可以把模运算非正式地与通常的整数运算一样看待,如果仅限于加法,减法,乘法运算。则用这样的非正式模型就足够了
可以理解为在所有数均mod一个特定数n的情况下,可以进行加减乘运算。
a^(k1*n+b1) + b^(k2*n-k2+1) = 0

a^(k1*n+b1) = -b^(k2*n-k2+1)

a^b1/b^(1-k2) = -(b^k2/a^k1)^n ……………..1

0 = C*(b^k2/a^k1)^n………………………………….2
1+2
得:
a^b1/b^(1-k2) = (C-1)*(b^k2/a^k1)^n

a^b1*b^(k2-1)/(C-1) = (b^k2/a^k1)^n
可知,只有当b^k2 = a^k1,a^b1*b^(k2-1) = (C-1)时(即b^k2/a^k1 = 1)才能满足题意

又当n = 1时,a^(k1+b1) + b = 0
即 a^(k1+b1) + b = C
b = C - a^(k1+b1)
可知b和a是线性的一一对应关系,至此,枚举a,根据公式算出b,判断a,b是否满足条件,满足则输出。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <bitset>
#include <cmath>

using namespace std;
typedef long long LL;

LL fun(LL a,LL b,LL c){
    LL ans = 1;
    while(b){
        if(b&1)ans = ans*a%c;
        a= a*a%c;
        b>>=1;
    }
    return ans;
}

int main(){
    int CNT = 1;
    LL C,k1,b1,k2;
    while(scanf("%lld%lld%lld%lld",&C,&k1,&b1,&k2)!=EOF){
        printf("Case #%d:\n",CNT++);
        int a,b;
        bool p = false;
        for(a = 1;a<C;a++){
            b = C - fun(a,k1+b1,C);
            if(C-1 == fun(a,b1,C)*fun(b,k2-1,C)%C && fun(b,k2,C) == fun(a,k1,C)){
                p = true;
                printf("%d %d\n",a,b);
            }
        }
        if(!p) printf("-1\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值