题目
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5478
题目来源:2015上海网络赛第三题
简要题意:字典升序输出满足 ak1n+b1+bk2n−k2+1≡0modC 的 a,b 组合
数据范围: 1⩽C⩽2×105;1⩽k1,k2,b1⩽1091⩽a,b<C
题解
一开始猜左右的余数都是不变的,然后WA了。
然后之后是猜 1,2 都成立就成立,然后就A了。
其实根本不知道为什么来的,但是就是过了。
实现
先把所有对 1 的结果映射到两个数组的
ZC 然后对 a 去找b 中的补。然后内部直接两个for循环去判断 2 <script type="math/tex" id="MathJax-Element-502">2</script>的时候是否满足方程。
对所有结果排序然后输出即可。
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 2E5+5;
vector<int> a[N];
vector<int> b[N];
vector<PII> res;
int c, k1, b1, k2, cas = 1;
LL getA(int x, int n) {
return powmod(x, 1LL*k1*n+b1, c);
}
LL getB(int x, int n) {
return powmod(x, 1LL*k2*n-k2+1, c);
}
int main()
{
while (scanf("%d%d%d%d", &c, &k1, &b1, &k2) == 4) {
for (int i = 1; i < c; i++) {
a[getA(i, 1)].pb(i);
}
for (int i = 1; i < c; i++) {
b[getB(i, 1)].pb(i);
}
printf("Case #%d:\n", cas++);
int op, lena, lenb;
for (int i = 0; i < c; i++) {
op = c-i;
lena = a[i].size(), lenb = b[op].size();
if (lena == 0 || lenb == 0) continue;
for (int j = 0; j < lena; j++) {
for (int k = 0; k < lenb; k++) {
if ((getA(a[i][j], 2)+getB(b[op][k], 2)) % c) continue;
res.pb(mp(a[i][j], b[op][k]));
}
}
}
sort(res.begin(), res.end());
int len = res.size();
for (int i = 0; i < len; i++) {
printf("%d %d\n", res[i].fi, res[i].se);
}
if (res.size() == 0) {
puts("-1");
}
for (int i = 0; i < c; i++) {
a[i].clear();
b[i].clear();
}
res.clear();
}
return 0;
}