C. Hacking Cyphe(大数取mod)

该博客介绍了一种使用分治策略解决数字串是否能被整除的问题。给定一个数字串和两个整数a、b,我们需要判断是否可以将数字串分成两部分,使得这两部分分别能被a和b整除。通过计算数字串的前缀和后缀模运算,博主提供了C++代码实现来检查这种可能性。代码中使用了动态规划和快速幂运算,有效地计算了每个位置的模结果,并通过枚举分隔点来寻找满足条件的位置。
摘要由CSDN通过智能技术生成

https://codeforces.com/problemset/problem/490/C


题意:

给一个数字串,和两个整数a,b。

问能否将该串分成两部分,其前后两部分分别被这两个整数整除。

思路:

大数取mod,字符串Mod后由于取模的公式可得是等价的。

然后枚举断点i

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e6+100;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
char str[maxn];
LL pre[maxn],suf[maxn];
LL mod=0;
LL ksm(LL a,LL k){LL res=1;while(k>0){if(k&1) res=res*a%mod;k>>=1;a=a*a%mod;}return res%mod;}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  cin>>(str+1);
  LL a,b;cin>>a>>b;
  mod=b;
  LL len=strlen(str+1);
  for(LL i=1;i<=len;i++){
     pre[i]=(pre[i-1]*10%a+(str[i]-'0')%a)%a;
  }
  LL cnt=0;
  for(LL i=len;i>=1;i--){
     suf[i]=( ( (str[i]-'0')%b*ksm(10,cnt)%b)%b+suf[i+1]%b )%b;
     cnt++;
  }
    /*
  for(LL i=1;i<=len;i++) cout<<pre[i]<<" ";
  cout<<"\n";
  for(LL i=1;i<=len;i++) cout<<suf[i]<<" ";
  cout<<"\n";
*/
  LL pos1=0;LL pos2=0;bool flag=1;
  for(LL i=1;i<=len;i++){
    if(pre[i]==0&&suf[i+1]==0&&str[i+1]!='0'){
        pos1=i;pos2=i+1;
        flag=0;
        break;
    }
  }
  ///debug(pos1);debug(pos2);
  if(flag==1||pos2==0||pos1==0||pos2>len||pos1>len) cout<<"NO"<<"\n";
  else{
    cout<<"YES"<<"\n";
    for(LL i=1;i<=pos1;i++){
        cout<<str[i];
    }
    cout<<"\n";
    for(LL i=pos2;i<=len;i++){
        cout<<str[i];
    }
    cout<<"\n";
  }
return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值