Day1 exam

examination

T1

Given \ (A \) , \ (B \) , \ (C \) find
\ (A ^ B \ MOD \ C \)
1.2 format input
line, three numbers separated by spaces, respectively \ (A \ ) ; \ (B \) ; \ (C \) .
1.3 output formats
A number that is the answer.
1.4 Sample input
233
1.5 Sample Output

2

1.6 the data size, and arrangements
for the 30% of the data,. 1 ≤ \ (A \) ; \ (B \) ; \ (C \)(10 ^. 6 \) \
Data for 60%,. 1 ≤ \ (A \ ) ; \ (B \) ; \ (C \)\ (^ 10. 9 \)
to 100% of the data,. 1 ≤ \ (a; B; C \)\ (^ {15} 10 \)

Obviously quickly get power, but \ (a, b \) is \ (long \ long \) range, so again a turtle speed ride

Supplement:

If we want to read accurately and there is to know mod, mod we can while being read.

(AcknowledgmentscancerWater_lift)

\(Code\)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const int inf=2147483647;
inline ll read()
{
    char ch=getchar();
    ll x=0;bool f=0;//然鹅我这里看着1e15写成了int(甚至不知道自己为什么一共三个数要用快读)
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') f=1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=(x<<3)+(x<<1)+(ch^48);
        ch=getchar();
    }
    return f?-x:x;
}
ll a,b,c;
ll mul(ll x,ll y)
{
    ll r=0;
    while(y)
    {
        if(y&1) r=(r+x)%c;
        x=x*2%c;
        y>>=1;
    }
    return r;
}
ll ksm()
{
    ll r=1;
    while(b)
    {
        if(b&1) r=mul(r,a);
        a=mul(a,a);
        b>>=1;
    }
    return r;
}
int main()
{
    freopen("nine.in","r",stdin);
    freopen("nine.out","w",stdout);
    a=read();b=read();c=read();
    printf("%lld",ksm());
}
T2

2.1 Problem description
given length of \ (n-\) sequence \ (a_i \) .
Number number pair; request meets the following requirements (j i).
\ (I <J \)
\ (a_i> a_j \)
\ ((a_i a_j +) \ equiv0 \ mod3 \)
2.2 Input format
of the first line of an integer \ (n-\) .
The next line, separated by a space \ (n \) integer representing \ (a_i \) .
2.3 output format
output contains a number that is the answer.
2.4 Sample input
. 5
2. 3. 1. 1. 4
2.5 Output Sample
2
2.6 Sample explained
(1,3), (1,4) meet the requirements

2.7 scale data and agreed
for the first 30% of the data, $ n $ ≤ 2000.
For the first 60% of the data, \ (n-\) ≤. 4 × \ (10. 4 ^ \) .
100% for the first data,. 1 ≤ \ (n-\)\ (10. 5 ^ \) , 0 ≤ \ (a_i \)\ (. 6 10 ^ \)

Obviously the first two conditions is to reverse

Merge practices:

The number of reverse merge sort rectifiable

Processing Condition 3: if \ ((A + B) \ MOD \ 3 = 0 \) , then \ (A \ B MOD 3 + \ MOD \ 3 = 0 \) , so we can always maintain a suffix and, calculation \ (MOD \ = 0. 3 \) , \ (MOD \ =. 3. 1 \) , \ (MOD \ = 2. 3 \) the number of the number of

\(Code\ (from\ std)\)

#include<cstdio
#include <cstring>
#include <algorithm>
#include <iostream>

using namespace std;
typedef long long LL;

int n;
int a[100005];
int b[100005];
int sum[100005][3];

LL ans;

void merge_sort(int l,int r){
    if(l==r) return;
    int mid=(l+r)/2;
    merge_sort(l,mid);
    merge_sort(mid+1,r);
    for(int i=l;i<=r;i++) b[i]=a[i];
    for(int i=mid;i>=l;i--){
        if(i<mid) for(int j=0;j<3;j++) sum[i][j] = sum[i+1][j];//由于是从mid直接覆盖过来所以不用memset
        sum[i][b[i]%3]++;
    }
    int i = l;
    int j = mid+1;
    int k = l;

    while(i<=mid && j<=r){
        if(b[i]<=b[j]){
            a[k++]=b[i];
            i++;
        }else{
            // solve
            ans += sum[i][(3-b[j]%3)%3];
            a[k++]=b[j];
            j++;
        }
    }
    while(i<=mid) a[k++] = b[i],i++;
    while(j<=r) a[k++] = b[j],j++;//由于i已经到了mid,所以不需要再统计逆序对了
}

int main(){
    
    freopen("teen.in", "r", stdin);
    freopen("teen.out", "w", stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    merge_sort(1,n);
    printf("%lld\n",ans);
    return 0;
}

Fenwick tree approach:

3 can start the tree-like array, are maintained \ (mod \ 3 = 0, mod \ 3 = 1, mod \ 3 = 2 \) number. Because I will not seek the suffix tree and the array, the array values recorded tree \ (D \) is \ (1 + 10 ^ 6 \) Save the original value, so that looking \ ((d + a) \ mod \ 3 = 1 \) a \ (a \)

\(Code\)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const int inf=2147483647;
inline ll read()
{
    char ch=getchar();
    int x=0;bool f=0;
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') f=1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=(x<<3)+(x<<1)+(ch^48);
        ch=getchar();
    }
    return f?-x:x;
}
int n,a[100009],b[1000009][3];
ll ans;
int hv[1000009];
const int all=(int)1e6+1;
void add(int d)
{
    int md=d%3;hv[d]++;
    while(md<0) md=(md+3)%3;
    for(;d<=all;d+=(d&(-d)))
     b[d][md]++;
}
ll sum(int d,int md)
{
    ll r=0,dd=d;
    while(md<0) md=(md+3)%3;
    for(;d;d-=(d&(-d)))
     r+=b[d][md];
    if(dd%3==md&&hv[dd]) r-=hv[dd];
    return r; 
}
int main()
{
    freopen("teen.in","r",stdin);
    freopen("teen.out","w",stdout);
    n=read();
    for(int i=1;i<=n;i++)
        a[i]=read();
    for(int i=1;i<=n;i++)
    {
        int d=all-a[i];
        ans+=sum(d,(1-d+3)%3);
        add(d);
    } 
    printf("%lld",ans);
}
T3

