Luo Gu P3955 librarian solution to a problem

Daily questions day12 punch

Analysis

Fast power analog +

First coding books save up ordering to ensure that the first one found is minimal. If a required bit number x, x will be the number of analog power 10 Similarly, we can determine the law suffix. Each code encoding and needs no more than 10,000,000, so x <8. For insurance, I used to seek quick power x 10 to the power.

Time complexity of O (n * q * log (8)) <O (1000000) acceptable

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define maxn 1000+10
 6 using namespace std;
 7 inline int read() 
 8 {
 9     int x=0;
10     bool f=1;
11     char c=getchar();
12     for(; !isdigit(c); c=getchar()) if(c=='-') f=0;
13     for(; isdigit(c); c=getchar()) x=(x<<3)+(x<<1)+c-'0';
14     if(f) return x;
15     return 0-x;
16 }
17 inline void write(int x)
18 {
19     if(x<0){putchar('-');x=-x;}
20     if(x>9)write(x/10);
21     putchar(x%10+'0');
22 }
23 int n,q;
24 int num[maxn];
25 inline int ksm(int x,int y)
26 {
27     int res=1;
28     while(y>0)
29     {
30         if(y&1)
31             res*=x;
32         x*=x;
33         y>>=1;
34     }
35     return res;
36 }
37 int main()
38 {
39     n=read();q=read();
40     for(int i=1;i<=n;i++) num[i]=read();
41     sort(num+1,num+n+1);
42     for(int i=1;i<=q;i++)
43     {
44         int len=read(),req=read(),ans=-1;
45         for(int j=1;j<=n;j++)
46             if(num[j]%ksm(10,len)==req)
47             {
48                 ans=num[j];
49                 break;
50             } 
51         write(ans);
52         printf("\n");
53     }
54     return 0;
55 }

Please Gangster treatise(Anyway, I do not know what that means treatise)

 

Guess you like

Origin www.cnblogs.com/handsome-zyc/p/11517924.html