2016沈阳赛区网络赛(1007,1009)

odd-even number(数位DP)

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 107 Accepted Submission(s): 53

Problem Description

For a number,if the length of continuous odd digits is even and the length of continuous even digits is odd ,we call it odd-even number.Now we want to know the amount of odd-even number between L,R(1<=L<=R<=9×1018).

Input

First line a t ,then t cases.every line contains two integers L and R.

Output

Print the output for each case on one line in the format as shown below.

Sample Input

2
1 100
110 220

Sample Output

Case #1: 29
Case #2: 36

Source
2016 ACM/ICPC Asia Regional Shenyang Online

题意

一个数字,它每个数位上的奇数都形成偶数长度的段,偶数位都形成奇数长度的段,他就是一个odd-even number,问 [L,R] 的这种数的个数

解题思路

裸的数位dp, 从高到低考虑每个数位, 状态里存下到当前位为止的值的奇偶性和长度奇偶性即可,一个个认真判断别粗心即可

代码

时间复杂度:就是一个简单的数位DP的复杂度

/*头文件模板代码*/

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <typeinfo>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

#define pb push_back
#define mp make_pair
#define mem(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define FIN freopen("input.txt", "r", stdin)
#define FOUT freopen("output.txt", "w", stdout)

typedef long long LL;
typedef pair<int, int > PII;
typedef pair<int, string> PIS;
typedef unsigned long long uLL;

template<typename T>
void print(T* p, T* q, string Gap = " ", bool flag = false) {
    int d = p < q ? 1 : -1;
    while(p != q) {
        if(flag) cout << Gap[0] << *p << Gap[1];
        else cout << *p;
        p += d;
        if(p != q && !flag) cout << Gap;
    }
    cout << endl;
}

template<typename T>
void print(const T &a, string bes = "") {
    int len = bes.length();
    if(len >= 2)cout << bes[0] << a << bes[1] << endl;
    else cout << a << endl;
}

void IO_Init() {
    ios::sync_with_stdio(false);
}


const double PI = 3.1415926535898;
const double eps = 1e-10;
const int MAXM = 1e4 + 5;
const int MAXN = 1e6 + 5;
const LL INF = 0x3f3f3f3f;

/*头文件模板代码*/

int bits[70];
LL DP[70][20][2];


LL dfs(int pos, int pre, int status, int limit) {
    if(pos == -1){
        if(pre == -1) return 0;
        else if((pre & 1) && (~ status & 1)) return 1;
        else if((~ pre & 1) && (status & 1)) return 1;
        else return 0;
    }


    if(pre != -1 && !limit && DP[pos][pre][status & 1] != -1) {
        return DP[pos][pre][status & 1];
    }

    int ends = limit ? bits[pos] : 9;
    LL ret = 0;


    for(int i = 0; i <= ends; i ++) {
        int npos = pos - 1;
        int npre = i;
        int nstatus = status + 1;
        int nlimit = (limit && i == ends) ? 1 : 0;

        if(pre != -1 && (status & 1) && (pre & 1) && (~ i & 1)) continue;
        if(pre != -1 && (~ status & 1) && (~ pre & 1) && (i & 1)) continue;

        if(pre == -1 && npre == 0){
             npre = -1;
             nstatus = 0;
        }

        if(pre != -1 && (pre & 1) && (~ i & 1)) nstatus = 1;
        if(pre != -1 && (~ pre & 1) && (i & 1)) nstatus = 1;
        ret += dfs(npos, npre, nstatus, nlimit);
    }

    if(!limit && pre != -1) {
        DP[pos][pre][status & 1] = ret;
    }
    return ret;
}

LL solve(LL c) {
    int len = 0;
    while(c > 0) {
        bits[len ++] = c % 10LL;
        c /= 10LL;
    }
    return dfs(len - 1, -1, 0, 1);
}


LL L, R;

int main() {
#ifndef ONLINE_JUDGE
    //FIN;
    //FOUT;
#endif
    IO_Init();
    int _;
    int cas = 1;
    scanf("%d", &_);
    mem(DP, -1);
    while(_ --) {
        scanf("%I64d%I64d", &L, &R);
        printf("Case #%d: %I64d\n", cas ++, solve(R) - solve(L - 1));
    }
    return 0;
}

QSC and Master

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 228 Accepted Submission(s): 80

Problem Description

Every school has some legends, Northeastern University is the same.

Enter from the north gate of Northeastern University,You are facing the main building of Northeastern University.Ninety-nine percent of the students have not been there,It is said that there is a monster in it.

QSCI am a curious NEU_ACMer,This is the story he told us.

It’s a certain period,QSCI am in a dark night, secretly sneaked into the East Building,hope to see the master.After a serious search,He finally saw the little master in a dark corner. The master said:

“You and I, we’re interfacing.please solve my little puzzle!

