Can you find it
Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 822 Accepted Submission(s): 365
Problem Description
Given a prime number
C(1≤C≤2×10
5
)
, and three integers k1, b1, k2
(1≤k1,k2,b1≤10
9
)
. Please find all pairs (a, b) which satisfied the equation
a
k1⋅n+b1![]()
+
b
k2⋅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.
Please output all pairs (a, b) in lexicographical order. (1≤a,b<C)
Sample Input
23 1 1 2
Sample Output
Case #1: 1 22
Source
题意:求出最小的a和对应的b满足题目所给式子。
分析:刚看到这题时也想到了快速幂,但一看数据范围,感觉肯定会超时,想了很久,未果,参考大牛博客,就是快速幂。
详解见这--某大牛博客
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
#define ll long long
#define CL(a) memset(a,0,sizeof(a))
ll C,k1,b1,k2;
ll a,b;
ll mod_pow(ll x, ll y, ll mod)
{
ll re=1;
while (y)
{
if (y&1) re=re*x%C;
x=x*x%C;
y>>=1;
}
return re;
}
int main ()
{
int cas=1;
while (cin>>C>>k1>>b1>>k2)
{
cout<<"Case #"<<cas++<<":"<<endl;
bool flag=false;
for (a=1; a<C; a++)
{
b = C-mod_pow(a, k1+b1, C);
if ((mod_pow(a,k1,C)*(C-b)%C+mod_pow(b,k2,C)*b%C)%C==0)
cout<<a<<" "<<b<<endl,flag=true;
}
if (!flag)
cout<<"-1"<<endl;
}
return 0;
}

本文探讨了给定特定模数条件下寻找满足特定方程的整数对(a, b)的问题。通过分析方程的形式与限制条件,采用快速幂算法进行有效计算。提供了一个C++实现示例,用于演示如何解决此类数学问题。
1万+

被折叠的 条评论
为什么被折叠?



