You are given two integers a and b, and q queries. The i-th query consists of two numbers li and ri, and the answer to it is the number of integers x such that li≤x≤ri, and ((xmoda)modb)≠((xmodb)moda). Calculate the answer for each query.

Recall that ymodz is the remainder of the division of y by z. For example, 5mod3=2, 7mod8=7, 9mod4=1, 9mod9=0.

Input
The first line contains one integer t (1≤t≤100) — the number of test cases. Then the test cases follow.

The first line of each test case contains three integers a, b and q (1≤a,b≤200; 1≤q≤500).

Then q lines follow, each containing two integers li and ri (1≤li≤ri≤1018) for the corresponding query.

Output
For each test case, print q integers — the answers to the queries of this test case in the order they appear.

Example
Input
2
4 6 5
1 1
1 3
1 5
1 7
1 9
7 10 2
7 8
100 200
Output
0 0 0 2 4
0 91
思路:数学题,没有什么思路就暴力打表找规律,我们可以发现以a*b为一个周期,这个周期内的特殊数的个数是确定的。那么我们就把[1,a *b]内的特殊数暴力找出来。dp[i]代表的是前i个数中有多少个特殊数。
对于输入的l和r,我们只需要看他们中分别有多少个a * b以及对a * b取余是多少,然后加起来就可以了。分别对l和r计算出这个数,然后再用fcs®-fcs(l-1)前缀和思想。具体看代码
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=4e4+100;
int dp[maxx];
int a,b,q;
int n;inline ll fcs(ll cnt)
{return (ll)dp[n-1]*(ll)(cnt/n)+(ll)dp[cnt%n];
}
int main()
{int t;scanf("%d",&t);while(t--){scanf("%d%d%d",&a,&b,&q);dp[0]=0;n=a*b;for(int i=1;i<=n;i++){if(i%a%b==i%b%a) dp[i]=dp[i-1];else dp[i]=dp[i-1]+1;}ll l,r;while(q--){scanf("%lld%lld",&l,&r);printf("%lld\n",fcs(r)-fcs(l-1));}}return 0;
}

努力加油a啊,(o)/~

Yet Another Counting Problem CodeForces - 1342C(规律+前缀和)相关推荐

  1. Military Problem CodeForces 1006E (dfs序)

    J - Military Problem CodeForces - 1006E 就是一道dfs序的问题 给定一个树, 然后有q次询问. 每次给出u,k, 求以u为根的子树经过深搜的第k个儿子,如果一个 ...

  2. CodeForces - 948C(前缀和 + 二分)

    链接:CodeForces - 948C 题意:N天,每天生产一堆雪体积 V[i] ,每天每堆雪融化 T[i],问每天融化了多少雪. 题解:对 T 求前缀和,求每一堆雪能熬过多少天,再记录一下多余的就 ...

  3. UVA10025 The ? 1 ? 2 ? ... ? n = k problem【数学规律】

    Given the following formula, one can set operators '+' or '-' instead of each '?', in order to obtai ...

  4. Balanced Substring CodeForces - 873B (思维+前缀和)

    Balanced Substring CodeForces - 873B You are given a string s consisting only of characters 0 and 1. ...

  5. P1865 A % B Problem (素数筛法,前缀和)

    题目描述 区间质数个数 输入输出格式 输入格式: 一行两个整数 询问次数n,范围m 接下来n行,每行两个整数 l,r 表示区间 输出格式: 对于每次询问输出个数 t,如l或r∉[1,m]输出 Cros ...

  6. Subsequence Hate CodeForces - 1363B(前缀和+dp)

    Shubham has a binary string s. A binary string is a string containing only characters "0" ...

  7. String Modification CodeForces - 1316B(规律)

    Vasya has a string s of length n. He decides to make the following modification to the string: Pick ...

  8. AC日记——Periodic RMQ Problem codeforces 803G

    G - Periodic RMQ Problem 思路: 题目给一段序列,然后序列复制很多次: 维护序列很多次后的性质: 线段树动态开点: 来,上代码: #include <cstdio> ...

  9. Codeforce Flea CodeForces - 32C 规律|思维

    题意 给我们一个n*m的表格 告诉我们每次跳跃的格子数量s 只能垂直或者水平跳跃 可以跳跃无限次 那么某格子能到达的不同格子的数量就是这个格子的值 求表格中值最大的格子的数量 分析 表面上看像是搜索 ...

最新文章

  1. Python数据科学-技术详解与商业实践视频教程
  2. Java动态excel模板
  3. 一类新算法研究智能飞行器航迹规划问题
  4. Apache java文件比对,Java Apache Commons的字符串比较
  5. 在控制台中实现“单词竞猜”游戏 C# 猜词游戏
  6. seo提交工具_经验分享:SEO新手面对新网站应该如何去优化
  7. 解决 ifconfig: command not found
  8. Winform DataGridView中利用WebClient异步加载显示网络地址的图片
  9. PHP原生实现,校验微信公众号||小程序服务器地址
  10. 元组-不仅仅是不可变的列表
  11. snipaste如何滚动截图_如何让windows10得心应手之工具篇2
  12. 计算机屏幕地图是不是地图,地图软件 如何在截图时超出一个屏幕?
  13. CDN的原理技术及使用方法
  14. Java应用题:模拟一个简单的购房商贷月供计算器,按照以下公式计算总利息和每月还款金额,总利息=贷款金额*利率,贷款年限不同利率也不同,这里规定只有三种年限、利率,见表
  15. drools -规则语法
  16. C# ThoughtWorks.QRCode.dll 生成完美二维码(大小 边距 备注 颜色 LOGO大小背景形状)
  17. 被印在纸币上的七大科学家
  18. php随机名人名言,自建网站随机名言警句API也就是一言随机名句api【教程】
  19. 堆积木(vector数组)
  20. 影响计算机安全的诸多因素中,影响计算机设备安全的诸多因素中,影响较大的是()。...

热门文章

  1. 虚拟网络的组建和应用课后习题答案
  2. mysql --explicit_【MySQL】 explicit_defaults_for_timestamp 参数解析
  3. cat查看tomcat日志 linux_linux怎么使用cat命令查看文件? linux中cat命令的使用方法
  4. python数据分析准备_使用Python进行数据分析I 环境准备
  5. 网站使用手机相机_相机拍完照片,如何传到手机上?详细步骤教你使用
  6. Android开发之EditText限制输入中文以及字数长度限制的标准姿势
  7. php 实现同一个账号同时只能一个人登录
  8. 5.SpringMVC
  9. Docker 安装创建
  10. Java并发编程-volatile关键字介绍