Codeforces之Goodbye 2015

A. New Year and Days

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Today is Wednesday, the third day of the week. What's more interesting is that tomorrow is the last day of the year 2015.

Limak is a little polar bear. He enjoyed this year a lot. Now, he is so eager to the coming year 2016.

Limak wants to prove how responsible a bear he is. He is going to regularly save candies for the entire year 2016! He considers various saving plans. He can save one candy either on some fixed day of the week or on some fixed day of the month.

Limak chose one particular plan. He isn't sure how many candies he will save in the 2016 with his plan. Please, calculate it and tell him.

Input

The only line of the input is in one of the following two formats:

  • "x of week" where x (1 ≤ x ≤ 7) denotes the day of the week. The 1-st day is Monday and the 7-th one is Sunday.
  • "x of month" where x (1 ≤ x ≤ 31) denotes the day of the month.
Output

Print one integer — the number of candies Limak will save in the year 2016.

Sample test(s)
input
4 of week
output
52
input
30 of month
output
11
Note

Polar bears use the Gregorian calendar. It is the most common calendar and you likely use it too. You can read about it on Wikipedia if you want to – https://en.wikipedia.org/wiki/Gregorian_calendar. The week starts with Monday.

In the first sample Limak wants to save one candy on each Thursday (the 4-th day of the week). There are 52 Thursdays in the 2016. Thus, he will save 52 candies in total.

In the second sample Limak wants to save one candy on the 30-th day of each month. There is the 30-th day in exactly 11 months in the 2016 — all months but February. It means that Limak will save 11 candies in total.

解析: 计算2016年中有多少个星期一...星期日?或者计算2016年有多少个月有指定的日期?

思路:翻开日历自己看看就行,好久没有写博客了,糜烂了。

CODE:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    string s1,s2;
    while(cin>>n){
        cin>>s1>>s2;
        if(s2=="week"){
            if(n==5||n==6){
                cout<<53<<endl;
            }
            else{
                cout<<52<<endl;
            }
        }
        else{
            if(n<30){
                cout<<12<<endl;
            }
            else if(n==30){
                cout<<11<<endl;
            }
            else{
                cout<<7<<endl;
            }
        }
    }
    return 0;
}

B. New Year and Old Property
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The year 2015 is almost over.

Limak is a little polar bear. He has recently learnt about the binary system. He noticed that the passing year has exactly one zero in its representation in the binary system — 201510 = 111110111112. Note that he doesn't care about the number of zeros in the decimal representation.

Limak chose some interval of years. He is going to count all years from this interval that have exactly one zero in the binary representation. Can you do it faster?

Assume that all positive integers are always written without leading zeros.

Input

The only line of the input contains two integers a and b (1 ≤ a ≤ b ≤ 1018) — the first year and the last year in Limak's interval respectively.

Output

Print one integer – the number of years Limak will count in his chosen interval.

Sample test(s)
input
5 10
output
2
input
2015 2015
output
1
input
100 105
output
0
input
72057594000000000 72057595000000000
output
26
Note

In the first sample Limak's interval contains numbers 510 = 1012610 = 1102710 = 1112810 = 10002910 = 10012 and1010 = 10102. Two of them (1012 and 1102) have the described property.


解析:给2个数,计算在这范围内有多少个数满足:一个数的二进制表示时,没有前导0,恰好只包含一个数字0,其余都是1.

思路:long long类型小于为2的64次方,则可以暴力计算。计算所有满足条件的数字,然后判断是否在a,b范围内。例如:先计算出二进制数字全为1的,如11111,方法是1向左移位5,得100000,然后减1即可。然后分别用1,10,100,1000作为减数,计算出数字11110,11101,11011,10111.都是满足条件的数字。

CODE:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    long long a,b,ans;
    while(cin>>a>>b){
        ans=0;
        for(int i=2;i<64;i++){
            long long x=(1ll<<i)-1;
            for(int j=0;j<i-1;j++){
                long long y=x-(1ll<<j);
                if(a<=y&&y<=b) ans++;
            }
		}
		cout<<ans<<endl;
	}
    return 0;
}

CODE:(T神代码)

#include <bits/stdc++.h>

using namespace std;

