只要把模逆元求出来就很轻松了
#include <cstdio> #include <iostream> #include <cstring> using namespace std; int const mod = 26; char str[100]; int Expend_Gcd(int a,int n){ int p = a,q = n; int x = 0,y = 1; int z = q/p; while(p != 1 && q != 1){ int t = p; p = q % p; q = t; t = y; y = x - y * z; x = t; z = q/p; } y %= n; if(y < 0) y += n; return y; } int main(){ int k1,k2; while(cin>>str>>k1>>k2){ int x = Expend_Gcd(k1,mod); for(int i = 0;str[i];i++){ int m = str[i] - 'A'; str[i] = (x * (m - k2 + mod)) % mod + 'A'; } puts(str); } return 0; }