QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#1263#752060#9738. Make It DivisibleMilmonsdnuwySuccess!2024-11-27 18:44:302024-11-27 18:44:30

Details

Extra Test:

Time Limit Exceeded

input:

1
50000 1000000000
735134402 2 2 2 735134402 735134402 735134402 2 735134402 735134402 735134402 2 735134402 735134402 2 735134402 2 735134402 2 2 2 735134402 735134402 2 735134402 2 2 735134402 2 2 2 735134402 735134402 735134402 2 2 2 2 2 735134402 2 2 2 735134402 735134402 2 735134402 2 2 2 73513...

output:

1342 3809753473

result:


IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#752060#9738. Make It Divisiblesdnuwy#TL 524ms22216kbC++203.0kb2024-11-15 21:50:552024-11-27 18:47:30

answer

//Think twice,code once.
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define INF 0x3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define ff first
#define ss second
#define pii pair<int,int>
#define mem(i,n) memset(i,n,sizeof i)
#define dg(a) std::cout << #a << " = " << a << endl
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

const int N=5e4+10;
int st[N][20];
int pos[N][20];
int a[N];
int lg2[N];

void build(int n) {
    for (int i = 2; i <= n; i ++ ) lg2[i] = lg2[i >> 1] + 1;
	for (int i = 1; i <= n; i ++ ) 
    {
        st[i][0] = a[i];
        pos[i][0]=i;
    }
	for (int j = 1; (1 << j) <= n; j ++ ) {
		for (int i = 1; i + (1 << j - 1) <= n; i ++ ) {
            if(st[i][j-1]<st[i + (1 << j - 1)][j - 1])
            {
                pos[i][j]=pos[i][j-1];
            }
            else
            {
                pos[i][j]=pos[i + (1 << j - 1)][j - 1];
            }
			st[i][j] = min(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);
            
		}
	}
}

inline int query_num(int l, int r,int x) {
    if(l>r) return -x;
	int s =  lg2[r - l + 1];
	return min(st[l][s], st[r - (1 << s) + 1][s]);
}

inline int query_pos(int l,int r)
{
    int s=lg2[r-l+1];
    if(st[l][s]<st[r - (1 << s) + 1][s])
    {
        return pos[l][s];
    }
    else
    {
        return pos[r - (1 << s) + 1][s];
    }
}

bool check(int l,int r,int x)
{
    if(l==r||l>r) return true;
    bool res=true;
    int idx=query_pos(l,r);
    int mn=query_num(l,r,x)+x;
    if((query_num(l,idx-1,x)+x)%mn!=0||(query_num(idx+1,r,x)+x)%mn!=0) return false;
    res&=check(l,idx-1,x);
    if(!res) return false;
    res&=check(idx+1,r,x);
    return res;
}

void solve()
{
    int n,k;
    cin >> n >> k;
    int num=0;
    int mn=inf;
    for(int i=1;i<=n;i++)
    {
        cin >> a[i];
        mn=min(mn,a[i]);
    }
    bool flag=1;
    for(int i=2;i<=n;i++)
    {
        if(a[i]!=a[i-1]) flag=false;
    }
    if(flag)
    {
        cout << k << ' ' << k*(k+1)/2 << endl;
        return;
    }
    int mn2=inf;
    for(int i=1;i<=n;i++)
    {
        if(a[i]!=mn)
        {
            mn2=min(a[i],mn2);
        }
    }
    build(n);
    for(int i=2;i<=n;i++)
    {
        num=gcd(num,abs(a[i]-a[i-1]));
    }  
    int ans1=0;
    int ans2=0;
    vector<int>c;
    for(int i=1;i*i<=num;i++)
    {
        if(num%i==0)
        {
            c.push_back(i);
            if(num/i!=i) c.push_back(num/i);
        }
    }
    sort(c.begin(),c.end());
    for(int y:c)
    {
        int x=y-mn;
        if(x>=mn2) break;
        if(x<=0||x>k) continue;
        if(check(1,n,x))
        {
            ans1++;
            ans2+=x;
        }
    }
    cout << ans1 << ' ' << ans2 << endl;
}

int32_t main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    int t = 1;
    std::cin >> t;
    while(t--) solve();
    return 0;
}