湖北2022省赛C. Potion(hard version),推结论

这篇博客主要探讨了一种使用仅含有a和a+b量程的烧杯来配比x:y水与药水比例的方法。通过分析问题,将比例简化,发现解必定与(1+g)的次方有关,其中g为a:b的比例。当b>=a时,检查x和y是否可以不断减去最大项达到比例;当b<a时,检查x和y在不同进制下的余数。文章提供了C++代码实现,并对特殊情况进行了处理,最后检查x和y是否都为零来验证结果。
摘要由CSDN通过智能技术生成

题意:

 

给你一个只有a与a+b量程的烧杯,有两种操作:

1:将烧杯装满水或魔法药水

2:倒掉一些混合药水,留下a。

问你是否能配出x:y的水与药水比例。

分析:

先将x:y,a:b去除公因数。

将a:b变成1:g,发现答案必定是(1+g)的次方,并且独立起来每个(1+g)进制下都有且只能有一个g。

比如1:4,x与y某个数5进制下都为4,1:0.5,x与y某个数1.5进制下都为0.5。

如果b>=a,则(a+b)进制满足第i项大于前面所有项之和,检查x,y不断减去大的一个即可。

如果b<a,则高进制必定带有更多小数点,检查x,y哪个%s^i!=0就代表有这个数,减去。

最后检查x与y是否全为零。

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <iomanip>//io控制头文件 cout<<fixed<<setprecision(8);
//#include <unordered_map>
#include <bitset>      
//#pragma GCC optimize(2)
#define ms(x,n) memset(x,n,sizeof x)
#define endl '\n'
#define pii pair<int,int> 
#define M(a,b) make_pair(a,b)
#define fi first
#define se second
#define pb push_back
#define __builtin_popcount popcnt1//getcnt 
#define db1(x) cerr<<#x<<"="<<(x)<<" "
#define db2(x) cerr<<#x<<"="<<(x)<<"\n"
#define int long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll _gcd(ll a, ll b) { return b > 0 ? _gcd(b, a % b) : a; }
int popcnt1(ll x) { int ans = 0;for (int j = 0;j <= 60;j++)if (x & (1ll << j)) ans++;return ans; }
template <typename T> inline void read(T& t) { int f = 0, c = getchar(); t = 0;while (!isdigit(c)) f |= c == '-', c = getchar();while (isdigit(c)) t = t * 10 + c - 48, c = getchar();if (f) t = -t; }
template <typename T> void print(T x) { if (x < 0) x = -x, putchar('-');if (x > 9) print(x / 10);putchar(x % 10 + 48); }
const double pi = acos(-1.0);
const double eps = 1e-7;//有时候精度可能不够
const int inf = 0x3f3f3f3f;
const ll Inf = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const int N = 100+10;
int n, m,s, t, k;
int a,b,x,y;
int facm[N], facs[N],cntm,cnts;
void init() {
    facm[0] = 1, cntm = 0;
    for (int i = 1;i < N;i++) {
        facm[i] = facm[i - 1] * m;
        if (facm[i] < 0 || facm[i]>2e18) {
            break;
        }
        cntm++;
    }
    facs[0] = 1, cnts = 0;
    for (int i = 1;i < N;i++) {
        facs[i] = facs[i - 1] * s;
        if (facs[i] < 0 || facs[i]>2e18) {
            break;
        }
        cnts++;
    }
}
bool check1(int x, int y) {
    int now, cnt;
    for (int i = 1;i <= n;i++) {
        cnt = 0;
        if (x % facs[i] != 0) cnt++, now = 0;
        if (y % facs[i] != 0) cnt++, now = 1;
        if (cnt == 2) {
            return 0;
        }
        if (!now) {
            x -= facs[i - 1] * facm[(n - 1) - (i - 1)];
            if (x < 0) return 0;
        }
        else {
            y-= facs[i - 1] * facm[(n - 1) - (i - 1)];
            if (y < 0) return 0;
        }
    }
    return (x == 0) && (y == 0);
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> t;
    while (t--) {
        cin >> x >> y >> a >> b;
        int ab = _gcd(a, b), xy = _gcd(x, y);
        a /= ab, b /= ab, x /= xy, y /= xy;
        m = a + b, s = a;
        init();
        bool fg = 0;
        int ans = 0;
        for (int i = 1;i <= cntm;i++) {
            if (x + y == facm[i]) {
                n=ans = i;break;
            }
        }
        if (!ans) {
            cout << -1 << endl;continue;
        }
        if (a > b) {
            if (x >= facs[n]) {
                int resx = x - facs[n], resy = y;
                if (resx % (m - s) == 0 && resy % (m - s) == 0) {
                    resx /= (m - s), resy /= (m - s);
                    if (check1(resx, resy)) fg = 1;
                }
            }
            if (!fg && y >= facs[n]) {
                int resx = x, resy = y-facs[n];
                if (resx % (m - s) == 0 && resy % (m - s) == 0) {
                    resx /= (m - s), resy /= (m - s);
                    if (check1(resx, resy)) fg = 1;
                }
            }
        }
        else {
            bool fg2 = 1;
            for (int i = 0;i <= n;i++) {
                if (i == n) {
                    if (max(x, y) >= facs[i]) {
                        if (x >= facs[i]) x -= facs[i];
                        else y -= facs[i];
                    }
                    else {
                        fg2 = 0;
                    }
                }
                if (max(x,y) >= (m - s) * facs[i] * facm[n - 1 - i]) {
                    if (x >= (m - s) * facs[i] * facm[n - 1 - i]) {
                        x -= (m - s) * facs[i] * facm[n - 1 - i];
                    }
                    else {
                        y-= (m - s) * facs[i] * facm[n - 1 - i];
                    }
                }
                else {
                    fg2 = 0;
                }
            }
            if (fg2&&x == 0 && y == 0) {
                fg = 1;
            }
        }
        if (fg) {
            cout << ans+1 << endl;
        }
        else {
            cout << -1 << endl;
        }
    }
    return 0;
}

