计蒜客2019蓝桥杯国赛B组模拟赛题解汇总:
https://blog.csdn.net/daixinliangwyx/article/details/90231587
第三题
标题:游戏
解法:肯定是不能暴力一轮一轮去算的,观察一下c、d的两个式子,会发现c+d=2*a+b-a=a+b,假设他们的和为mod,那么c和d的值无论经过多少轮,都是不会超过mod的,由于c+d=mod恒成立,因此c=c%mod,c经过一轮后c=2*a,又可以表示为c=(2*a)%mod,经过k轮,应该是会变成c=a*2^k的,可以输出几轮的值来看,会发现每一次c的值其实都是上一次乘了个2,也就证明经历k代后,c的值为c=(2^k * a)%mod;再考虑到上面的公式c+d=mod,因此d又可以表示为d=mod-c;然后再比较一下c和d的大小就可以得到k轮后a的值了。
代码:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static InputReader in = new InputReader(new BufferedInputStream(System.in));
public static PrintWriter out = new PrintWriter(System.out);
public static BigInteger a, b, c, d, mod, k;
public static void main(String[] args) {
a = new BigInteger("482333897982347239");
b = new BigInteger("557432748293424892");
k = new BigInteger("1389472389742429877");
mod = a.add(b);
c = a.multiply(BigInteger.valueOf(2).modPow(k, mod)).mod(mod);
out.println(c.min(mod.subtract(c)));
out.flush();
out.close();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
String str = null;
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public Double nextDouble() {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
}
}