QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#377826 | #7996. 报数 IV | Destiny | TL | 8ms | 146984kb | C++17 | 5.0kb | 2024-04-05 18:27:50 | 2024-04-05 18:27:50 |
Judging History
answer
#include<bits/stdc++.h>
#ifndef xxx
#define dbg(...) ;
#endif
using namespace std;
template<class T> ostream &operator<<(ostream &o, const vector <T> &v) { bool f=false; for(T i:v) { f?o<<' ':o; o<<(i); f=true; } return o; }
template<class T> void sort(T &v) { std::sort(v.begin(), v.end()); }
template<class T, class C> void sort(T &v, C c) { std::sort(v.begin(), v.end(), c); }
template<class T> void reverse(T &v) { std::reverse(v.begin(), v.end()); }
template<class T> bool is_sorted(T &v) { return std::is_sorted(v.begin(), v.end()); }
template<class T> inline void _min(T &x, const T &y) { x>y?x=y:x; }
template<class T> inline void _max(T &x, const T &y) { x<y?x=y:x; }
istream &operator>>(istream &i, __int128_t &x) { x=0; short f=1; char c=0; while(!isdigit(c)) { if(c=='-')f=-1; c=i.get(); } while(isdigit(c))x=x*10+c-'0', c=i.get(); x*=f; return i; }
ostream &operator<<(ostream &o, __int128_t x) { if(x==0) { o<<0; return o; } bool f=false; string s; if(x<0)f=true, x=-x; while(x)s+=x%10+'0', x/=10; reverse(s); if(f)o<<'-'; o<<s; return o; }
mt19937 mt(time(0));
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
constexpr int maxn=1e4+5;
constexpr int mod=1e9+7;
constexpr int inf=0x3f3f3f3f;
constexpr ll INF=0x3f3f3f3f3f3f3f3fll;
constexpr double pi=acos(-1.0);
constexpr double eps=1e-9;
template <int m>
struct static_modint
{
using mint=static_modint;
public:
static_modint() : v(0) {}
template <class T>
static_modint(T x)
{
x=(x%(ll)(umod()));
if(x<0)x+=umod();
v=(unsigned int)(x);
}
static_modint(bool x) { v=((unsigned int)(x)%umod()); }
unsigned int val() const { return v; }
mint &operator++()
{
v++;
if(v==umod()) v=0;
return *this;
}
mint &operator--()
{
if(v==0) v=umod();
v--;
return *this;
}
mint operator++(int)
{
mint result=*this;
++*this;
return result;
}
mint operator--(int)
{
mint result=*this;
--*this;
return result;
}
mint &operator+=(const mint &rs)
{
v+=rs.v;
if(v>=umod()) v-=umod();
return *this;
}
mint &operator-=(const mint &rs)
{
if(v<rs.v) v+=umod();
v-=rs.v;
return *this;
}
mint &operator*=(const mint &rs)
{
ull z=v;
z*=rs.v;
v=(unsigned int)(z%umod());
return *this;
}
mint &operator/=(const mint &rs) { return *this=*this*rs.inv(); }
mint operator+() const { return *this; }
mint operator-() const { return mint()-*this; }
mint pow(ll n) const
{
assert(n>=0);
mint x=*this, r=1;
while(n)
{
if(n&1) r*=x;
x*=x;
n>>=1;
}
return r;
}
mint inv() const
{
//assert(v);
return pow(umod()-2);
}
istream &operator>>(istream &in)
{
in>>v;
*this=static_modint(v);
return in;
}
ostream &operator<<(ostream &o)const
{
o<<v;
return o;
}
friend mint operator+(const mint &l, const mint &r)
{
return mint(l)+=r;
}
friend mint operator-(const mint &l, const mint &r)
{
return mint(l)-=r;
}
friend mint operator*(const mint &l, const mint &r)
{
return mint(l)*=r;
}
friend mint operator/(const mint &l, const mint &r)
{
return mint(l)/=r;
}
friend bool operator==(const mint &l, const mint &r)
{
return l.v==r.v;
}
friend bool operator!=(const mint &l, const mint &r)
{
return l.v!=r.v;
}
friend bool operator<(const mint &l, const mint &r)
{
return l.v<r.v;
}
friend bool operator>(const mint &l, const mint &r)
{
return l.v>r.v;
}
friend bool operator<=(const mint &l, const mint &r)
{
return l.v<=r.v;
}
friend bool operator>=(const mint &l, const mint &r)
{
return l.v>=r.v;
}
friend ostream &operator<<(ostream &o, mint u)
{
u.operator<<(o);
return o;
}
friend istream &operator>>(istream &in, mint &u)
{
u.operator>>(in);
return in;
}
private:
unsigned int v;
static constexpr unsigned int umod() { return m; }
};
typedef static_modint<mod> mint;
int F(int x)
{
int ans=0;
while(x)
{
ans+=x%10;
x/=10;
}
return ans;
}
int k, m;
string N;
int timestamp=0;
int vis[1005][2][2][9005];
mint f[1005][2][2][9005];
mint dfs(int pos, bool limited, bool leading_zero, int sum)
{
if(pos==0)
{
for(int i=1; i<=k-1; i++)
{
sum=F(sum);
}
return sum==m;
}
if(vis[pos][limited][leading_zero][sum]==timestamp)return f[pos][limited][leading_zero][sum];
mint ans=0;
int lim=limited?N[N.size()-pos]-'0':9;
for(int i=0; i<=lim; i++)
{
ans+=dfs(pos-1, limited&&i==lim, leading_zero&&!i, sum+i);
}
vis[pos][limited][leading_zero][sum]=timestamp;
return f[pos][limited][leading_zero][sum]=ans;
}
mint calc()
{
timestamp++;
return dfs(N.size(), true, true, 0);
}
mint getans()
{
_min(k, 4);
return calc();
}
int main()//MARK: main
{
#ifndef xxx
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#endif
cout<<fixed<<setprecision(10);
int t;
cin>>t;
while(t--)
{
cin>>N>>k>>m;
cout<<getans()<<'\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 8ms
memory: 145128kb
input:
2 114 1 5 514 2 10
output:
8 10
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 4ms
memory: 146984kb
input:
5 114 1 5 514 2 10 114514 3 7 1919810 2 13 1145141919810114514191981011451419198101145141919810114514191981011451419198101145141919810114514191981011451419198101145141919810 1 79
output:
8 10 12724 504 481046284
result:
ok 5 lines
Test #3:
score: -100
Time Limit Exceeded
input:
5 3134666912140933323880320519044791121794814671711104987304374190280064994554822259889216567113228716903875026053927191961850586115167336109747673868148288830282192461669674173201533887392623483710043038941036243583011049049915834937139438028987629569618561762595613799223807979488245056374812076511...