首页 > 其他分享 >poj-1023

poj-1023

时间:2023-05-23 16:04:07浏览次数:28  
标签:1023 int curDigitPos NP poj long curLeftVal binaryExpression


//184K  0MS C++ 
#include <cstdio>
#include <cstring>

using namespace std;

char NP[65]; //-1: n, 1: p

char str[80];
char digitUsed[80];

char binaryExpression[80];

int caseNum;
int length;
long long val;

void solve(long long val) {

    long long curLeftVal = val;
    int curDigitPos = 1;
    while(1) {
        // printf("%lld\n", curLeftVal);
        if (curDigitPos == length) {
            if (curLeftVal != 0) {
                if (curLeftVal - NP[curDigitPos] != 0) {
                    printf("Impossible\n");
                    return;
                } else {
                    binaryExpression[curDigitPos] = 1;
                }
            } else if (curLeftVal == 0) {
                binaryExpression[curDigitPos] = 0;
            }
            break;
        }

        if (curLeftVal & 1) {
            curLeftVal -= NP[curDigitPos];
            binaryExpression[curDigitPos] = 1;
        } else {
            binaryExpression[curDigitPos] = 0;
        }
        curLeftVal >>= 1;
        curDigitPos++;
    }

    for (int i = length; i >= 1; i--) {
        printf("%d", binaryExpression[i]);
    }
    printf("\n");
}

int main() {
    scanf("%d", &caseNum);
    for (int i = 0; i < caseNum; i++) {
        memset(NP, 0xff, sizeof(NP));
        memset(digitUsed, 0, sizeof(digitUsed));
        memset(binaryExpression, 0, sizeof(binaryExpression));
        scanf("%d", &length);
        scanf("%s", str);
        for (int i = 0; i < length; i++) {
            if (str[i] == 'p') {
                NP[length - i] = 1;
            }
        }
        scanf("%lld", &val);
        solve(val);
    }
}



一道比较trick的题,不看答案,我估计只会暴搜.

具体是这么操作的:

对于某个数N,现在有K位可以用来表示,每一位可能是n或p,称这个数组为NP,

那么先从最后一位L(权值最小)开始,如果N是奇数的话,那么L这一位不管是p还是n,都必须是1,否则就不可能满足N是奇数的条件,

取了1之后,N根据p或n, -/+1,

如果N是偶数的话,L这一位就只能取0,否则不可能满足N是偶数的条件,这种情况下,N不变,

那么对于前K-1位,则必须能够表示 N/2(因为前一位被处理以后,剩下没有被表示的数是N, 将最后一位去掉以后,整个数组右移一位,这时候就是一个K-1位的数了,表示的值也要除以2,因此是K-1位表示N/2)这个数,否则就不能满足K位表示N这个数的条件,这样,就将问题的规模从(K位,N) 缩小为 (K-1位, N(偶数)N+/-1(N是奇数)/2)。

就这样处理,直到到了最高一位, 不可能再向高位扩展,这时候,这一位必须能够表示当前的N, 否则就impossible,

能表示的前提下,N只能是0, 1(最高一位是p) 或 -1(最高一位是n)。

这种办法只能强记了,我绝对想不出来的.

标签:1023,int,curDigitPos,NP,poj,long,curLeftVal,binaryExpression
From: https://blog.51cto.com/u_9420214/6332970

相关文章

  • poj-1401
    //408K375MSG++#include<cstdio>#include<cstring>longlongget2FactorNum(longlongN){longlongres=0;while(N){res+=N/2;N/=2;}returnres;}longlongget5FactorNum(longlongN){long......
  • poj-2231
    //264K 47MS C++#include<cstdio>#include<cstring>#include<cstdlib>constintMAX=10005;longlongcowLocation[10005];intcmp(constvoid*a,constvoid*b){ return*((longlong*)a)-*((longlong*)b);}longlongcowNum;......
  • poj-1308
    //392K0MSG++#include<cstdio>#include<cstring>usingnamespacestd;constintMAX=10000;intUF_set[MAX];voidUF_get_setId(intcurId){intparentId=UF_set[curId];if(parentId==0){return;}while(UF......
  • poj-1120
    //408K16MSG++#include<cstdio>#include<cstring>usingnamespacestd;intdish[20][20];intK[20][20];intD[16];intdays;intgetDensitySum(introwId,intcolumnId){intK=0;K+=dish[rowId][columnId];if(rowId......
  • poj-3641
    //712K0MSG++#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>usingnamespacestd;longlonga,p;//longlongpower2(longlonga,longlongn)//{//longlongret=1;//for(longlongm......
  • poj-1026
    //188K110MSC++#include<cstring>#include<cstdio>#include<iostream>usingnamespacestd;charstr1[205];charstr2[205];intkey[205];intcycleLength[205];//voidreplace(char*str,intkeyLength,intstrLength){//......
  • poj-2707
    //408K0MSG++#include<cstdio>#include<cstring>usingnamespacestd;intoX;intoY;intdX;intdY;inlinedoubleMIN(doublea,doubleb){returna<b?a:b;}inlinedoubleMAX(doublea,doubleb){returna>b?......
  • poj-2635
    //1652K875MSG++1000//1648K1313MSG++10000#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>constintMAX=1000100;charnotPrime[MAX+1];intPrimeNum;intPrimes[MAX];voidcheckPrim......
  • poj-3286
    //stupidmethod!!!!!!!!!!!!!!!//388K360MSG++#include<stdio.h>#include<string.h>#include<math.h>intC[33][33];//C[n][m],choosemfromn;voidgetCombination(){for(intn=0;n<=32;n++){for(intm=......
  • poj-2282
    //380K 32MS G++#include<stdio.h>#include<string.h>#include<math.h>longlongappearTime1[10];longlongappearTime2[10];voidgetAppearTime(intnum,longlong*appearArray){ appearArray[0]=1; if(num==0){ return; }......