美团codeM预赛B 模

 模

时间限制:1秒

空间限制:32768K

给定四个正整数a,b,c,k,回答是否存在一个正整数n,使得a*n在k进制表示下的各位的数值之和模b为c。 
输入描述:
第一行一个整数T(T <= 5,000)。 

接下来T行,每行四个正整数a,b,c,k(1 ≤ a ≤ 10^18; 2 ≤ k ≤ 10^18; 0 ≤ c < b ≤ 10^18)表示一个询问,所有输入都是十进制的。


输出描述:
对于每组数据输出一行,Yes表示存在,No表示不存在。

输入例子1:
2
3 9 5 10
7 3 1 10

输出例子1:
No
Yes
解题思路:设a转化为k进制后,各位和为sum,扩大x倍后,若没有进位的话变为sum*x,而一次进位对和的贡献为(1-k),所以(x*sum+y*(1-k)))%b=c,即x*sum+y*(1-k)=z*b+c,移项后变为x*sum+y*(k-1)+z*b=c,根据拓展欧几里得定理,c%gcd(b,gcd(sum,k-1))==0


#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <string>  
#include <algorithm>  
#include <queue>  
#include <stack>  
#include <cmath>  
#include <map>  
#include <bitset>  
#include <set>  
#include <vector>  
#include <functional>  
  
using namespace std;  
  
#define LL long long  
const int INF = 0x3f3f3f3f;  

LL gcd(LL a,LL b)
{
    return b?gcd(b,a%b):a;
}

int main()
{
    int t;
    LL a,b,c,k;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld%lld%lld",&a,&b,&c,&k);
        LL sum=0;
        while(a) {sum+=a%k;a/=k;}
        if(c%gcd(b,gcd(sum,k-1))==0) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值