1. 程式人生 > 實用技巧 >GORM關聯查詢

GORM關聯查詢

B. Mask Allocation

B. Mask Allocation

題意

\(n \cdot m\)個口罩,

思路

程式碼

/*************************************************************************
 > FileName:
 > Author:      Lance
 > Mail:        [email protected]
 > Date:        9102.1.8
 > Description:
 ************************************************************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const double pi = acos(-1.0);
const double eps = 1e-6;
const int mod = 1e9 + 7;
#define debug(a) cout << "*" << a << "*" << endl
const int INF = 0x3f3f3f3f;//int2147483647//ll9e18//unsigned ll 1e19
const int maxn = 1000005;
//sacnf("%lf") printf("%f")
ll read() {
    ll x = 0,f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
        f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

int t, n, m, flag;
int dp[10005][10005];

int dfs(int m, int n) {
    if (n == 0) return 0;
    if (dp[m][n]) return dp[m][n];
    else return dp[m][n] = dfs(n, m % n) + m / n * n;
}

void print(int m, int n) {
    if (n == 0) return;
    int all = m / n * n;
    for (int i = 0; i < all; i++)
        if (flag == 0) {
            printf("%d", n);
            flag = 1;
        } else printf(" %d", n);
    print(n, m % n);
}
 
void solve() {
    scanf("%d", &t);
    while (t--) {
        flag = 0;
        n = read(), m = read();
        if (m < n) swap(m, n);
        ll s = m / n;
        ll yu = m % n;
        if (yu == 0) {
            printf("%d\n", m / n * n);
            for (int i = 0; i < m; i++)
                if (i == 0) printf("%d", n);
                else printf(" %d", n);
            puts("");
        } else {
            ll ans = dfs(m, n);
            printf("%lld\n", ans);     
            print(m, n);
            puts("");
        }
    }
}
 
int main() {
//    freopen("F:/Overflow/in.txt","r",stdin);
//    ios::sync_with_stdio(false);
    solve();
    return 0;
}

H. Dividing

H. Dividing

題意

思路

根據題目的意思,可以發現$(n, k) => $

程式碼

/*************************************************************************
 > FileName:
 > Author:      Lance
 > Mail:        [email protected]
 > Date:        9102.1.8
 > Description:
 ************************************************************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const double pi = acos(-1.0);
const double eps = 1e-6;
const int mod = 1e9 + 7;
#define debug(a) cout << "*" << a << "*" << endl
const int INF = 0x3f3f3f3f;//int2147483647//ll9e18//unsigned ll 1e19
const int maxn = 1000005;
//sacnf("%lf") printf("%f")
ll read()
{
    ll x = 0,f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
	{
		if (ch == '-')
		f = -1;
		ch = getchar();
	}
    while (ch >= '0' && ch <= '9')
	{
		x = x * 10 + ch - '0';
		ch = getchar();
	}
    return x * f;
}
ll t, n, N, K, ans = 0;

void solve()
{
	N = read(), K = read();
	// n是k的倍數 n=xk
	for (ll i = 2, j; i <= K; i = j + 1) {
		ll x = N / i;
		if (x == 0) break;
		j = N / x;
		ans = (ans + ((min(j, K) - i + 1) % mod * (x % mod))) % mod;
	}
	// n-1是k的倍數 n=xk+1
	for (ll i = 2, j; i <= K; i = j + 1) {
		ll x = (N - 1) / i;
		if (x == 0) break;
		j = (N - 1) / x;
		ans = (ans + ((min(j, K) - i + 1)) % mod * (x % mod)) % mod;
	}
	ans = (ans + N + K - 1) % mod;
	cout << ans << '\n';
}

int main()
{

//    freopen("F:/Overflow/in.txt","r",stdin);
//    ios::sync_with_stdio(false);
    solve();
    return 0;
}

J. Pointer Analysis

J. Pinter Analysis

題意

思路

程式碼


參考部落格