题解:http://vjudge.net/problem/viewProblem.action?id=43952

把<10^6的素数先搞出来,然后枚举每个

对于p
筛掉(a * n + b) % p = 0的
分两种情况
1. a % p = 0 的话,(a * n + b) % p = b % p,
   b % p = 0 的话才能使 (a * n + b) % p = 0 ,然后直接答案为0
2. a % p != 0 的话,先求出最小的n0,使得(a * n0 + b) % p = 0
    然后(a * (n0 + x) + b) % p = (a * x) % p = 0 ,p是素数,所以x 是p的倍数。。。
    然后筛掉n0, n0 + p, n0 + 2 * p....

最后统计下就行了

#include <iostream>
#include <vector>
#include <algorithm>using namespace std;// maximum prime trial divisor we need (up to sqrt of max a*U+b)
const long long MAX_PRIME = 1000000;// maximum range of values of n to consider (U-L+1)
const long long MAX_RANGE = 1000000+1;vector<int> primes;
bool prime[MAX_PRIME+1];long long gcd(long long a, long long b)
{if (!b) return a;return gcd(b, a%b);
}long long gcd(long long a, long long b, long long &s, long long &t)
{long long r, r1, r2, a1, a2, b1, b2, q;a1 = b2 = 1;a2 = b1 = 0;while (b) {q = a / b;r = a % b;r1 = a1 - q*b1;r2 = a2 - q*b2;a = b;a1 = b1;a2 = b2;b = r;b1 = r1;b2 = r2;}s = a1;t = a2;return a;
}// generate all primes up to max(sqrt(a*U+b))
void genprimes()
{fill(prime, prime+MAX_PRIME+1, true);prime[0] = prime[1] = false;// use a sievefor (long long p = 2; p < MAX_PRIME+1; p++) {if (!prime[p]) continue;primes.push_back(p);for (long long k = p*p; k < MAX_PRIME+1; k += p) {prime[k] = false;}}//cout<<primes.size()<<endl;
}bool isprime(long long n)
{// if it's below max prime, just check the sieveif (n <= MAX_PRIME) {return prime[n];}// trial division for the restfor (unsigned int i = 0; i < primes.size(); i++) {if (n % primes[i] == 0) return false;}return true;
}bool solve(int id)
{long long a, b, L, U;cin >> a;if (!a) return false;cin >> b >> L >> U;cout << "Case " << id << ": ";// special case: if gcd(a, b) > 1, then the only potential prime is when// n == 0 and b is prime.if (gcd(a, b) > 1) {if (L == 0 && isprime(b)) {cout << 1 << endl;} else {cout << 0 << endl;}return true;}// gcd(a, b) = 1, do a sieve on range of nbool nprime[MAX_RANGE];int range = U - L + 1;fill(nprime, nprime + range, true);// special cases//// if a*n+b <= 2 we will have to account for that//// n = 0 (only occurs if L = 0)// n = 1 (if L <= 1)if (L == 0) {nprime[0] = isprime(b);}if (L <= 1) {nprime[1-L] = isprime(a+b);}long long maxval = a * U + b;for (unsigned int i = 0; i < primes.size(); i++) {long long p = primes[i];// p cannot divide b since gcd(a,b) = 1if (a % p == 0) continue;// p cannot divide more a*n+bif (p*p > maxval) break;// solve for n in a*n+b = 0 mod plong long s, t;gcd(a, p, s, t);int n_start =  (-(b*s)) %p;if (n_start < 0) n_start += p;int Lrem = L % p;n_start -= Lrem;if (n_start < 0) n_start += p;// get past the first multiple which is prime!while (a*(n_start + L)+b <= p) {n_start += p;}// finally do the sievewhile (n_start < range) {nprime[n_start] = false;n_start += p;}}#ifdef DEBUGfor (int i = 0; i < range; i++) {if (nprime[i]) {cout << "  " << a*(i+L)+b << endl;}}
#endifcout << count(nprime, nprime+range, true) << endl;return true;
}int main()
{genprimes();int id = 1;while (solve(id++));return 0;
}

UVA Live 6068相关推荐

  1. [搜索]UVa 129 困难的串

    题意:将一个包含两个相邻的重复子串的子串,称为"容易的串",其他为"困难的串". 输入正整数n和l,输出由前l个字符组成的,字典序第n小的困难的串. 输入样例: ...

  2. uva 401.Palindromes

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  3. Uva 3767 Dynamic len(set(a[L:R])) 树套树

    Dynamic len(set(a[L:R])) Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://uva.onlinejudge.org/in ...

  4. UVA 11752 超级幂

    UVA 11752 超级幂 Z - The Super Powers Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & ...

  5. UVa 11174 - Stand in a Line

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  6. UVa 10112 - Myacm Triangles

    UVa第一卷最后一题. 求内部不含点并且面积最大的三角形. 暴力. 代码如下: 1 #include<iostream> 2 #include<cstdio> 3 #inclu ...

  7. UVa 10180 - Rope Crisis in Ropeland!

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=41&pa ...

  8. Uva 10074【递推dp】

    UVa 10074 题意:求01矩阵的最大子0矩阵. http://www.csie.ntnu.edu.tw/~u91029/MaximumSubarray.html#2 这里说的很清楚.先求Larg ...

  9. UVA 116 Unidirectional TSP DP

    题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&p ...

最新文章

  1. win7为什么打开桌面上的计算机很卡很慢,windows7很卡怎么处理_windows7系统卡慢的解决方法...
  2. lingo 一些函数
  3. python----yield(generator)生成器
  4. 【Win32汇编】__declspec(naked)裸函数
  5. LeetCode 167. 两数之和 II - 输入有序数组 思考分析
  6. 队友招募!60天一起搞定MySQL所有重点
  7. 【Flink】FileNotFoundException: JAR file does not exist: -ynm -yst
  8. NodsJs第六天笔记
  9. 设计模式之责任链模式(Chain of Responsibility )
  10. 从零开始--系统深入学习android(实践-让我们开始写代码-Android框架学习-3. 菜单)...
  11. 萦绕在头脑中的思路_我的编程梦们 【更新至2010.06.03】
  12. hdu2243 ac自动机
  13. mybatis 插入一条记录 参数为map的写法【用遍历Map的key和value的方式,可以实现只插入有效值】...
  14. PYTHON之路(九)
  15. Jade报错:Invalid indentation,you can use tabs or spaces but not both问题
  16. 官方授权正版 Avast AntiTrack Premium 反跟踪工具软件
  17. @Secured注解验证无法通过的问题
  18. Spring、SpringMVC
  19. 在追梦的路上,唯独脚踏实地,才能梦想成真
  20. 48本实体书包邮免费送!

热门文章

  1. 全面布局大数据平台,银科控股签约神策数据
  2. Java Socket编程如何建立两者关系
  3. Ubuntu 10.04下SSH配置
  4. 100c之31:哥德巴赫猜想
  5. Win7 一键获得管理所有权限(最高权限)注册表
  6. JavaScript实现浏览器菜单的一些功能
  7. 跟小段一起学Solaris(20)---ipFilter防火墙
  8. go to ifm as frequently as possible
  9. white board and magnet
  10. 《刺杀小说家》读后感