Required Remainder

简介: Required Remainder

文章目录

一、A. Required Remainder

总结


一、A. Required Remainder

本题链接:A. Required Remainder


题目:


A. Required Remainder


time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard output


You are given three integers x,y and n. Your task is to find the maximum integer k such that 0≤k≤n that kmodx=y, where mod is modulo operation. Many programming languages use percent operator % to implement it.


In other words, with given x,y and n you need to find the maximum possible integer from 0 to n that has the remainder y modulo x.


You have to answer t independent test cases. It is guaranteed that such k exists for each test case.


Input

The first line of the input contains one integer t (1≤t≤5⋅1e4) — the number of test cases. The next t lines contain test cases.


The only line of the test case contains three integers x,y and n (2≤x≤1e9; 0≤y<x; y≤n≤1e9).


It can be shown that such k always exists under the given constraints.


Output

For each test case, print the answer — maximum non-negative integer k such that 0≤k≤n and kmodx=y. It is guaranteed that the answer always exists.


Example

input


7

7 5 12345

5 0 4

10 5 15

17 8 54321

499999993 9 1000000000

10 5 187

2 0 999999999

output

12339

0

15

54306

999999995

185

999999998

Note

In the first test case of the example, the answer is 12339=7⋅1762+5 (thus, 12339mod7=5). It is obvious that there is no greater integer not exceeding 12345 which has the remainder 5 modulo 7.


本博客给出本题截图:

6.png

题意:找到这么一个k使得k % x = y,k的取值范围是0到n,求k的最大值

TLE代码

#include <cstdio>
using namespace std;
int main()
{
    int t;
    scanf("%d", &t);
    while (t -- )
    {
        int x, y, n;
        scanf("%d%d%d", &x, &y, &n);
        for (int i = n; i >= 0; i -- )
            if (i % x == y)
            {
                printf("%d\n", i);
                break;
            }
    }
    return 0;
}

AC代码

#include <cstdio>
using namespace std;
int main()
{
    int t;
    scanf("%d", &t);
    while (t -- )
    {
        int x, y, n;
        scanf("%d%d%d", &x, &y, &n);
        int k = n % x;
        if (k >= y)
            printf("%d\n", n + y - k);
        else 
            printf("%d\n", n - x + y  -k);
    }
    return 0;
}

总结

简单数论小题,动手模拟一下即可

目录
相关文章
|
2月前
|
JSON 小程序 API
【企业微信开发】基于API接口的多类型消息收发与管理:IPAD协议下消息同步及批量发送开发设计
本文档详述企业微信23个消息接口,涵盖发送、撤回、同步等全场景功能。统一POST+JSON格式,以uuid标识实例。支持文本、多媒体、卡片、直播等类型,含语音转文字、已读标记、引用回复等;大文件需专用上传,群发限每日每人一次
【企业微信开发】基于API接口的多类型消息收发与管理:IPAD协议下消息同步及批量发送开发设计
|
2月前
|
人工智能 开发者
Qoder夜间优惠Night Qoder:使用AI模型更划算(低至2折)
阿里云Qoder推出“Night Qoder”夜间优惠计划:每晚22:00–次日8:00,Qwen3.7-Max享2折、Plus享4折,最高省80%。错峰时段模型能力不变,仅Credits计价优惠,覆盖Qoder CN/国际版,新用户Pro试用期亦可参与。(239字)
229 0
|
3月前
|
人工智能 自然语言处理 供应链
艾奇在线发布《艾奇GEO高维场景方法论白皮书》:从大词竞争到真实决策场景的系统跃迁
艾奇在线发布《GEO高维场景方法论白皮书》,提出“AI不响应关键词,而响应场景”,以九维决策向量(主体、地域、预算、风险等)重构营销逻辑,覆盖66个行业实践。白皮书系统构建SSI人群运营、V11内容质量模型及三层可信治理框架,推动企业从“大词竞争”迈向“认知资产建设”。
406 0
|
4月前
|
SQL 人工智能 JSON
当所有人都在谈论语义层,如何判断一个方案的上限?
对语义层做一次认真的拆解,为市场提供一把选型的衡量标尺。
|
5月前
|
存储 关系型数据库 MySQL
数据库备份恢复最佳实践:RTO评估、PITR时间点恢复与演练SOP
本文从真实故障案例出发,分析RTO评估方法、全量增量恢复策略、PITR时间点恢复实践,附备份恢复演练SOP。
|
小程序 前端开发 关系型数据库
告别“月月光”:Uni+Php校园系统小程序,给大学生的低成本创业方案,学业赚钱两不误
uni+Php,寓意“大学加技能”,融合技术与校园生活。轻量小程序整合跑腿、团购、打印等服务,助力学生技能变现。PHP低成本架构,快速落地,覆盖代取快递、宿舍团购、失物招领等高频需求,打造校园一站式服务平台,实现多渠道盈利,月入过万可期。
告别“月月光”:Uni+Php校园系统小程序,给大学生的低成本创业方案,学业赚钱两不误
|
人工智能 缓存 决策智能
AI律师数字分身:法律科技领域的多智能体架构实践
AI数字分身破解法律服务规模化难题,通过多模态智能体实现7×24小时咨询响应、案情结构化提取与智能分流。基于知识图谱与推理引擎,提升律所线索留存率与律师人效,推动法律服务降本增效。
1579 11
|
人工智能 前端开发 安全
构建现代交互式平台:CodeBuddy如何简化复杂系统开发
文章探讨了构建交互式平台的复杂架构挑战,涵盖前后端分离的五层架构设计。重点介绍了AI编程助手CodeBuddy在简化开发中的作用,包括智能代码生成、架构优化建议、跨技术栈支持、安全实践集成及文档生成等功能。通过实战案例展示,CodeBuddy显著提升开发效率与代码质量,助力团队应对复杂系统开发挑战,成为开发者不可或缺的工具。下载链接:腾讯云代码助手 CodeBuddy - AI 时代的智能编程伙伴。
796 10
阿里云百炼:零门槛一键搭建 DeepSeek-R1 满血版
本文介绍如何使用阿里云百炼平台和chatbox客户端,一键搭建DeepSeek R1满血版
1559 18