UVa 1363 Joseph's Problem (数论)
题意:给定 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 (数论)的更多相关文章
- 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的想 ...
- UVa 1363 - Joseph's Problem(数论)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 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 ...
- UVA 11490 - Just Another Problem(数论)
11490 - Just Another Problem option=com_onlinejudge&Itemid=8&page=show_problem&category= ...
- 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的整数部分 ...
- UVa 101 The Blocks Problem Vector基本操作
UVa 101 The Blocks Problem 一道纯模拟题 The Problem The problem is to parse a series of commands that inst ...
- 【暑假】[深入动态规划]UVa 1380 A Scheduling Problem
UVa 1380 A Scheduling Problem 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557 ...
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- UVA 305 Joseph (约瑟夫环 打表)
Joseph The Joseph's problem is notoriously known. For those who are not familiar with the original ...
随机推荐
- 洛谷P2057 善意的投票
题目描述 幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可以投和自己本来 ...
- 通过调用C语言的库函数与在C代码中使用内联汇编两种方式来使用同一个系统调用来分析系统调用的工作机制
通过调用C语言的库函数与在C代码中使用内联汇编两种方式来使用同一个系统调用来分析系统调用的工作机制 前言说明 本篇为网易云课堂Linux内核分析课程的第四周作业,我将通过调用C语言的库函数与在C代码中 ...
- Assigning to "id<CALayerDelegate> _Nullable" from incompatible type "ZXCapture *const __strong" 的警告提示信息
该警告提示信息,是说,设置了代理对象,但是并没有继承它的代理.下图中,可以看出,警告信息提示我们没有继承“CALayerDelegate”的代理. 解决方法,很简单,(在 @interface 文件中 ...
- Linux硬件监控
https://blog.csdn.net/qq_30353203/article/details/62222882
- 洛谷 P4720 【模板】扩展 / 卢卡斯 模板题
扩展卢卡斯定理 : https://www.luogu.org/problemnew/show/P4720 卢卡斯定理:https://www.luogu.org/problemnew/show/P3 ...
- POJ 2135 最小费用最大流 入门题
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19207 Accepted: 7441 Descri ...
- [bzoj2780][Spoj8093]Sevenk Love Oimaster_广义后缀自动机
Sevenk Love Oimaster bzoj-2780 Spoj-8093 题目大意:给定$n$个大串和$m$次询问,每次给出一个字符串$s$询问在多少个大串中出现过. 注释:$1\le n\l ...
- T1365 浴火银河星际跳跃 codevs
http://codevs.cn/problem/1365/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 小 K 又在玩浴 ...
- [Bzoj1767][Ceoi2009]harbingers (树上斜率优化)
1767: [Ceoi2009]harbingers Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 451 Solved: 120[Submit][S ...
- SAS学习笔记 - R的数据操作
1.对象 1.1 对象及其内在属性 R中的处理数据就是对象,每个对象可以包含多个元素.对象有两个内在属性:类型和长度.类型是对象元素的基本种类,共四种:数值型,字符型,复数型和逻辑型.对象的类型和长度 ...