C. Decryption
题目描述
Fatwj udgmv ljauck, xdqafy klsjk khjwsv zslw, Yafzsf xsj vsjc vwyjww. Ak kggf sk lzw ygdvwf oafv sfv lzw bsvw vwo ewwl, lzwq oadd oaf lzw affmewjstdw af lzw ogjdv.
Twfvwj sk oslwj, zshhq sk s vjwse, twsj lg yg tsuc lg esyhaw tjavyw. Ix lzw log dgnw xgj s dgfy laew, sfv fgl af lzw egjfafy sfv wnwfafy!
Tzw fayzl ak fgl gnwj. Lggcafy sl lzw lzgmksfv qwsj dgnw gx Cgozwjv sfv Wwsnwj Gajd oalz vwdauslw sfv ugflafmgmk wqwk, lzw hdsaf, vwdauslw sfv dgfy-lwje zshhafwkk aeewvaslwdq hwjewslwk eq zwsjl. Pwjzshk zshhafwkk ak dacw lzak, fgl xgj lzw egjfafy sfv wnwfafy, tml xgj wnwj. Il ak ksav lzsl Tgfypaf dguc ak s cafv gx cwwhkscw xgj dgnwjk, ozauz ak wfyjsnwv oalz log hwghdw’k fsewk. Il oalfwkkwk lzw wnwjdsklafy dgnw sfv lwddk lzw mhk sfv vgofk sfv kowwlfwkk gx dgnw.
Ganwf lzjww aflwywjk s, t sfv h, Ygm fwwv lg usdumdslw lzw nsdmw gx s / t egvmdw h.
Hshhq Ahjad Fggd’k Dsq!
输入格式
Sww lzw vwkujahlagf stgnw.
1 <= s, h <= 10^5.
1 <= t <= 10^9.
h ak s hjaew fmetwj ak ymsjsflwwv.
输出格式
Sww lzw vwkujahlagf stgnw.
输入输出样例
无
说明/提示
The problem above has been encrypted with a single lowercase letters s
The encryption method is as follows:
For every character
If this character ch is a lowercase letter then change it to (ch - ‘a’ + s - ‘a’) % 26 + ‘a’;
else do not change this character.
天真的我以为加密就是本题的难点了,结果证明,师哥的心思果然不是我们能猜的。小菜鸡不会写逆元,趁这个机会学一下吧。
分析
题目被一个小写字母 s 加密了,加密规则是:对于每个字符 ch ,如果这个字符是小写字母则把它改成 (ch-‘a’+s-‘a’),否则不改变这个字符。
因此我们需要先把题目解密,可是我们不知道小写字母 s 呀(所以从 ‘a’ 到 ‘z’ 一个个试吧。
规律就是ch+8(其实解密不难,理解了题意就很简单)。
解密之后可以发现题目就是让求 a/b%p,保证 p 为质数。
解密代码
#include <bits/stdc++.h>
#include<cmath>
using namespace std;
string s;
int main()
{
while(cin >> s)
{
int len = s.length();
for(int i=0;i<len;i++)
{
if(s[i]>='a'&&s[i]<='z') s[i]=((s[i]-'a')+8)%26+'a';
else continue;
}
cout<<s<<'\n';
}
return 0;
}
解密后的题目为
Fiber cloud tricks, flying stars spread hate, Yinhan far dark degree. As soon as the golden wind and the jade dew meet, they will win the innumerable in the world.
Tender as water, happy as a dream, bear to go back to magpie bridge. If the two love for a long time, and not in the morning and evening!
The night is not over. Looking at the thousand year love of Cowherd and Weaver Girl with delicate and continuous eyes, the plain, delicate and long-term happiness immediately permeates my heart. Perhaps happiness is like this, not for the morning and evening, but for ever. It is said that Tongxin lock is a kind of keepsake for lovers, which is engraved with two people’s names. It witnesses the everlasting love and tells the ups and downs and sweetness of love.
Given three integers a, b and p, You need to calculate the value of a / b module p.
Happy April Fool’s Day!
输入格式
See the description above.
1 <= a, p <= 10^5.
1 <= b <= 10^9.
p is a prime number is guaranteed.
输出格式
See the description above.
提交代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll quick(ll a, ll b, ll p)
{
ll s = 1;
while(b)
{
if(b & 1) s = s * a % p;
a = a * a % p;
b >>= 1;
}
return s % p;
}
ll inv(ll n, ll p)
{
return quick(n, p - 2, p);
}
void work()
{
ll a, b, p; scanf("%lld %lld %lld", &a, &b, &p);
printf("%lld\n", a * inv(b, p) % p);
}
int main()
{
work();
return 0;
}
实在不行就去背模板吧(沧桑)