/*
100
1000000000000000000 1 1000000000000000000 1
*/

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是制作背包的基本思路: 1. 创建一个背包界面,包含物品的图标、名称、数量等信息。 2. 创建一个物品类,用来存储每个物品的信息,包括编号、名称、图标、数量等信息。 3. 在游戏启动时读取text文件中的物品信息,将每个物品的信息存储到物品类中,并将物品类存储到一个数组中。 4. 点击背包界面中的物品图标时,根据物品编号在物品数组中查找对应的物品信息,并在背包界面中显示该物品的详细信息。 5. 可以在背包界面中拖拽物品图标进行交换位置,或者丢弃物品。 下面是一个示例代码,可以参考一下: ```csharp using UnityEngine; using UnityEngine.UI; using System.Collections.Generic; // 物品类 public class Item { public int id; // 物品编号 public string name; // 物品名称 public Sprite icon; // 物品图标 public int count; // 物品数量 public int price; // 物品价格 public int hp; // 恢复生命值 public int mp; // 恢复魔法值 // 从text文件中读取物品信息 public static Item Load(string line) { string[] data = line.Split(','); Item item = new Item(); item.id = int.Parse(data[0]); item.name = data[1]; item.icon = Resources.Load<Sprite>(data[2]); item.count = int.Parse(data[3]); item.price = int.Parse(data[4]); item.hp = int.Parse(data[5]); item.mp = int.Parse(data[6]); return item; } } // 背包界面 public class Backpack : MonoBehaviour { public Image itemIcon; // 物品图标 public Text itemName; // 物品名称 public Text itemCount; // 物品数量 public Text itemPrice; // 物品价格 public Text itemHp; // 恢复生命值 public Text itemMp; // 恢复魔法值 public Transform content; // 背包物品容器 private List<Item> items = new List<Item>(); // 物品列表 // 游戏启动时加载物品信息 private void Start() { TextAsset asset = Resources.Load<TextAsset>("items"); string[] lines = asset.text.Split('\n'); foreach (string line in lines) { Item item = Item.Load(line); items.Add(item); } } // 点击物品图标时显示该物品的详细信息 public void OnItemClick(int id) { Item item = items.Find(x => x.id == id); itemIcon.sprite = item.icon; itemName.text = item.name; itemCount.text = "数量:" + item.count; itemPrice.text = "价格:" + item.price; itemHp.text = "恢复生命值:" + item.hp; itemMp.text = "恢复魔法值:" + item.mp; } // 向背包中添加物品 public void AddItem(int id, int count) { Item item = items.Find(x => x.id == id); if (item != null) { item.count += count; UpdateItem(item); } } // 从背包中移除物品 public void RemoveItem(int id, int count) { Item item = items.Find(x => x.id == id); if (item != null) { item.count -= count; UpdateItem(item); } } // 更新物品信息 private void UpdateItem(Item item) { foreach (Transform child in content) { ItemSlot slot = child.GetComponent<ItemSlot>(); if (slot.itemId == item.id) { slot.UpdateItem(item); break; } } } } // 背包中的物品槽 public class ItemSlot : MonoBehaviour { public Image itemIcon; // 物品图标 public Text itemCount; // 物品数量 public int itemId; // 物品编号 public int itemAmount; // 物品数量 // 更新物品信息 public void UpdateItem(Item item) { itemId = item.id; itemAmount = item.count; itemIcon.sprite = item.icon; itemCount.text = item.count.ToString(); } } ``` 使用方法: 1. 创建一个UI界面,添加上述代码中的Backpack组件。 2. 创建一个TextAsset资源,命名为items,并将物品信息按照示例格式填入。 3. 在UI界面中添加一个ItemSlot预制体,用来显示每个物品的图标和数量。 4. 在UI界面中添加一个拖拽组件,用来实现物品的拖拽和交换。 5. 在代码中调用Backpack组件的AddItem和RemoveItem方法,可以向背包中添加或移除物品。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值