[TJOI2009] Guess (Luo Gu 3868)

Title Description

Existing two sets of figures, each of k, the first set of numbers, respectively: a1, a2, ..., ak, said second set of numbers, respectively b1, b2, ..., bk FIG. Wherein the second set of numbers are pairwise relatively prime. Seeking the smallest non-negative integer n, is satisfied for any i, n - ai bi be divisible.

Input Format

The first line of input data is an integer k, (1 ≤ k ≤ 10). The next two lines, the first line is: a1, a2, ..., ak, the second line is b1, b2, ..., bk

Output Format

The required output integer n.

Sample input and output

Input # 1

3
1 2 3
2 3 5

Output # 1

23

Description / Tips

All the data, the absolute value of the first set of numbers does not exceed 10 ^ 9 (may be negative), a second set of numbers are positive integers not exceeding 6000, and the product of all the numbers in the second group of no more than 10 ^ 18


This is actually just a " Chinese remainder theorem " template title only, then the topic and people are really geese frenzied well-intentioned, we seek to set several pit let us jump, unfortunately, I caught it.

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N=10000005;
 5 int n;
 6 int a[11],b[11];
 7 int read()
 8 {
 9     int x=0,f=1;
10     char ch=getchar();
11     while(ch<'0'||ch>'9')
12     {
13         if(ch=='-') f=-1;
14         ch=getchar();
15     }
16     while(ch>='0'&&ch<='9')
17     {
18         x=x*10+ch-'0';
19         ch=getchar();
20     }
21     return x*f;
22 }
23 void write(int x)
24 {
25     if(x<0)
26     {
27         putchar('-');
28         x=-x;
29     }
30     if(x>9) write(x/10);
31     putchar(x%10+'0');
32 }
33 void exgcd(ll a,ll b,ll &d,ll &x,ll &y)
34 {
35     if(!b)
36     {
37         d=a;
38         x=1;
39         y=0;
40     }
41     else
42     {
43         exgcd(b,a%b,d,x,y);
44         ll t=x;x=y;y=t-a/b*y;
45     }
46 }
47 int main()
48 {
49     int k;k=read();ll m=1,ans=0;
50     for(int i=1;i<=k;i++)a[i]=read();
51     for(int i=1;i<=k;i++)
52     {
53         b[i]=read();
54         m*=b[i];
55     }
56     for(int i=1;i<=k;i++)
57     {
58         ll mi=m/b[i],d,x,y;
59         exgcd(mi,b[i],d,x,y);
60         ans=(ans+a[i]*mi*x)%m;
61     }
62     printf("%lld",(ans+m)%m);
63     return 0;
64

 Ok,

After the code Qiaowan feel good about themselves,

Direct Ctrl + c, Ctrl + v,

Press the submit button.

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N=10000005;
 5 int n;
 6 int a[11],b[11];
 7 int read()
 8 {
 9     int x=0,f=1;
10     char ch=getchar();
11     while(ch<'0'||ch>'9')
12     {
13         if(ch=='-') f=-1;
14         ch=getchar();
15     }
16     while(ch>='0'&&ch<='9')
17     {
18         x=x*10+ch-'0';
19         ch=getchar();
20     }
21     return x*f;
22 }
23 void write(int x)
24 {
25     if(x<0)
26     {
27         putchar('-');
28         x=-x;
29     }
30     if(x>9) write(x/10);
31     putchar(x%10+'0');
32 }
33 void exgcd(ll a,ll b,ll &d,ll &x,ll &y)
34 {
35     if(!b)
36     {
37         d=a;
38         x=1;
39         y=0;
40     }
41     else
42     {
43         exgcd(b,a%b,d,x,y);
44         ll t=x;x=y;y=t-a/b*y;
45     }
46 }
47 int main()
48 {
49     int k;k=read();ll m=1,ans=0;
50     for(int i=1;i<=k;i++)a[i]=read();
51     for(int i=1;i<=k;i++)
52     {
53         b[i]=read();
54         m*=b[i];
55     }
56     for(int i=1;i<=k;i++)
57     {
58         ll mi=m/b[i],d,x,y;
59         exgcd(mi,b[i],d,x,y);
60         ans=(ans+a[i]*mi*x)%m;
61     }
62     printf("%lld",(ans+m)%m);
63     return 0;
64 }

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N=10000005;
 5 int n;
 6 int a[11],b[11];
 7 int read()
 8 {
 9     int x=0,f=1;
10     char ch=getchar();
11     while(ch<'0'||ch>'9')
12     {
13         if(ch=='-') f=-1;
14         ch=getchar();
15     }
16     while(ch>='0'&&ch<='9')
17     {
18         x=x*10+ch-'0';
19         ch=getchar();
20     }
21     return x*f;
22 }
23 void write(int x)
24 {
25     if(x<0)
26     {
27         putchar('-');
28         x=-x;
29     }
30     if(x>9) write(x/10);
31     putchar(x%10+'0');
32 }
33 void exgcd(ll a,ll b,ll &d,ll &x,ll &y)
34 {
35     if(!b)
36     {
37         d=a;
38         x=1;
39         y=0;
40     }
41     else
42     {
43         exgcd(b,a%b,d,x,y);
44         ll t=x;x=y;y=t-a/b*y;
45     }
46 }
47 int main()
48 {
49     int k;k=read();ll m=1,ans=0;
50     for(int i=1;i<=k;i++)a[i]=read();
51     for(int i=1;i<=k;i++)
52     {
53         b[i]=read();
54         m*=b[i];
55     }
56     for(int i=1;i<=k;i++)
57     {
58         ll mi=m/b[i],d,x,y;
59         exgcd(mi,b[i],d,x,y);
60         ans=(ans+a[i]*mi*x)%m;
61     }
62     printf("%lld",(ans+m)%m);
63     return 0;
64

 

The results actually found -

Then I get back to the code glances, feeling nothing wrong, solution to a problem so he looked at someone else's -

 

Oh, I added a fast hand speed ride, sweep again the code, test the sample over the  sample did not use soft again Ctrl + c, Ctrl + v, pressed the submit button, this is certainly no problem na ~

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N=10000005;
 5 int n;
 6 int a[11],b[11];
 7 int read()
 8 {
 9     int x=0,f=1;
10     char ch=getchar();
11     while(ch<'0'||ch>'9')
12     {
13         if(ch=='-') f=-1;
14         ch=getchar();
15     }
16     while(ch>='0'&&ch<='9')
17     {
18         x=x*10+ch-'0';
19         ch=getchar();
20     }
21     return x*f;
22 }
23 void write(int x)
24 {
25     if(x<0)
26     {
27         putchar('-');
28         x=-x;
29     }
30     if(x>9) write(x/10);
31     putchar(x%10+'0');
32 }
33 void exgcd(ll a,ll b,ll &d,ll &x,ll &y)
34 {
35     if(!b)
36     {
37         d=a;
38         x=1;
39         y=0;
40     }
41     else
42     {
43         exgcd(b,a%b,d,x,y);
44         ll t=x;x=y;y=t-a/b*y;
45     }
46 }
47 ll ff(ll a,ll b,ll m)
48 {
49     ll ans=0;
50     while(b)
51     {
52         if(b&1)ans=(ans+a)%m;
53         a=(a+a)%m;
54         b>>=1;
55     }
56     return ans;
57 }
58 int main()
59 {
60     int k;k=read();ll m=1,ans=0;
61     for(int i=1;i<=k;i++)a[i]=read();
62     for(int i=1;i<=k;i++)
63     {
64         b[i]=read();
65         m*=b[i];
66     }
67     for(int i=1;i<=k;i++)
68     {
69         ll mi=m/b[i],d,x,y;
70         exgcd(mi,b[i],d,x,y);
71         x=(x%b[i]+b[i])%b[i];
72         ans=(ans+ff(ff(mi,x,m),a[i],m))%m;
73     }
74     printf("%lld",(ans+m)%m);
75     return 0;
76 }

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N=10000005;
 5 int n;
 6 int a[11],b[11];
 7 int read()
 8 {
 9     int x=0,f=1;
10     char ch=getchar();
11     while(ch<'0'||ch>'9')
12     {
13         if(ch=='-') f=-1;
14         ch=getchar();
15     }
16     while(ch>='0'&&ch<='9')
17     {
18         x=x*10+ch-'0';
19         ch=getchar();
20     }
21     return x*f;
22 }
23 void write(int x)
24 {
25     if(x<0)
26     {
27         putchar('-');
28         x=-x;
29     }
30     if(x>9) write(x/10);
31     putchar(x%10+'0');
32 }
33 void exgcd(ll a,ll b,ll &d,ll &x,ll &y)
34 {
35     if(!b)
36     {
37         d=a;
38         x=1;
39         y=0;
40     }
41     else
42     {
43         exgcd(b,a%b,d,x,y);
44         ll t=x;x=y;y=t-a/b*y;
45     }
46 }
47 ll ff(ll a,ll b,ll m)
48 {
49     ll ans=0;
50     while(b)
51     {
52         if(b&1)ans=(ans+a)%m;
53         a=(a+a)%m;
54         b>>=1;
55     }
56     return ans;
57 }
58 int main()
59 {
60     int k;k=read();ll m=1,ans=0;
61     for(int i=1;i<=k;i++)a[i]=read();
62     for(int i=1;i<=k;i++)
63     {
64         b[i]=read();
65         m*=b[i];
66     }
67     for(int i=1;i<=k;i++)
68     {
69         ll mi=m/b[i],d,x,y;
70         exgcd(mi,b[i],d,x,y);
71         x=(x%b[i]+b[i])%b[i];
72         ans=(ans+ff(ff(mi,x,m),a[i],m))%m;
73     }
74     printf("%lld",(ans+m)%m);
75     return 0;

 

However goose on her face that came so suddenly (¯ε (# ¯) ☆ ╰╮ (¯ ▽ ¯ ///)

What I especially second TLE point out is how is it? ! !

No, the first pass are submitted duck Mo was the problem! I could not help but want to vomit fragrance ( `understands')

 Rushed to the area turn to the problem solution Mo Kanwan solution to a problem before -

 ╮ (╯ ▽ ╰) ╭ this cancer problem, really nothing I can do ┑ (¯Д ¯) ┍

The third submission, finally full screen all green (in order to live a decent, do you have to question the point of green -

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N=10000005;
 5 int n;
 6 int a[11],b[11];
 7 int read()
 8 {
 9     int x=0,f=1;
10     char ch=getchar();
11     while(ch<'0'||ch>'9')
12     {
13         if(ch=='-') f=-1;
14         ch=getchar();
15     }
16     while(ch>='0'&&ch<='9')
17     {
18         x=x*10+ch-'0';
19         ch=getchar();
20     }
21     return x*f;
22 }
23 void write(int x)
24 {
25     if(x<0)
26     {
27         putchar('-');
28         x=-x;
29     }
30     if(x>9) write(x/10);
31     putchar(x%10+'0');
32 }
33 void exgcd(ll a,ll b,ll &d,ll &x,ll &y)
34 {
35     if(!b)
36     {
37         d=a;
38         x=1;
39         y=0;
40     }
41     else
42     {
43         exgcd(b,a%b,d,x,y);
44         ll t=x;x=y;y=t-a/b*y;
45     }
46 }
47 ll ff(ll a,ll b,ll m)
48 {
49     ll ans=0;
50     while(b)
51     {
52         if(b&1)ans=(ans+a)%m;
53         a=(a+a)%m;
54         b>>=1;
55     }
56     return ans;
57 }
58 int main()
59 {
60     int k;k=read();ll m=1,ans=0;
61     for(int i=1;i<=k;i++)a[i]=read();
62     for(int i=1;i<=k;i++)
63     {
64         b[i]=read();
65         m*=b[i];
66     }
67     for(int i=1;i<=k;i++)
68     {
69         ll mi=m/b[i],d,x,y;
70         exgcd(mi,b[i],d,x,y);
71         x=(x%b[i]+b[i])%b[i];
72         ans=(ans+ff(ff(mi,x,m),(a[i]+m)%m,m))%m;
73     }
74     printf("%lld",(ans+m)%m);
75     return 0;
76 }

 // Reference: lahlah's blog

 

 

Guess you like

Origin www.cnblogs.com/ljy-endl/p/11409941.html
Recommended