44H - Phone Number - CodeForces Solution


dp *1700

Please click on ads to support us..

Python Code:

import sys
sys.setrecursionlimit(2147483647)
MOD = 1000000007
input = sys.stdin.readline
    
s = input().strip()
n = len(s)
memo = {}

def dp(i,prev):
    if (i,prev) in memo:
        return memo[(i,prev)]
    if i==n:
        return 1
    nxt = int(s[i])+prev
    if nxt%2==0:
        memo[(i,prev)] = dp(i+1,nxt//2)
    else:
        memo[(i,prev)] = dp(i+1,nxt//2) + dp(i+1,nxt//2+1)
    return memo[(i,prev)]

ans = 0
for i in range(0,10):
    ans += dp(1,i)

def check(i,cur):
    if int(s[i])!=cur:
        return False
    if i==n-1:
        return True
    nxt = int(s[i+1])+(cur)
    if nxt%2==0:
        return check(i+1,nxt//2)
    else:
        if int(s[i+1])==nxt//2:
            return check(i+1,nxt//2)
        elif int(s[i+1])==nxt//2+1:
            return check(i+1,nxt//2+1)
        else:
            return False

if check(0,int(s[0])):
    print(ans-1)
else:
    print(ans)
            
    

C++ Code:

//f[i+1][(j+(s[i+1]-'0'))/2]+=f[i][j];
//if((j+(s[i+1]-'0'))%2)
//	f[i+1][(j+(s[i+1]-'0'))/2+1]+=f[i][j];
//i=当前位置,j=当前数字,f[i][j]=当前种类
#include<bits/stdc++.h>
using namespace std;
long long ba;
long long f[55][15];
string s;
bool check()
{
	for(long long i=1;i<s.size();i++)
	{
		if(abs(s[i]-s[i-1])>1)
			return 0;
	}
	return 1;
}
int main()
{
	cin>>s;
	for(long long i=0;i<10;i++)
		f[0][i]=1;
	for(long long i=0;i<s.size()-1;i++)
		for(long long j=0;j<10;j++)
		{
			f[i+1][(j+(s[i+1]-'0'))/2]+=f[i][j];
			if((j+(s[i+1]-'0'))%2)
				f[i+1][(j+(s[i+1]-'0'))/2+1]+=f[i][j];
		}
	for(long long i=0;i<10;i++)
		ba+=f[s.size()-1][i];
	if(check())
		cout<<ba-1;
	else
		cout<<ba;
	return 0;
}


Comments

Submit
0 Comments
More Questions

1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness
1611B - Team Composition Programmers and Mathematicians