3.1 Description of the problem
given a sequence of length n, ai, and a number k.
If an interval a [l: r] satisfies the following conditions, referred to as \ (\ Gamma \) interval.
\ (L <R & lt \)
\ (max_ l≤i≤r} {a_i - min_ l≤i≤r} {a_i ≤ K \)
asks all \ (\ Gamma \) number and interval. Because this is a big value, so the output answer mode \ (10 ^ 9 + 7 \) the remainder enough.
Due to the large amount of input, it is recommended to read the following functions.

inline int read(){
    char x;
    int num;
    while(x=getchar(),x<'0'||x>'9');
    num=x-'0';
    while(x=getchar(),x>='0'&&x<='9') num*=10,num+=x-'0';
    return num;
}

3.2 Input format
two spaces spaced from the first row of integers n; k.
The next line, separated by spaces the n integer representing ai.
3.3 output format
output is only an integer that represents the answer.
3.4 Sample input
. 6. 1
2. 3. 1. 1. 1. 4
3.5 sample output
12
3.6 Sample interpretation
of four \ (gamma] \) interval.
A [1..2] = 2,3
A [3..5] = 1,1,1
A [3..4] = 1,1
A [4..5] 1,1 =
3.7, and the size of data conventions
for the first 30% of the data, n ≤ 103.
For data before 60%, n ≤ 105.
100% for the first data, 1 ≤ n ≤ 1000000, 0 ≤ ai; k ≤ 109.

First consider only the \ (\ gamma \) the number of intervals

We found that if the interval \ ([l, r] \ ) is legitimate, then \ ([l, r '] , l <r' <r \) must be a legal range, if \ ([l, r] \ ) not legal, then the \ ([l, r '' ], r ''> r \) must not legal. For each \ (L \) , we are looking for the most by a \ (r \) .

At the same time we can enumerate the left end point of the right-half points

Half + \ (the Check \) :

Note that if the interval \ ([l, n] \ ) is legitimate, half out of the right end point will be \ (n-1 \) instead of \ (the n-\) , so the first sentence again.

Complexity \ (O (nlogn) \)

Think we can \ (O (n) \) to find \ (γ \) range.

We enumerate \ (l \) each \ (+ 1 \) , then the new \ (r \) and the original \ (r \) there must be contact.

Set \ (l '\) as the new \ (L \) , \ (r' \) as the new \ (r \) , then \ ([l ', r] \) is certainly legitimate range, then \ ( r '\) must not be more than \ (r \) is small, so you can move directly \ (r \) , each time to determine what is legitimate.

Such movement \ (l, r \) is generally \ (O (n) \) of

But \ (check \) or \ (logn \) , so how can \ (O (1) \ the Check \) ?

Because of \ (l, r \) are continuously moved back, not forward, the queue can be monotonous. Monotone two queues are maintained open \ (Max \) and \ (Min \) , \ (Check \) a \ (O (. 1) \) , overall complexity \ (O (n) \)

Digital and seek:
The first interval may seek out violence.

Consider the transfer.

\ (l-> l + 1, r \) unchanged: subtracting \ (len \ Times A [L] \) , where \ (len \) is the interval \ ([l, r] \ ) in length

\ (L \) constant, \ (R-> +. 1 R & lt \) : plus \ (SUM [R & lt +. 1] -sum [. 1-L] \) , i.e. plus the interval \ ([l, r +1] \) interval and

\(Code\) \((from\ std)\)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>

using namespace std;

typedef long long LL;
int n,k;
int a[1000005];
LL ans;
const LL mod=1e9+7;

struct dddl{
    int q[1000005];
    int h,t;
    void push(int x){
        while(h!=t && a[q[t-1]]>=a[x]) t--;
        q[t++] = x;
    }
    void pop(int x){
        if(q[h] == x) h++;
    }
    int ask(){
        return a[q[h]];
    }
}mn;

struct dddl2{
    int q[1000005];
    int h,t;
    void push(int x){
        while(h!=t && a[q[t-1]]<=a[x]) t--;
        q[t++] = x;
    }
    void pop(int x){
        if(q[h] == x) h++;
    }
    int ask(){
        return a[q[h]];
    }
}mx;

int main(){
    
    freopen("nineteen.in", "r", stdin);
    freopen("nineteen.out", "w", stdout);
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    int first = 1;
    LL sum=0;
    LL sum_suf=0;
    for(int i=1;i<=n;i++){
        mn.push(i);
        mx.push(i);
        sum = (sum+a[i])%mod;
        sum_suf = ((sum_suf)%mod+a[i]*(i-first+1)%mod)%mod;
        while(mx.ask() - mn.ask()>k){
            mx.pop(first);
            mn.pop(first);
            sum_suf = (sum_suf-sum+mod)%mod;
            sum = (sum-a[first]%mod+mod)%mod;
            first++;
        }
        ans += sum_suf;
        // printf("# %d %d %lld %lld\n",first, i, sum, sum_suf);
    }
    for(int i=1;i<=n;i++) ans = (ans-a[i]%mod+mod)%mod;
    cout<<ans<<endl;

    return 0;
}

Guess you like

Origin www.cnblogs.com/lcez56jsy/p/12209800.html