Vigenère 密码
这个名字实在打不来...
题解:模拟
1 #include <cstdio>
2 #include <cstring>
3
4 const int MAXL = 1000+10;
5
6 bool cj;
7 int cl, kl;
8 char c[MAXL], k[MAXL], an;
9
10 int main(){
11 gets(k), gets(c);
12 kl = strlen(k), cl = strlen(c);
13 for (int i=0; i<kl; i++)
14 if (k[i]<97) k[i] += 32;
15
16 for (int i=0; i<cl; i++){
17 if (c[i]<97) c[i] += 32, cj = true;
18 else cj = false;
19 an = (c[i]-'a'-(k[i%kl]-'a')+26)%26+'a';
20 if (cj) an -= 32;
21 printf("%c", an);
22 }
23 }
国王游戏:
题解:贪心+高精度,好吧,我看了wikioi的c++最快,这个方法简直....
1 #include <cmath> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using std::sort; 6 7 const int MAXN = 1000+10; 8 const int MAXL = 100000+10; 9 const int MOD = 10000; 10 11 int n; 12 double sum[MAXN]; 13 14 struct bigInteger{ 15 int len, a[MAXL]; 16 17 bigInteger(){ 18 memset(a, 0, sizeof(a)); 19 len = 0; 20 } 21 22 inline void print(){ 23 printf("%d", a[len]); 24 for (int i=len-1; i>=1; i--) 25 printf("%04d", a[i]); 26 printf(" "); 27 } 28 }Pri; 29 30 struct Minister{ 31 int l, r; 32 double sm; 33 34 friend bool operator < (const Minister& a, const Minister& b){ 35 return a.sm<b.sm; 36 } 37 }mt[MAXN]; 38 39 void operator *= (bigInteger& a, int b){ 40 for (int i=1, c=0, t; i<=a.len+2; i++) 41 t = a.a[i]*b+c, 42 a.a[i] = t%MOD, 43 c = t/MOD; 44 45 while (a.a[a.len+1]) a.len ++; 46 } 47 48 void operator /= (bigInteger& a, int b){ 49 for (int i=a.len, c=0; i>=1; i--) 50 c = c*MOD+a.a[i], 51 a.a[i] = c/b, 52 c %= b; 53 while (!a.a[a.len] && a.len>1) -- a.len; 54 } 55 56 int main(){ 57 scanf("%d", &n); 58 for (int i=0; i<=n; i++) 59 scanf("%d %d", &mt[i].l, &mt[i].r), 60 mt[i].sm = log(mt[i].l)+log(mt[i].r); 61 62 sort(mt+1, mt+n+1); 63 64 double t; 65 sum[0] = log(mt[0].l); 66 for (int i=1; i<=n; i++) 67 t = log(mt[i].l), 68 sum[i] = sum[i-1]+t; 69 70 int k = 0; 71 t = -1; 72 for (int i=1; i<=n; i++) 73 if (sum[i-1]-log(mt[i].r)>t) 74 k = i, t = sum[i-1]-log(mt[i].r); 75 76 Pri.len = 1, Pri.a[1] = 1; 77 for (int i=0; i<k; i++) 78 Pri *= mt[i].l; 79 Pri /= mt[k].r; 80 Pri.print(); 81 }
同余方程
题解:我只会用扩展欧几里得解...
1 #include <cstdio>
2
3 int a, b, x, y, t;
4
5 inline void ExGcd(int a, int b){
6 if (b==0) return;
7 ExGcd(b, a%b);
8 t = x, x = y, y = t-(a/b)*x;
9 }
10
11 int main(){
12 scanf("%d %d", &a, &b), x = 1, y = 0;
13 ExGcd(a, b);
14 while (x<=0) x += b;
15 printf("%d", x);
16 }
借教室
题解:二分。开始没加读入优化,在vijos上TLE了...
1 #include <cstdio>
2 #include <cstring>
3
4 const int MAXN = 1e6+10;
5
6 struct Indent{
7 int l, r, n;
8 }d[MAXN];
9
10 int n, m, mid, temp[MAXN], lim[MAXN];
11 char c;
12
13 inline int NextInt(){
14 int ret = 0;
15 do
16 c = getchar();
17 while (!(48<=c && c<=57));
18
19 do
20 ret *= 10, ret += c-48, c = getchar();
21 while (48<=c && c<=57);
22
23 return ret;
24 }
25
26 inline bool Check(int x){
27 memset(temp, 0, sizeof(temp));
28
29 int sum = 0;
30 for (register int i=1; i<=x; i++)
31 temp[d[i].l] += d[i].n, temp[d[i].r+1] -= d[i].n;
32 for (register int i=1; i<=n; i++){
33 sum += temp[i];
34 if (sum>lim[i]) return false;
35 }
36 return true;
37 }
38
39 int main(){
40 n = NextInt(), m = NextInt();
41 for (register int i=1; i<=n; i++)
42 lim[i] = NextInt();
43 for (register int i=1; i<=m; i++)
44 d[i].n = NextInt(), d[i].l = NextInt(), d[i].r = NextInt();
45
46 int l = 1, r = m+1, mid;
47 while (l<r){
48 mid = (l+r)>>1;
49 if (Check(mid)) l = mid+1;
50 else r = mid;
51 }
52 if (l<m) printf("-1
%d", l);
53 else printf("0");
54 }