long long a, b;
long long ans;

void dfs(long long x, int cnt) {
  if (x > b) {
    return;
  }
  if (a <= x && x <= b && cnt == 1) {
    ans++;
  }
  if (cnt == 0) {
    dfs(x * 2, cnt + 1);
  }
  dfs(x * 2 + 1, cnt);
}

int main() {
  cin >> a >> b;
  ans = 0;
  dfs(1, 0);
  cout << ans << endl;
  return 0;
}

CODE:(牛人代码)
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int a[]={2,6,14,30,62,126,254,510,1022,2046,4094,8190,16382,32766,65534,131070,262142,524286,1048574,2097150,4194302,8388606,16777214,33554430,67108862,134217726,268435454,536870910,1073741822,2147483646,4294967294,8589934590,17179869182,34359738366,68719476734,137438953470,274877906942,549755813886,1099511627774,2199023255550,4398046511102,8796093022206,17592186044414,35184372088830,70368744177662,140737488355326,281474976710654,562949953421310,1125899906842622,2251799813685246,4503599627370494,9007199254740990,18014398509481982,36028797018963966,72057594037927934,144115188075855870,288230376151711742,576460752303423486,1152921504606846974,5,13,29,61,125,253,509,1021,2045,4093,8189,16381,32765,65533,131069,262141,524285,1048573,2097149,4194301,8388605,16777213,33554429,67108861,134217725,268435453,536870909,1073741821,2147483645,4294967293,8589934589,17179869181,34359738365,68719476733,137438953469,274877906941,549755813885,1099511627773,2199023255549,4398046511101,8796093022205,17592186044413,35184372088829,70368744177661,140737488355325,281474976710653,562949953421309,1125899906842621,2251799813685245,4503599627370493,9007199254740989,18014398509481981,36028797018963965,72057594037927933,144115188075855869,288230376151711741,576460752303423485,1152921504606846973,11,27,59,123,251,507,1019,2043,4091,8187,16379,32763,65531,131067,262139,524283,1048571,2097147,4194299,8388603,16777211,33554427,67108859,134217723,268435451,536870907,1073741819,2147483643,4294967291,8589934587,17179869179,34359738363,68719476731,137438953467,274877906939,549755813883,1099511627771,2199023255547,4398046511099,8796093022203,17592186044411,35184372088827,70368744177659,140737488355323,281474976710651,562949953421307,1125899906842619,2251799813685243,4503599627370491,9007199254740987,18014398509481979,36028797018963963,72057594037927931,144115188075855867,288230376151711739,576460752303423483,1152921504606846971,23,55,119,247,503,1015,2039,4087,8183,16375,32759,65527,131063,262135,524279,1048567,2097143,4194295,8388599,16777207,33554423,67108855,134217719,268435447,536870903,1073741815,2147483639,4294967287,8589934583,17179869175,34359738359,68719476727,137438953463,274877906935,549755813879,1099511627767,2199023255543,4398046511095,8796093022199,17592186044407,35184372088823,70368744177655,140737488355319,281474976710647,562949953421303,1125899906842615,2251799813685239,4503599627370487,9007199254740983,18014398509481975,36028797018963959,72057594037927927,144115188075855863,288230376151711735,576460752303423479,1152921504606846967,47,111,239,495,1007,2031,4079,8175,16367,32751,65519,131055,262127,524271,1048559,2097135,4194287,8388591,16777199,33554415,67108847,134217711,268435439,536870895,1073741807,2147483631,4294967279,8589934575,17179869167,34359738351,68719476719,137438953455,274877906927,549755813871,1099511627759,2199023255535,4398046511087,8796093022191,17592186044399,35184372088815,70368744177647,140737488355311,281474976710639,562949953421295,1125899906842607,2251799813685231,4503599627370479,9007199254740975,18014398509481967,36028797018963951,72057594037927919,144115188075855855,288230376151711727,576460752303423471,1152921504606846959,95,223,479,991,2015,4063,8159,16351,32735,65503,131039,262111,524255,1048543,2097119,4194271,8388575,16777183,33554399,67108831,134217695,268435423,536870879,1073741791,2147483615,4294967263,8589934559,17179869151,34359738335,68719476703,137438953439,274877906911,549755813855,1099511627743,2199023255519,4398046511071,8796093022175,17592186044383,35184372088799,70368744177631,140737488355295,281474976710623,562949953421279,1125899906842591,2251799813685215,4503599627370463,9007199254740959,18014398509481951,36028797018963935,72057594037927903,144115188075855839,288230376151711711,576460752303423455,1152921504606846943,191,447,959,1983,4031,8127,16319,32703,65471,131007,262079,524223,1048511,2097087,4194239,8388543,16777151,33554367,67108799,134217663,268435391,536870847,1073741759,2147483583,4294967231,8589934527,17179869119,34359738303,68719476671,137438953407,274877906879,549755813823,1099511627711,2199023255487,4398046511039,8796093022143,17592186044351,35184372088767,70368744177599,140737488355263,281474976710591,562949953421247,1125899906842559,2251799813685183,4503599627370431,9007199254740927,18014398509481919,36028797018963903,72057594037927871,144115188075855807,288230376151711679,576460752303423423,1152921504606846911,383,895,1919,3967,8063,16255,32639,65407,130943,262015,524159,1048447,2097023,4194175,8388479,16777087,33554303,67108735,134217599,268435327,536870783,1073741695,2147483519,4294967167,8589934463,17179869055,34359738239,68719476607,137438953343,274877906815,549755813759,1099511627647,2199023255423,4398046510975,8796093022079,17592186044287,35184372088703,70368744177535,140737488355199,281474976710527,562949953421183,1125899906842495,2251799813685119,4503599627370367,9007199254740863,18014398509481855,36028797018963839,72057594037927807,144115188075855743,288230376151711615,576460752303423359,1152921504606846847,767,1791,3839,7935,16127,32511,65279,130815,261887,524031,1048319,2096895,4194047,8388351,16776959,33554175,67108607,134217471,268435199,536870655,1073741567,2147483391,4294967039,8589934335,17179868927,34359738111,68719476479,137438953215,274877906687,549755813631,1099511627519,2199023255295,4398046510847,8796093021951,17592186044159,35184372088575,70368744177407,140737488355071,281474976710399,562949953421055,1125899906842367,2251799813684991,4503599627370239,9007199254740735,18014398509481727,36028797018963711,72057594037927679,144115188075855615,288230376151711487,576460752303423231,1152921504606846719,1535,3583,7679,15871,32255,65023,130559,261631,523775,1048063,2096639,4193791,8388095,16776703,33553919,67108351,134217215,268434943,536870399,1073741311,2147483135,4294966783,8589934079,17179868671,34359737855,68719476223,137438952959,274877906431,549755813375,1099511627263,2199023255039,4398046510591,8796093021695,17592186043903,35184372088319,70368744177151,140737488354815,281474976710143,562949953420799,1125899906842111,2251799813684735,4503599627369983,9007199254740479,18014398509481471,36028797018963455,72057594037927423,144115188075855359,288230376151711231,576460752303422975,1152921504606846463,3071,7167,15359,31743,64511,130047,261119,523263,1047551,2096127,4193279,8387583,16776191,33553407,67107839,134216703,268434431,536869887,1073740799,2147482623,4294966271,8589933567,17179868159,34359737343,68719475711,137438952447,274877905919,549755812863,1099511626751,2199023254527,4398046510079,8796093021183,17592186043391,35184372087807,70368744176639,140737488354303,281474976709631,562949953420287,1125899906841599,2251799813684223,4503599627369471,9007199254739967,18014398509480959,36028797018962943,72057594037926911,144115188075854847,288230376151710719,576460752303422463,1152921504606845951,6143,14335,30719,63487,129023,260095,522239,1046527,2095103,4192255,8386559,16775167,33552383,67106815,134215679,268433407,536868863,1073739775,2147481599,4294965247,8589932543,17179867135,34359736319,68719474687,137438951423,274877904895,549755811839,1099511625727,2199023253503,4398046509055,8796093020159,17592186042367,35184372086783,70368744175615,140737488353279,281474976708607,562949953419263,1125899906840575,2251799813683199,4503599627368447,9007199254738943,18014398509479935,36028797018961919,72057594037925887,144115188075853823,288230376151709695,576460752303421439,1152921504606844927,12287,28671,61439,126975,258047,520191,1044479,2093055,4190207,8384511,16773119,33550335,67104767,134213631,268431359,536866815,1073737727,2147479551,4294963199,8589930495,17179865087,34359734271,68719472639,137438949375,274877902847,549755809791,1099511623679,2199023251455,4398046507007,8796093018111,17592186040319,35184372084735,70368744173567,140737488351231,281474976706559,562949953417215,1125899906838527,2251799813681151,4503599627366399,9007199254736895,18014398509477887,36028797018959871,72057594037923839,144115188075851775,288230376151707647,576460752303419391,1152921504606842879,24575,57343,122879,253951,516095,1040383,2088959,4186111,8380415,16769023,33546239,67100671,134209535,268427263,536862719,1073733631,2147475455,4294959103,8589926399,17179860991,34359730175,68719468543,137438945279,274877898751,549755805695,1099511619583,2199023247359,4398046502911,8796093014015,17592186036223,35184372080639,70368744169471,140737488347135,281474976702463,562949953413119,1125899906834431,2251799813677055,4503599627362303,9007199254732799,18014398509473791,36028797018955775,72057594037919743,144115188075847679,288230376151703551,576460752303415295,1152921504606838783,49151,114687,245759,507903,1032191,2080767,4177919,8372223,16760831,33538047,67092479,134201343,268419071,536854527,1073725439,2147467263,4294950911,8589918207,17179852799,34359721983,68719460351,137438937087,274877890559,549755797503,1099511611391,2199023239167,4398046494719,8796093005823,17592186028031,35184372072447,70368744161279,140737488338943,281474976694271,562949953404927,1125899906826239,2251799813668863,4503599627354111,9007199254724607,18014398509465599,36028797018947583,72057594037911551,144115188075839487,288230376151695359,576460752303407103,1152921504606830591,98303,229375,491519,1015807,2064383,4161535,8355839,16744447,33521663,67076095,134184959,268402687,536838143,1073709055,2147450879,4294934527,8589901823,17179836415,34359705599,68719443967,137438920703,274877874175,549755781119,1099511595007,2199023222783,4398046478335,8796092989439,17592186011647,35184372056063,70368744144895,140737488322559,281474976677887,562949953388543,1125899906809855,2251799813652479,4503599627337727,9007199254708223,18014398509449215,36028797018931199,72057594037895167,144115188075823103,288230376151678975,576460752303390719,1152921504606814207,196607,458751,983039,2031615,4128767,8323071,16711679,33488895,67043327,134152191,268369919,536805375,1073676287,2147418111,4294901759,8589869055,17179803647,34359672831,68719411199,137438887935,274877841407,549755748351,1099511562239,2199023190015,4398046445567,8796092956671,17592185978879,35184372023295,70368744112127,140737488289791,281474976645119,562949953355775,1125899906777087,2251799813619711,4503599627304959,9007199254675455,18014398509416447,36028797018898431,72057594037862399,144115188075790335,288230376151646207,576460752303357951,1152921504606781439,393215,917503,1966079,4063231,8257535,16646143,33423359,66977791,134086655,268304383,536739839,1073610751,2147352575,4294836223,8589803519,17179738111,34359607295,68719345663,137438822399,274877775871,549755682815,1099511496703,2199023124479,4398046380031,8796092891135,17592185913343,35184371957759,70368744046591,140737488224255,281474976579583,562949953290239,1125899906711551,2251799813554175,4503599627239423,9007199254609919,18014398509350911,36028797018832895,72057594037796863,144115188075724799,288230376151580671,576460752303292415,1152921504606715903,786431,1835007,3932159,8126463,16515071,33292287,66846719,133955583,268173311,536608767,1073479679,2147221503,4294705151,8589672447,17179607039,34359476223,68719214591,137438691327,274877644799,549755551743,1099511365631,2199022993407,4398046248959,8796092760063,17592185782271,35184371826687,70368743915519,140737488093183,281474976448511,562949953159167,1125899906580479,2251799813423103,4503599627108351,9007199254478847,18014398509219839,36028797018701823,72057594037665791,144115188075593727,288230376151449599,576460752303161343,1152921504606584831,1572863,3670015,7864319,16252927,33030143,66584575,133693439,267911167,536346623,1073217535,2146959359,4294443007,8589410303,17179344895,34359214079,68718952447,137438429183,274877382655,549755289599,1099511103487,2199022731263,4398045986815,8796092497919,17592185520127,35184371564543,70368743653375,140737487831039,281474976186367,562949952897023,1125899906318335,2251799813160959,4503599626846207,9007199254216703,18014398508957695,36028797018439679,72057594037403647,144115188075331583,288230376151187455,576460752302899199,1152921504606322687,3145727,7340031,15728639,32505855,66060287,133169151,267386879,535822335,1072693247,2146435071,4293918719,8588886015,17178820607,34358689791,68718428159,137437904895,274876858367,549754765311,1099510579199,2199022206975,4398045462527,8796091973631,17592184995839,35184371040255,70368743129087,140737487306751,281474975662079,562949952372735,1125899905794047,2251799812636671,4503599626321919,9007199253692415,18014398508433407,36028797017915391,72057594036879359,144115188074807295,288230376150663167,576460752302374911,1152921504605798399,6291455,14680063,31457279,65011711,132120575,266338303,534773759,1071644671,2145386495,4292870143,8587837439,17177772031,34357641215,68717379583,137436856319,274875809791,549753716735,1099509530623,2199021158399,4398044413951,8796090925055,17592183947263,35184369991679,70368742080511,140737486258175,281474974613503,562949951324159,1125899904745471,2251799811588095,4503599625273343,9007199252643839,18014398507384831,36028797016866815,72057594035830783,144115188073758719,288230376149614591,576460752301326335,1152921504604749823,12582911,29360127,62914559,130023423,264241151,532676607,1069547519,2143289343,4290772991,8585740287,17175674879,34355544063,68715282431,137434759167,274873712639,549751619583,1099507433471,2199019061247,4398042316799,8796088827903,17592181850111,35184367894527,70368739983359,140737484161023,281474972516351,562949949227007,1125899902648319,2251799809490943,4503599623176191,9007199250546687,18014398505287679,36028797014769663,72057594033733631,144115188071661567,288230376147517439,576460752299229183,1152921504602652671,25165823,58720255,125829119,260046847,528482303,1065353215,2139095039,4286578687,8581545983,17171480575,34351349759,68711088127,137430564863,274869518335,549747425279,1099503239167,2199014866943,4398038122495,8796084633599,17592177655807,35184363700223,70368735789055,140737479966719,281474968322047,562949945032703,1125899898454015,2251799805296639,4503599618981887,9007199246352383,18014398501093375,36028797010575359,72057594029539327,144115188067467263,288230376143323135,576460752295034879,1152921504598458367,50331647,117440511,251658239,520093695,1056964607,2130706431,4278190079,8573157375,17163091967,34342961151,68702699519,137422176255,274861129727,549739036671,1099494850559,2199006478335,4398029733887,8796076244991,17592169267199,35184355311615,70368727400447,140737471578111,281474959933439,562949936644095,1125899890065407,2251799796908031,4503599610593279,9007199237963775,18014398492704767,36028797002186751,72057594021150719,144115188059078655,288230376134934527,576460752286646271,1152921504590069759,100663295,234881023,503316479,1040187391,2113929215,4261412863,8556380159,17146314751,34326183935,68685922303,137405399039,274844352511,549722259455,1099478073343,2198989701119,4398012956671,8796059467775,17592152489983,35184338534399,70368710623231,140737454800895,281474943156223,562949919866879,1125899873288191,2251799780130815,4503599593816063,9007199221186559,18014398475927551,36028796985409535,72057594004373503,144115188042301439,288230376118157311,576460752269869055,1152921504573292543,201326591,469762047,1006632959,2080374783,4227858431,8522825727,17112760319,34292629503,68652367871,137371844607,274810798079,549688705023,1099444518911,2198956146687,4397979402239,8796025913343,17592118935551,35184304979967,70368677068799,140737421246463,281474909601791,562949886312447,1125899839733759,2251799746576383,4503599560261631,9007199187632127,18014398442373119,36028796951855103,72057593970819071,144115188008747007,288230376084602879,576460752236314623,1152921504539738111,402653183,939524095,2013265919,4160749567,8455716863,17045651455,34225520639,68585259007,137304735743,274743689215,549621596159,1099377410047,2198889037823,4397912293375,8795958804479,17592051826687,35184237871103,70368609959935,140737354137599,281474842492927,562949819203583,1125899772624895,2251799679467519,4503599493152767,9007199120523263,18014398375264255,36028796884746239,72057593903710207,144115187941638143,288230376017494015,576460752169205759,1152921504472629247,805306367,1879048191,4026531839,8321499135,16911433727,34091302911,68451041279,137170518015,274609471487,549487378431,1099243192319,2198754820095,4397778075647,8795824586751,17591917608959,35184103653375,70368475742207,140737219919871,281474708275199,562949684985855,1125899638407167,2251799545249791,4503599358935039,9007198986305535,18014398241046527,36028796750528511,72057593769492479,144115187807420415,288230375883276287,576460752034988031,1152921504338411519,1610612735,3758096383,8053063679,16642998271,33822867455,68182605823,136902082559,274341036031,549218942975,1098974756863,2198486384639,4397509640191,8795556151295,17591649173503,35183835217919,70368207306751,140736951484415,281474439839743,562949416550399,1125899369971711,2251799276814335,4503599090499583,9007198717870079,18014397972611071,36028796482093055,72057593501057023,144115187538984959,288230375614840831,576460751766552575,1152921504069976063,3221225471,7516192767,16106127359,33285996543,67645734911,136365211647,273804165119,548682072063,1098437885951,2197949513727,4396972769279,8795019280383,17591112302591,35183298347007,70367670435839,140736414613503,281473902968831,562948879679487,1125898833100799,2251798739943423,4503598553628671,9007198180999167,18014397435740159,36028795945222143,72057592964186111,144115187002114047,288230375077969919,576460751229681663,1152921503533105151,6442450943,15032385535,32212254719,66571993087,135291469823,272730423295,547608330239,1097364144127,2196875771903,4395899027455,8793945538559,17590038560767,35182224605183,70366596694015,140735340871679,281472829227007,562947805937663,1125897759358975,2251797666201599,4503597479886847,9007197107257343,18014396361998335,36028794871480319,72057591890444287,144115185928372223,288230374004228095,576460750155939839,1152921502459363327,12884901887,30064771071,64424509439,133143986175,270582939647,545460846591,1095216660479,2194728288255,4393751543807,8791798054911,17587891077119,35180077121535,70364449210367,140733193388031,281470681743359,562945658454015,1125895611875327,2251795518717951,4503595332403199,9007194959773695,18014394214514687,36028792723996671,72057589742960639,144115183780888575,288230371856744447,576460748008456191,1152921500311879679,25769803775,60129542143,128849018879,266287972351,541165879295,1090921693183,2190433320959,4389456576511,8787503087615,17583596109823,35175782154239,70360154243071,140728898420735,281466386776063,562941363486719,1125891316908031,2251791223750655,4503591037435903,9007190664806399,18014389919547391,36028788429029375,72057585447993343,144115179485921279,288230367561777151,576460743713488895,1152921496016912383,51539607551,120259084287,257698037759,532575944703,1082331758591,2181843386367,4380866641919,8778913153023,17575006175231,35167192219647,70351564308479,140720308486143,281457796841471,562932773552127,1125882726973439,2251782633816063,4503582447501311,9007182074871807,18014381329612799,36028779839094783,72057576858058751,144115170895986687,288230358971842559,576460735123554303,1152921487426977791,103079215103,240518168575,515396075519,1065151889407,2164663517183,4363686772735,8761733283839,17557826306047,35150012350463,70334384439295,140703128616959,281440616972287,562915593682943,1125865547104255,2251765453946879,4503565267632127,9007164895002623,18014364149743615,36028762659225599,72057559678189567,144115153716117503,288230341791973375,576460717943685119,1152921470247108607,206158430207,481036337151,1030792151039,2130303778815,4329327034367,8727373545471,17523466567679,35115652612095,70300024700927,140668768878591,281406257233919,562881233944575,1125831187365887,2251731094208511,4503530907893759,9007130535264255,18014329790005247,36028728299487231,72057525318451199,144115119356379135,288230307432235007,576460683583946751,1152921435887370239,412316860415,962072674303,2061584302079,4260607557631,8658654068735,17454747090943,35046933135359,70231305224191,140600049401855,281337537757183,562812514467839,1125762467889151,2251662374731775,4503462188417023,9007061815787519,18014261070528511,36028659580010495,72057456598974463,144115050636902399,288230238712758271,576460614864470015,1152921367167893503,824633720831,1924145348607,4123168604159,8521215115263,17317308137471,34909494181887,70093866270719,140462610448383,281200098803711,562675075514367,1125625028935679,2251524935778303,4503324749463551,9006924376834047,18014123631575039,36028522141057023,72057319160020991,144114913197948927,288230101273804799,576460477425516543,1152921229728940031,1649267441663,3848290697215,8246337208319,17042430230527,34634616274943,69818988363775,140187732541439,280925220896767,562400197607423,1125350151028735,2251250057871359,4503049871556607,9006649498927103,18013848753668095,36028247263150079,72057044282114047,144114638320041983,288229826395897855,576460202547609599,1152920954851033087,3298534883327,7696581394431,16492674416639,34084860461055,69269232549887,139637976727551,280375465082879,561850441793535,1124800395214847,2250700302057471,4502500115742719,9006099743113215,18013298997854207,36027697507336191,72056494526300159,144114088564228095,288229276640083967,576459652791795711,1152920405095219199,6597069766655,15393162788863,32985348833279,68169720922111,138538465099775,279275953455103,560750930165759,1123700883587071,2249600790429695,4501400604114943,9005000231485439,18012199486226431,36026597995708415,72055395014672383,144112989052600319,288228177128456191,576458553280167935,1152919305583591423,13194139533311,30786325577727,65970697666559,136339441844223,277076930199551,558551906910207,1121501860331519,2247401767174143,4499201580859391,9002801208229887,18010000462970879,36024398972452863,72053195991416831,144110790029344767,288225978105200639,576456354256912383,1152917106560335871,26388279066623,61572651155455,131941395333119,272678883688447,554153860399103,1117103813820415,2243003720663039,4494803534348287,8998403161718783,18005602416459775,36020000925941759,72048797944905727,144106391982833663,288221580058689535,576451956210401279,1152912708513824767,52776558133247,123145302310911,263882790666239,545357767376895,1108307720798207,2234207627640831,4486007441326079,8989607068696575,17996806323437567,36011204832919551,72040001851883519,144097595889811455,288212783965667327,576443160117379071,1152903912420802559,105553116266495,246290604621823,527765581332479,1090715534753791,2216615441596415,4468415255281663,8972014882652159,17979214137393151,35993612646875135,72022409665839103,144080003703767039,288195191779622911,576425567931334655,1152886320234758143,211106232532991,492581209243647,1055531162664959,2181431069507583,4433230883192831,8936830510563327,17944029765304319,35958428274786303,71987225293750271,144044819331678207,288160007407534079,576390383559245823,1152851135862669311,422212465065983,985162418487295,2111062325329919,4362862139015167,8866461766385663,17873661021126655,35888059530608639,71916856549572607,143974450587500543,288089638663356415,576320014815068159,1152780767118491647,844424930131967,1970324836974591,4222124650659839,8725724278030335,17732923532771327,35747322042253311,71776119061217279,143833713099145215,287948901175001087,576179277326712831,1152640029630136319,1688849860263935,3940649673949183,8444249301319679,17451448556060671,35465847065542655,71494644084506623,143552238122434559,287667426198290431,575897802350002175,1152358554653425663,3377699720527871,7881299347898367,16888498602639359,34902897112121343,70931694131085311,142989288169013247,287104476244869119,575334852396580863,1151795604700004351,6755399441055743,15762598695796735,33776997205278719,69805794224242687,141863388262170623,285978576338026495,574208952489738239,1150669704793161727,13510798882111487,31525197391593471,67553994410557439,139611588448485375,283726776524341247,571957152676052991,1148417904979476479,27021597764222975,63050394783186943,135107988821114879,279223176896970751,567453553048682495,1143914305352105983,54043195528445951,126100789566373887,270215977642229759,558446353793941503,1134907106097364991,108086391056891903,252201579132747775,540431955284459519,1116892707587883007,216172782113783807,504403158265495551,1080863910568919039,432345564227567615,1008806316530991103,864691128455135231};
    long long int i,aa,b,ans=0;;
    cin >> aa >> b;
    for(i=0;i<=1769;i++)
    {
        if((a[i]>=aa)&&(a[i]<=b))
          ans++;
    }
    cout << ans;
    return 0;
    }


