日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 综合教程 >内容正文

综合教程

SPOJ

發(fā)布時(shí)間:2024/8/26 综合教程 25 生活家
生活随笔 收集整理的這篇文章主要介紹了 SPOJ 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

鏈接:

https://vjudge.net/problem/SPOJ-BALNUM

題意:

Balanced numbers have been used by mathematicians for centuries. A positive integer is considered a balanced number if:

 Every even digit appears an odd number of times in its decimal representation
 Every odd digit appears an even number of times in its decimal representation

For example, 77, 211, 6222 and 112334445555677 are balanced numbers while 351, 21, and 662 are not.

Given an interval [A, B], your task is to find the amount of balanced numbers in [A, B] where both A and B are included.

思路:

三進(jìn)制記錄每個(gè)值用的奇數(shù)次還是偶數(shù)次。
直接DP即可。

代碼:

// #include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<set>
#include<queue>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int MOD = 1e9+7;
const int MAXN = 1e6+10;

ULL a, b;
ULL F[21][60000];
int dig[21];
ULL m[11];

int Upd(int x, int p)
{
    int sum = 0;
    for (int i = 0;i < 10;i++)
    {
        int tmp = x%3;
        x /= 3;
        if (i == p)
            sum += (tmp == 1 ? 2 : 1) * m[i];
        else
            sum += tmp * m[i];
    }
    return sum;
}

bool Check(int x)
{
    int p = 0;
    while(x)
    {
        if (x%3 == 2 && p%2 == 0)
            return false;
        if (x%3 == 1 && p%2 == 1)
            return false;
        x /= 3;
        p++;
    }
    return true;
}

ULL Dfs(int pos, int sta, bool zer, bool lim)
{
    if (pos == -1)
        return Check(sta);
    if (!lim && F[pos][sta] != -1)
        return F[pos][sta];
    int up = lim ? dig[pos] : 9;
    ULL ans = 0;
    for (int i = 0;i <= up;i++)
    {
        ans += Dfs(pos-1, (zer && i == 0) ? 0 : Upd(sta, i), zer && i == 0, lim && i == up);
    }
    if (!lim)
        F[pos][sta] = ans;
    return ans;
}

ULL Solve(ULL x)
{
    int p = 0;
    while(x)
    {
        dig[p++] = x%10;
        x /= 10;
    }
    return Dfs(p-1, 0, 1, 1);
}

int main()
{
    // freopen("test.in", "r", stdin);
    m[0] = 1;
    for (int i = 1;i < 11;i++)
        m[i] = m[i-1]*3;
    memset(F, -1, sizeof(F));
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%llu %llu", &a, &b);
        printf("%llu
", Solve(b)-Solve(a-1));
    }

    return 0;
}

總結(jié)

以上是生活随笔為你收集整理的SPOJ的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。