CodeForces - 1567C Carrying Conundrum(思维/状压)
Posted Frozen_Guardian
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 1567C Carrying Conundrum(思维/状压)相关的知识,希望对你有一定的参考价值。
题目链接:点击查看
题目大意:规定加法中使用隔项进位,问给定的 n n n 有多少种方案可以通过 “隔项进位加法” 得到
题目分析:隔项进位意味着奇偶位置的数字互不影响,所以将奇偶位置的两个数字拿出来,设为 a a a 和 b b b。不难看出组合出某个数字 x x x 的方案为 x + 1 x+1 x+1,即 { 0 + ( x ) , 1 + ( x − 1 ) , ⋯ , x + ( 0 ) } \\{0+(x),1+(x-1),\\cdots,x+(0)\\} {0+(x),1+(x−1),⋯,x+(0)}。所以总方案数为 ( a + 1 ) ∗ ( b + 1 ) (a+1)*(b+1) (a+1)∗(b+1),最终答案需要减去二,因为组合出来的数必须非零。
因为时间限制比较宽裕,所以本题可以直接状压枚举哪些位置是进位的,然后统计就好了
代码:
// Problem: C. Carrying Conundrum
// Contest: Codeforces - Codeforces Round #742 (Div. 2)
// URL: https://codeforces.com/contest/1567/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
#define lowbit(x) (x&-x)
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{
T f=1;x=0;
char ch=getchar();
while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
x*=f;
}
template<typename T>
inline void write(T x)
{
if(x<0){x=~(x-1);putchar('-');}
if(x>9)write(x/10);
putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=1e6+100;
int main()
{
#ifndef ONLINE_JUDGE
// freopen("data.in.txt","r",stdin);
// freopen("data.out.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);
int w;
cin>>w;
while(w--) {
string s;
cin>>s;
int ans1=0,ans2=0;
for(int i=0;i<(int)s.size();i++) {
if(i&1) {
ans1=ans1*10+s[i]-'0';
} else {
ans2=ans2*10+s[i]-'0';
}
}
cout<<1LL*(ans1+1)*(ans2+1)-2<<endl;
}
return 0;
}
// Problem: C. Carrying Conundrum
// Contest: Codeforces - Codeforces Round #742 (Div. 2)
// URL: https://codeforces.com/contest/1567/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
#define lowbit(x) (x&-x)
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{
T f=1;x=0;
char ch=getchar();
while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
x*=f;
}
template<typename T>
inline void write(T x)
{
if(x<0){x=~(x-1);putchar('-');}
if(x>9)write(x/10);
putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=1e6+100;
int sum[N];
void init() {
for(int i=0;i<10;i++) {
for(int j=0;j<10;j++) {
sum[i+j]++;
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("data.in.txt","r",stdin);
// freopen("data.out.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);
init();
int w;
cin>>w;
while(w--) {
int n;
read(n);
LL ans=-2;
for(int i=0;i<1<<8;i++) {
int res=1,nn=n;
for(int j=0;j<10;j++,nn/=10) {
int cur=nn%10;
if(i>>j&1) {
cur+=10;
}
if(j>=2&&((i>>(j-2))&1)) {
cur--;
}
if(cur<0) {
res=0;
break;
}
res*=sum[cur];
}
ans+=res;
}
cout<<ans<<endl;
}
return 0;
}
以上是关于CodeForces - 1567C Carrying Conundrum(思维/状压)的主要内容,如果未能解决你的问题,请参考以下文章
CodeForces - 1567C Carrying Conundrum(思维/状压)