There are N pairs of numbers,Each pair consists of a key and a value,Now you need to move out some of the pairs to get the score.You can move out two continuous pairs,if and only if their keys are non coprime(their gcd is not one).The final score you get is the sum of all pair’s value which be moved out. May I ask how many points you can get the most?

The answer you give is directly related to your final exam results~The young man~”

QSC is very sad when he told the story,He failed his linear algebra that year because he didn’t work out the puzzle.

Could you solve this puzzle?

(Data range:1<=N<=300
1<=Ai.key<=1,000,000,000
0<Ai.value<=1,000,000,000

Input

First line contains a integer T ,means there are T(1T10) test case。

Each test case start with one integer N . Next line contains N integers,means Ai.key.Next line contains N integers,means Ai.value.

Output

For each test case,output the max score you could get in a line.

Sample Input

3
3
1 2 3
1 1 1
3
1 2 4
1 1 1
4
1 3 4 3
1 1 1 1

Sample Output

0
2
0

Source

2016 ACM/ICPC Asia Regional Shenyang Online

题意

n pair<int,int>,每次可以选相邻两个 pair 。如果他们的 first 不互质就可以把它们都删掉,并且获得 second 之和的分数,问可能最大得分。

解题思路(区间DP)

dp[i][j] [i,j] 这段区间能取得的最大得分,转移就是不取左边/不取右边/断开拼起来/取两边,其中取两边需要中间能取干净才能取

代码

时间复杂度: O(n3)

/*头文件模板代码*/

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <typeinfo>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

#define pb push_back
#define mp make_pair
#define mem(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define FIN freopen("input.txt", "r", stdin)
#define FOUT freopen("output.txt", "w", stdout)

typedef long long LL;
typedef pair<int, int > PII;
typedef pair<int, string> PIS;
typedef unsigned long long uLL;

template<typename T>
void print(T* p, T* q, string Gap = " ", bool flag = false) {
    int d = p < q ? 1 : -1;
    while(p != q) {
        if(flag) cout << Gap[0] << *p << Gap[1];
        else cout << *p;
        p += d;
        if(p != q && !flag) cout << Gap;
    }
    cout << endl;
}

template<typename T>
void print(const T &a, string bes = "") {
    int len = bes.length();
    if(len >= 2)cout << bes[0] << a << bes[1] << endl;
    else cout << a << endl;
}

void IO_Init() {
    ios::sync_with_stdio(false);
}


const double PI = 3.1415926535898;
const double eps = 1e-10;
const int MAXM = 1e4 + 5;
const int MAXN = 3e2 + 5;
const LL INF = 0x3f3f3f3f;

/*头文件模板代码*/

struct d {
    LL k, v;
} D[MAXN];

LL dp[MAXN][MAXN], N;
LL DPT[MAXN];

LL gcd(LL a, LL b) {
    return b ? gcd(b, a % b) : a;
}

int main() {
#ifndef ONLINE_JUDGE
    //FIN;
    //FOUT;
#endif
    IO_Init();

    int _;
    scanf("%d", &_);
    while(_ --) {
        mem(dp, -1);
        scanf("%d", &N);
        for(int i = 1; i <= N; i ++) {
            scanf("%I64d", &D[i].k);
        }
        for(int i = 1; i <= N; i ++) {
            scanf("%I64d", &D[i].v);
        }

        for(int i = 2; i <= N; i ++) {
            if(gcd(D[i].k, D[i - 1].k) != 1) {
                dp[i - 1][i] = D[i - 1].v + D[i].v;
            }
        }

        for(int i = 2; i <= N; i += 2) {
            for(int j = 1; j + i - 1<= N; j ++) {
                for(int k = j + 1; k - j + 1 <= i; k += 2) {//求解一个区间中的最大
                    if(dp[j][k] != -1 && dp[k + 1][j + i - 1] != -1) {
                        dp[j][j + i - 1] = max(dp[j][j + i - 1], dp[j][k] + dp[k + 1][j + i - 1]);
                    }
                }
                if(j > 1 && j + i <= N && gcd(D[j - 1].k, D[j + i].k) != 1 && dp[j][j + i - 1] != -1) {//从已知区间向外再递推一个长度为2的区间,就是合并区间的左边界和右边界
                    dp[j - 1][j + i] = max(dp[j - 1][j + i], dp[j][j + i - 1] + D[j - 1].v + D[j + i].v);
                }
            }
        }

        LL ret = 0;
        mem(DPT, 0);
        for(int i = 1; i <= N; i ++) {
            DPT[i] = max(dp[1][i], DPT[i]);
            for(int j = 1; j < i; j ++) {
                DPT[i] = max(DPT[i], DPT[j] + dp[j + 1][i]);
                DPT[i] = max(DPT[i], DPT[j]);
            }
            ret = max(DPT[i], ret);
        }
        printf("%I64d\n", ret);
    }

    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值