题意:给定 n,k,求 while(i <=n) k % i的和。

析:很明显是一个数论题,写几个样例你会发现规律,假设 p = k / i.那么k  mod i = k - p*i,如果 k / (i+1) 也是p,那么就能得到 :

k mod (i+1) = k - p*(i+1) = k mod i - p。所以我们就能得到一个等差数列 k mod (i+1) - k mod i = -p,首项是 p % i。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <ctime>
#include <cstdlib>
#define debug puts("+++++")
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e6 + 5;
const LL mod = 1e9 + 7;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
inline int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }
inline int lcm(int a, int b){ return a * b / gcd(a, b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
LL solve(int a, int d, int n){
return (LL)((LL)n*a - (LL)n*(n-1)/2*d);
} int main(){
while(scanf("%d %d", &n, &m) == 2){
int i = 1;
LL ans = 0;
while(i <= n){
int a = m % i;
int d = m / i;
int cnt = n - i + 1;
if(d > 0) cnt = Min(cnt, a/d+1);
ans += solve(a, d, cnt);
i += cnt;
}
cout << ans << endl;
}
return 0;
}

题意:给定n, k,求出∑ni=1(k mod i)

UVa 1363 Joseph's Problem (数论)的更多相关文章

  1. UVA 1363 Joseph's Problem 找规律+推导 给定n,k;求k%[1,n]的和。

    /** 题目:Joseph's Problem 链接:https://vjudge.net/problem/UVA-1363 题意:给定n,k;求k%[1,n]的和. 思路: 没想出来,看了lrj的想 ...

  2. UVa 1363 - Joseph's Problem(数论)

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

  3. UVA 1363 Joseph's Problem

    https://vjudge.net/problem/UVA-1363 n 题意:求 Σ  k%i i=1 除法分块 如果 k/i==k/(i+1)=p 那么 k%(i+1)=k-(i+1)*p= k ...

  4. UVA 11490 - Just Another Problem(数论)

    11490 - Just Another Problem option=com_onlinejudge&Itemid=8&page=show_problem&category= ...

  5. UVa 1363 (数论 数列求和) Joseph's Problem

    题意: 给出n, k,求 分析: 假设,则k mod (i+1) = k - (i+1)*p = k - i*p - p = k mod i - p 则对于某个区间,i∈[l, r],k/i的整数部分 ...

  6. UVa 101 The Blocks Problem Vector基本操作

    UVa 101 The Blocks Problem 一道纯模拟题 The Problem The problem is to parse a series of commands that inst ...

  7. 【暑假】[深入动态规划]UVa 1380 A Scheduling Problem

     UVa 1380 A Scheduling Problem 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557 ...

  8. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

  9. UVA 305 Joseph (约瑟夫环 打表)

     Joseph  The Joseph's problem is notoriously known. For those who are not familiar with the original ...

随机推荐

  1. poj3532求生成树中最大权与最小权只差最小的生成树+hoj1598俩个点之间的最大权与最小权只差最小的路经。

    该题是最小生成树问题变通活用,表示自己开始没有想到该算法:先将所有边按权重排序,然后枚举最小边,求最小生成树(一个简单图的最小生成树的最大权是所有生成树中最大权最小的,这个容易理解,所以每次取最小边, ...

  2. Bootstrap3 为何无法显示Glyphicons 图标

    Bootstrap3 为何无法显示Glyphicons 图标 在CSS引入字体即可解决 @font-face { font-family: 'Glyphicons Halflings'; src: u ...

  3. 最短路——Dijkstra算法

    模板 水模板ing #include <cstdio> #include <cstring> #include <algorithm> #include <i ...

  4. linux显示系统时间

    date   查看系统时间 设置系统时间 # date --set “09/17/10 11:50" (月/日/年时:分:秒) # clock –systohc  将系统时间写入硬件时间

  5. 拷贝地图 CopyAndOverwriteMap()

    private void CopyAndOverwriteMap() { //Get IObjectCopy interface IObjectCopy objectCopy = new Object ...

  6. WebLogic中"域"的概念

    WebLogic 版权声明:本文为博主原创文章,未经博主允许不得转载. WebLogic Server中的域是逻辑上相关的一组 WebLogic Server 资源,可以作为一个单元进行管理.一个域中 ...

  7. 一个重绘MFC的文件传输client

     一个重绘MFC的文件传输client,TCP/IP协议的文件传输client(支持上传.下载.续传.管理等功能,本处仅仅选择了上传功能).从用户视觉上看,肯定比原生MFC界面要有感觉,啥也不说了 ...

  8. webpack-Manifest

    Manifest 在使用 webpack 构建的典型应用程序或站点中,有三种主要的代码类型: 你或你的团队编写的源码. 你的源码会依赖的任何第三方的 library 或 "vendor&qu ...

  9. vux-uploader 图片上传组件

    1.网址:https://github.com/greedying/vux-uploader 2.安装 npm install vux-uploader --save npm install --sa ...

  10. Linux - Ubuntu中文输入法安装(Ubuntu 12.04)

    Ubuntu中文输入法安装(Ubuntu 12.04) 本文地址:http://blog.csdn.net/caroline_wendy Ubuntu作为Linux常见的操作系统,是须要熟练使用的. ...