9.1 solution to a problem

T1

Is something I did not find a law of nature or type of call, for any one person, you give him the existing sesame $ {\ times} 2 $ then $ {\%} (n + m) $, is the time to adjust after the amount of sesame his own, to think about, if he is the man hands sesame relatively small, do must be right, if he is more hands sesame that person? Did not permit it, hand to play. .

 1 #include<iostream>
 2 #include<cstdio>
 3 #define int long long
 4 #define maxn 100100000
 5 using namespace std;
 6 int n,m,k,mod,mi;
 7 int pd[maxn],bh[maxn];
 8 int ksm(int a,int b)
 9 {
10     int ans=1;
11     while(b)
12     {
13         if(b&1)  ans=(ans*a)%mod;
14         b=b>>1;  a=(a*a)%mod;
15     }
16     return ans;
17 }
18 main()
19 {
20     scanf("%lld%lld%lld",&n,&m,&k);
21     mod=n+m;  mi=ksm(2,k);  n=(n*mi)%mod;
22     printf("%lld\n",min(n,mod-n));
23     return 0;
24 }
View Code

T2

This problem also of the equation, the title does not contain the required legal section $ x <y $ and $ a_x {\%} a_y = K $ of points, then we for any $ j $, and he can find from less than his most recent comply with the above formulas $ i $, which is all you can do legally middle range, so the question now is to find the $ i $

violence

First enumerate $ j $, $ j $ sweep over from forward, to find the nearest $ i $, calculate contributions, which were due to the need to ensure that we do not meet the above conditions on that point, so every $ i find a $, you must update the $ j $ forward sweep of the top-left circles, because for the $ j $ $ $ i found, it should be all found earlier and $ i $ to take a $ min $, so complexity is $ O (n ^ 2) $, obviously water is not in the past, consider optimizing

optimization

We have no way to find each $ j $ corresponding to $ i $, without sweeping it forward? Consider the formula for the conversion of the above, to ensure that $ x <y $

  $a_x{\%}a_y=k$

${\Rightarrow}a_x-n{\times}a_y=k$

${\Rightarrow}a_x-k=n{\times}a_y$

This time we found that all the $ a_y $ $ a_x-k $ is a factor, then we can use $ O (\ sqrt {n}) $ complexity of screening out all factor $ a_x-k $ look for this factor in the $ j $ in front there has never been seen before and a maximum position, you can find directly what we want $ i $

 1 //j<i 分解a[j]-k,找一个下标最小的i
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<vector>
 5 #define maxn 100100
 6 #define int long long
 7 using namespace std;
 8 int n,k,ans,tail;
 9 int a[maxn],bh[maxn];
10 main()
11 {
12     scanf("%lld%lld",&n,&k);  tail=n;
13     for(int i=1;i<=n;++i)  scanf("%lld",&a[i]);
14     for(int i=n;i>=1;--i)//左端点
15     {
16         int base=a[i]-k,right=tail+1/*右端点*/;
17         if(base<0)  continue;
18         if(base==0)
19         {
20             for(int j=i+1;j<=tail;++j)
21                 if(a[j]>a[i])  {right=j;  break;}
22         }
23         for(int j=1;j*j<=base;++j)
24         {
25             if(base%j==0)
26             {
27                 if(j>k&&bh[j]>i)  right=min(right,bh[j]);
28                 if(base/j>k&&bh[base/j]>i)  right=min(right,bh[base/j]);
29             }
30         }
31         if(right!=tail+1)
32         {
33             ans+=(tail-i+1)*(tail-i+2)/2;
34             ans+=-(right-i)*(right-i+1)/2-(tail-right+1);
35         }
36         tail=right-1;  bh[a[i]]=i;
37     }
38     ans+=tail*(tail+1)/2;
39     printf("%lld\n",ans);
40     return 0;
41 }
View Code

T3

The time will be filled pit in the evening, the first cushions

Guess you like

Origin www.cnblogs.com/hzjuruo/p/11484211.html
9.1