G. D-Function 题解 (快速幂, 组合数学)
原题链接:
https://codeforces.com/contest/1985/problem/G
题目:
思路:
要满足D(kn)==kD(n), k与n的每一位相乘都不能发生进位, k只能是一位数。
考虑n的位数可能有1e9,所以用到了快速幂。
代码:
#include <bits/stdc++.h> #define int long long using namespace std; const int mod=1e9+7; int fpow(int a, int n){//快速幂 int ans=a, sum=1; while(n){ if(n&1)sum*=ans, sum%=mod; ans*=ans;ans%=mod; n=n>>1; } return sum; } void solve(){ int l, r, k;cin>>l>>r>>k; if(k>=10){ cout<<0<<'\n'; } else { int pos;//确定每一位有几个数满足要求。 for(int i=1;i<=9;i++){ if(i*k>=10)break; else pos=i; } int sum=fpow(pos+1, r)+mod-fpow(pos+1, l); //类似于前缀和, 不加mod可能会出现负数。 cout<<sum%mod<<'\n'; } } signed main() { int T;cin>>T; while(T--){ solve(); } return 0; }
本文作者:成长宝地
本文链接:https://www.cnblogs.com/1747176348mi/p/18662914
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步