zoukankan      html  css  js  c++  java
  • 洛谷P2485 [SDOI2011]计算器

    咕咕咕·····

    #include <map>
    #include <cmath>
    #include <cstdio>
    #include <algorithm>
    #define LL long long
    #define gc() getchar()
    #define rep(i, a, b) for(int i = (a); i <= (b); ++i)
    using namespace std;
    const int N = 1e8 + 5;
    LL t, x, y, z, k, gg;
    LL X1, Y1;
    map <LL, LL> c;
    
    inline int read() {
    	char ch = gc();
    	int ans = 0;
    	while (ch > '9' || ch < '0') ch = gc();
    	while (ch >= '0' && ch <= '9') ans = (ans << 1) + (ans << 3) + ch - '0', ch = gc();
    	return ans;
    }
    
    inline LL quick_pow(LL a, LL b, LL p) {
    	LL base = a, ans = 1;
    	while (b) {
    		if (b & 1) ans = (ans * base) % p;
    		base = (base * base) % p;
    		b >>= 1;
    	}
    	return ans;
    } 
    
    inline void exgcd(int a, int b) {
    	if (!b) { gg = a; X1 = 1, Y1 = 0; return; }
    	exgcd(b, a % b);
    	int temp = X1;
    	X1 = Y1, Y1 = temp - a / b * Y1;	
    }
    
    int main() {
    	t = read(), k = read();
    	if (k == 1) {
    		while(t--) {
    			int p;
    			y = read(), z = read(), p = read();
    			printf("%lld
    ", quick_pow(y, z, p)); 
    		}
    	}
    	else if (k == 2) {
    		while (t--) {
    			int p;
    			y = read(), z = read(), p = read();
    			exgcd(y, p);
    			if (z % gg) { printf("Orz, I cannot find x!
    "); continue; }
    			X1 = X1 * (z / gg);
    			if (X1 > 0 && X1 % p) X1 = X1 - (X1 / p) * p;
    			else if (X1 < 0) {
    				if ((0 - X1) % p) X1 = X1 + ((0 - X1) / p + 1) * p;
    				else X1 = 0;
    			}
    			printf("%lld
    ", X1);
    		} 
    	}
    	else {
    		while (t--) {
    			int p;
    			y = read(), z = read(), p = read();
    			if (z && y % p == 0) { printf("Orz, I cannot find x!
    "); continue; }
    			x = ceil(sqrt(p));
    			c.clear();
    			LL cur = z % p, po = quick_pow(y, x, p);
    			c[cur] = 0;
    			rep(i, 1, x) cur = (cur * y) % p, c[cur] = i;
    			cur = 1;
    			int vis = 1;
    			rep(i, 1, x) {
    				cur = (cur * po) % p;
    				if (c[cur]) { X1 = (i * x - c[cur]) % p; printf("%lld
    ", (X1 + p) % p); vis = 0; break; }
    			}
    			if (vis) printf("Orz, I cannot find x!
    ");
    		} 
    	}
    	return 0;
    } 
    
  • 相关阅读:
    Linux centos 安装 Node.js
    maven 常用命令
    linux centos 设置笔记本合盖不待机
    linux centos 网卡有关调试
    Linux centos 安装 maven 3.5.4
    Linux centos 安装 jenkins & 本地构建jar & 远程构建jar
    Linux centos 安装 tomcat 7
    Linux centos 安装 JDK 8
    JS正则 replace()方法全局替换变量(可以对变量进行全文替换)
    node:json与csv互转
  • 原文地址:https://www.cnblogs.com/Miraclys/p/12422938.html
Copyright © 2011-2022 走看看