C. New Year and Domino
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so.

Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered1 through w from left to right.

Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid.

Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle?

Input

The first line of the input contains two integers h and w (1 ≤ h, w ≤ 500) – the number of rows and the number of columns, respectively.

The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#' — denoting an empty or forbidden cell, respectively.

The next line contains a single integer q (1 ≤ q ≤ 100 000) — the number of queries.

Each of the next q lines contains four integers r1ic1ir2ic2i (1 ≤ r1i ≤ r2i ≤ h, 1 ≤ c1i ≤ c2i ≤ w) — the i-th query. Numbers r1i andc1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle.

Output

Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle.

Sample test(s)
input
5 8
....#..#
.#......
##.#....
##..#.##
........
4
1 1 2 3
4 1 4 1
1 2 4 5
2 5 5 8
output
4
0
10
15
input
7 39
.......................................
.###..###..#..###.....###..###..#..###.
...#..#.#..#..#.........#..#.#..#..#...
.###..#.#..#..###.....###..#.#..#..###.
.#....#.#..#....#.....#....#.#..#..#.#.
.###..###..#..###.....###..###..#..###.
.......................................
6
1 1 3 20
2 10 6 30
2 10 7 30
2 2 7 7
1 7 7 7
1 8 7 8
output
53
89
120
23
0
2

