Saruman’s Level Up Gym - 101656G CSU 2069

题目链接: https://vjudge.net/problem/Gym-101656G // 也可在CSU 2069提交

题意: 给你一个十进制的数 二进制 111,也就是十进制的7表示第一个等级,问你所给的十进制是第几个等级

思路: 拿到题目首先想到的就是组合数,但是比赛的时候把这个过程想的太过复杂。我们假设这个数是n位二进制

1.其实就是考虑每一个1,假设它为最高位,这样的话 剩余的n-1位的范围就是 n-1个0 到 n-1个1,我们只需要用组合数C(3,n-1)

2.最高位肯定是1,下移的时候,我们要标记上次所用的1,保持上界的不变,也就是这个数不会溢出,这时候就用到了cnt变量,记录之前的1,后面的长度 只需要是 Len - i - 1,这个长度的表示应该是常识了。我们所需要排列的1 小于3的时候, 3 - cnt,超过3的时候 3 - cnt%3。依次遍历就好

3.这个想法会漏掉的情况就是,比如11111 会漏掉 11100这个位置,因为 到第三个1的时候 我们假设它为0 来枚举的,并没有考虑它自己为1,(因为它自己为1的话,是3的倍数,我们根据第二步的3 - cnt%3 得到3,不会考虑都为0的情况),且cnt正好等于3的倍数的情况,所以只需要在最后特判一下即可。1000表示为1111101000同理,会少两种情况。

4.涉及到大数的运算和转换为二进制的操作,并且将需要的数字转化为字符串,如果用Java会方便很多

#include <vector>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>


using namespace std;
typedef unsigned long long LL;
const int L = 110;


string mul(string a, string b) { //高精度乘法a,b,均为非负整数
    string s;
    int na[L], nb[L], nc[L], La = a.size(), Lb = b.size(); //na存储被乘数,nb存储乘数,nc存储积
    fill(na, na + L, 0);
    fill(nb, nb + L, 0);
    fill(nc, nc + L, 0); //将na,nb,nc都置为0
    for(int i = La - 1; i >= 0; i--) na[La - i] = a[i] - '0'; //将字符串表示的大整形数转成i整形数组表示的大整形数
    for(int i = Lb - 1; i >= 0; i--) nb[Lb - i] = b[i] - '0';
    for(int i = 1; i <= La; i++)
        for(int j = 1; j <= Lb; j++)
            nc[i + j - 1] += na[i] * nb[j]; //a的第i位乘以b的第j位为积的第i+j-1位(先不考虑进位)
    for(int i = 1; i <= La + Lb; i++)
        nc[i + 1] += nc[i] / 10, nc[i] %= 10; //统一处理进位
    if(nc[La + Lb]) s += nc[La + Lb] + '0'; //判断第i+j位上的数字是不是0
    for(int i = La + Lb - 1; i >= 1; i--)
        s += nc[i] + '0'; //将整形数组转成字符串
    return s;
}


string add(string a, string b) { //只限两个非负整数相加
    string ans;
    int na[L] = {0}, nb[L] = {0};
    int la = a.size(), lb = b.size();
    for(int i = 0; i < la; i++) na[la - 1 - i] = a[i] - '0';
    for(int i = 0; i < lb; i++) nb[lb - 1 - i] = b[i] - '0';
    int lmax = la > lb ? la : lb;
    for(int i = 0; i < lmax; i++) na[i] += nb[i], na[i + 1] += na[i] / 10, na[i] %= 10;
    if(na[lmax]) lmax++;
    for(int i = lmax - 1; i >= 0; i--) ans += na[i] + '0';
    return ans;
}


int sub(int *a, int *b, int La, int Lb) {
    if(La < Lb) return -1; //如果a小于b,则返回-1
    if(La == Lb) {
        for(int i = La - 1; i >= 0; i--)
            if(a[i] > b[i]) break;
            else if(a[i] < b[i]) return -1; //如果a小于b,则返回-1


    }
    for(int i = 0; i < La; i++) { //高精度减法
        a[i] -= b[i];
        if(a[i] < 0) a[i] += 10, a[i + 1]--;
    }
    for(int i = La - 1; i >= 0; i--)
        if(a[i]) return i + 1; //返回差的位数
    return 0;//返回差的位数


}


