[Acwing] 532. 货币系统 背包||生成函数||搜索

前言

传送门 :

完全背包

线性代数 :

题目要求:
( n , a ) 等 价 于 ( m , b ) ⇔ ∀ k ∈ Z + (n,a) 等价于(m,b) \Leftrightarrow \forall k \in Z^+ (n,a)(m,b)kZ+

求最小的 m 使得等式成立

m m m的范围看出来是不可能超过 0 − n 0 - n 0n

因此问题在于 缩小 ( n , a ) (n,a) (n,a) 即求 ( n , a ) (n,a) (n,a)的最大向量无关组

而这个又可以通过 筛法来求

因为对于每一个 a a a如果可以 被小于自己的数表示 那么这个
数就会被筛掉

CODE

// Problem: B. Update Files

// Contest: Codeforces - Educational Codeforces Round 116 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1606/problem/B
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define px first
#define py second

typedef pair<int,int> pii;
int dxy[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
const int N  = 40;


void cal()
{

}


void solve()
{
	ll n,k;
	cin>>n>>k;

	ll sum = (k)*(k+1) /2;

    if(n == 1)
    {
        cout<<0<<endl;
        return;
    }
    
    if( k == 1)
    {
        cout<<n-1<<endl;
        return;
    }

	if(sum  > n)
	{
			for(ll  i = k-1;k>=1;i--)
			{
				if((i*(i+1))/2  == n)
				{
					cout<<i<<endl;
					return;
				}

				if((i*(i+1))/2 < n)
				{
					cout<<i+1<<endl;
					return;
				}

			}
	}
	else
	{
		if(sum == n )
		{
            cout<<k<<endl;
            return;
		}

		n-=sum;

		ll d = n/k;
		n%=k;

		if(n)
		cout<<k+d+1<<endl;
		else
		cout<<d+k<<endl;

	}

}

int main()
{
    ios::sync_with_stdio(false);
    int t;cin>>t;while(t -- )
    solve();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值