解析:给一个图,#表示障碍,.表示通路,问总共有多少个连续2个.的块。只能横或者竖着计算。

思路:前缀维护。num1[505][505]记录横着计算时,到达当前这个点从0列开始共有多少个满足的块。num2[505][505]则是竖着计算。计算结果时,只需用末尾点-起始点,结果就是中间的答案。

CODE:

#include <bits/stdc++.h>
#define ll long long
using namespace std;

int h,w;
char mat[505][505];
int num1[505][505];
int num2[505][505];

int main()
{
    while(cin>>h>>w){
        for(int i=0;i<=h;i++)
            num1[i][0]=num2[0][i]=0;
        for(int i=1;i<=h;i++){
            int cnt=0;
            for(int j=1;j<=w;j++){
                //scanf("%c",&mat[i][j]);
                cin>>mat[i][j];
                //mat[i][j]=getchar();
                if(mat[i][j]=='.'){
                    cnt++;
                    if(cnt>1)
                        num1[i][j]=1+num1[i][j-1];
                    else
                        num1[i][j]=num1[i][j-1];
                }
                else{
                    cnt=0;
                    num1[i][j]=num1[i][j-1];
                }
            }
        }

        for(int i=1;i<=w;i++){
            int cnt=0;
            for(int j=1;j<=h;j++){
                if(mat[j][i]=='.'){
                    cnt++;
                    if(cnt>1)
                        num2[j][i]=1+num2[j-1][i];
                    else
                        num2[j][i]=num2[j-1][i];
                }
                else{
                    cnt=0;
                    num2[j][i]=num2[j-1][i];
                }
            }
        }

        int n; cin>>n;
        int x1,x2,y1,y2;
        while(n--){
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            ll ans=0;
            for(int i=x1;i<=x2;i++){
                ll t=num1[i][y2]-num1[i][y1];
                //cout<<t<<'a'<<endl;
                ans+=t;
            }
            for(int i=y1;i<=y2;i++){
                ll t=num2[x2][i]-num2[x1][i];
                //cout<<t<<'b'<<endl;
                ans+=t;
            }
            printf("%I64d\n",ans);
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值