string div(string n1, string n2, int nn) { //n1,n2是字符串表示的被除数,除数,nn是选择返回商还是余数
    string s, v; //s存商,v存余数
    int a[L], b[L], r[L], La = n1.size(), Lb = n2.size(), i, tp = La; //a,b是整形数组表示被除数,除数,tp保存被除数的长度
    fill(a, a + L, 0);
    fill(b, b + L, 0);
    fill(r, r + L, 0); //数组元素都置为0
    for(i = La - 1; i >= 0; i--) a[La - 1 - i] = n1[i] - '0';
    for(i = Lb - 1; i >= 0; i--) b[Lb - 1 - i] = n2[i] - '0';
    if(La < Lb || (La == Lb && n1 < n2)) {
        //cout<<0<<endl;
        return n1;
    }//如果a<b,则商为0,余数为被除数
    int t = La - Lb; //除被数和除数的位数之差
    for(int i = La - 1; i >= 0; i--) //将除数扩大10^t倍
        if(i >= t) b[i] = b[i - t];
        else b[i] = 0;
    Lb = La;
    for(int j = 0; j <= t; j++) {
        int temp;
        while((temp = sub(a, b + j, La, Lb - j)) >= 0) { //如果被除数比除数大继续减
            La = temp;
            r[t - j]++;
        }
    }
    for(i = 0; i < L - 10; i++) r[i + 1] += r[i] / 10, r[i] %= 10; //统一处理进位
    while(!r[i]) i--;//将整形数组表示的商转化成字符串表示的
    while(i >= 0) s += r[i--] + '0';
    //cout<<s<<endl;
    i = tp;
    while(!a[i]) i--;//将整形数组表示的余数转化成字符串表示的</span>
    while(i >= 0) v += a[i--] + '0';
    if(v.empty()) v = "0";
    //cout<<v<<endl;
    if(nn == 1) return s;
    if(nn == 2) return v;
}


string num, sum;
LL cnt, n;


//将所给10进制转化为2进制
void trans(LL n) {
    while(n) {
        num.push_back(n % 2 + '0');
        n /= 2;
    }
    reverse(num.begin(), num.end());
} 


//将一些数据转为字符串进行大数运算
string trans1(LL n) {
    string a;
    while(n) {
        a.push_back(n % 10 + '0');
        n /= 10;
    }
    reverse(a.begin(), a.end());
    return a;
}


int main() {
//    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while(cin >> n) {
        sum = "0", num.clear();
        cnt = 0;
        trans(n);
        int len = num.length();
//        cout<<num<<endl;
        int a = 0;
        for(int i = 0; i < len; i++) {
            if(num[i] == '1') {
                LL l = len - i - 1,c = 0;
                c = 3 - cnt % 3;
//                cout<<c<<" "<<l<<endl;
                while(c  <= l) {
                    string s1 = "1", s2 = "1", s = "1";
                    for(LL i = l; i >= l - c + 1; i--) {
                        s1 = mul(s1, trans1(i));
                    }
                    for(LL i = c; i >= 1; i--) {
                        s2 = mul(s2, trans1(i));
                    }
                    s = div(s1, s2, 1);
                    sum = add(sum, s);
                    c += 3;
                }
                cnt++;
                if(cnt % 3 == 0) sum = add(sum,"1");
            }
        }
            cout << "Day " << n << ": Level = " << sum << endl;
    }
    return 0;
}

阅读更多

Level up

10-31

Problem DescriptionnZty has met a big problem.His X Gu Niang was kidnaped by a secret organization.The organization only left Zty a letter:n"If you can beat our boss ,we will give her the freedom,or..."nThe boss of the organization was so secret that no one knows his name.We only know that he was so powerful,and Zty is not powerful enough now,what he need to do is to train and to get more experience(Exp).Then he found a place wonderful for traning,There are N enemies there with different Exp,and Zty has M power ball,the number of power ball he used will effect the probability he beat the enemy.These probabilities are given as percentages pij, where i (with 1 ≤ i ≤ N) is the number of the enemy and j is the quantity of power balls used on it.One power ball can be used only once.nZty has to level up to 99,then he will be able to beat the boss.Of cause he is level 1 at the begining.He want to know weather the maximal expected Exp he can get is enough.The expected Exp is calculated as Sum(P(i)*Exp) where P is the probability.nThe Exp Zty need to level up one level is K/100 , and K will be given.nnNotice that: If Zty doesn't used a power ball,the probability he beat the enemy is 0. ^_^n nnInputnThe first line contain a T ,then T cases followed.Each test case has the following format:nOne line with one integer K <= 100000: as the description means.nOne line with one integer N with 1 ≤ N ≤ 100: the number of enemies.nOne line with one integer M with 0 ≤ M ≤ 100: the maximal number of available power ball.nOne line with N integers indicating the Exp of the N enemies. nN lines, each line corresponding to a enemy i, containing n integers pi1, pi2, …, pim (the percentages, with 0 ≤ pi1, pi2, …, pim ≤ 100).n nnOutputnIf the maximal expected Exp Zty can get is enough for him to level up tp 99,then ouput "Love you Ten thousand years.",else ouput"Cry,men,not crime."n nnSample Inputn2n1000n2n4n8 975n85 94 93 100n0 0 100 100n1000n1n4n979n0 0 0 100n nnSample OutputnLove you Ten thousand years.nCry,men,not crime.

没有更多推荐了,返回首页