NC213962. RikkawithCompositeNumber
描述
输入描述
The first line contains a 01-string of length 9. The i-th character is 1 if and only if digit i is inside D.
The input guarantees that D is not empty.
输出描述
Output a single integer, representing the expected number of turns.
The answer is guaranteed to be a rational number. You are required to output the answer module 998244353. Formally, if the simplest fraction representation of the answer is , you need to output .
示例1
输入:
100000000
输出:
3
说明:
The generator must return 111 in the third turn.示例2
输入:
001100000
输出:
499122178
说明:
There are 3 possibilities:C++(clang++11) 解法, 执行用时: 3ms, 内存消耗: 376K, 提交时间: 2020-11-22 09:50:12
#include <iostream> #include <cstdio> using namespace std; char s[20]; int g,P=998244353,ans; int qpow(int x,int y) { int r=1; while(y) { if(y&1)r=1ll*r*x%P; x=1ll*x*x%P; y/=2; } return r; } int chk(long long x) { for(int i=2;1ll*i*i<=x;i++)if(x%i==0)return 1; return 0; } void dfs(long long x,int y,int z) { if(chk(x))ans=(ans+1ll*z*y)%P; else { for(int i=1;i<=9;i++)if(s[i]=='1')dfs(x*10+i,1ll*y*g%P,z+1); } } int main() { scanf("%s",s+1); for(int i=1;i<=9;i++)if(s[i]=='1')g++; g=qpow(g,P-2); dfs(0,1,0); cout<<ans<<endl; return 0; }
pypy3 解法, 执行用时: 90ms, 内存消耗: 24788K, 提交时间: 2022-11-17 22:15:53
s = input() mod = 998244353 cnt = s.count('1') inv = pow(cnt, mod - 2, mod) def is_prime(x): for i in range(2, x): if x % i == 0: return False if i * i > x: break return True ans = 0 def dfs(x, prob, step): if not is_prime(x): global ans ans = (ans + prob * step) % mod return for i in range(9): if s[i] == '1': dfs(10 * x + i + 1, prob * inv % mod, step + 1) dfs(0, 1, 0) print(ans)