poj 2800 找规律
来源:互联网 发布:三国志13英雄苍穹数据 编辑:程序博客网 时间:2024/05/26 02:54
Joseph's Problem
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7519 Accepted: 1977
Description
Joseph likes taking part in programming contests. His favorite problem is, of course, Joseph's problem.
It is stated as follows.
Of course, all of you know the way to solve this problem. The solution is very short, all you need is one cycle:
Here "x mod y" is the remainder of the division of x by y, But Joseph is not very smart. He learned the algorithm, but did not learn the reasoning behind it. Thus he has forgotten the details of the algorithm and remembers the solution just approximately.
He told his friend Andrew about the problem, but claimed that the solution can be found using the following algorithm:
Of course, Andrew pointed out that Joseph was wrong. But calculating the function Joseph described is also very interesting.
Given n and k, find ∑1<=i<=n(k mod i).
It is stated as follows.
- There are n persons numbered from 0 to n - 1 standing in a circle. The person numberk, counting from the person number 0, is executed. After that the person number k of the remaining persons is executed, counting from the person after the last executed one. The process continues until only one person is left. This person is a survivor. The problem is, given n and k detect the survivor's number in the original circle.
Of course, all of you know the way to solve this problem. The solution is very short, all you need is one cycle:
r := 0;for i from 1 to n dor := (r + k) mod i;return r;
Here "x mod y" is the remainder of the division of x by y, But Joseph is not very smart. He learned the algorithm, but did not learn the reasoning behind it. Thus he has forgotten the details of the algorithm and remembers the solution just approximately.
He told his friend Andrew about the problem, but claimed that the solution can be found using the following algorithm:
r := 0;for i from 1 to n dor := r + (k mod i);return r;
Of course, Andrew pointed out that Joseph was wrong. But calculating the function Joseph described is also very interesting.
Given n and k, find ∑1<=i<=n(k mod i).
Input
The input file contains n and k (1<= n, k <= 109).
Output
Output the sum requested.
Sample Input
5 3
Sample Output
7
Source
Northeastern Europe 2005
题意: 求给出的n,k的∑1<=i<=n(k mod i).
思路:直接暴力的话肯定要超了,所以需要想办法缩减
p = k/i
k mod i = k - p * i
k mod ( i + 1 ) = k - p * ( i + 1 ) = k mod i - p
k mod ( i + 2 ) = k - p * ( i + 2 ) = k mod i - 2 * p
对于连续的 i ,很多p都是一样的 . 相差的部分是一个等差数列 ,
i 的 范围是 从 i 到 min(k/p,n) 如果 p == 0 则 一直延续到最后
ac代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>using namespace std;typedef long long int LL;LL n,k;int main(){ freopen("joseph.in ","r",stdin); freopen("joseph.out","w",stdout);while(cin>>n>>k){LL ret=0;for(int i=1;i<=n;i++){LL p = k/i;if(p==0){ret += (n-i+1)*k;break;}else{LL q = k/p;if(q>n) q = n;LL md = k%i;LL num = q-i+1;LL temp = md*num-(num-1)*num/2*p;ret += temp;i=q;}}cout<<ret<<endl;} return 0;}
0 0
- poj 2800 找规律
- Poj 2800 Joseph's Problem - 找规律
- POJ 1068(找规律)
- 博弈 找规律 poj 2484
- POJ 1019 数学找规律
- POJ-2368-Buttons [找规律]
- poj 1063 找规律题目
- POJ 2800 Joseph’s Problem 数论找规律
- POJ 1218 囚犯问题 数学题找规律
- POJ 1095 找规律+递归打印
- POJ 2484 A Funny Game(找规律)
- POJ 1747 Expression(找规律 递归)
- poj 3090 (欧拉函数,找规律)
- poj 1426 数学推理找规律
- 【POJ】1663 - Number Steps(找规律)
- POJ 3517 找规律、递推
- poj 3731 Escape(找规律+组合数学)
- POJ 1405 Heritage 找规律+JAVA .
- 436. Find Right Interval 要重新做!!
- zoj1906,Relatives 欧拉函数
- Visual Assist X设置
- 开发者必看|Android 8.0 新特性及开发指南
- BZOJ 1257: [CQOI2007]余数之和sum 分块计算,基础数论
- poj 2800 找规律
- 《疯狂JAVA讲义》之六——java源程序中的注释
- JAVA中用final来修饰方法的参数
- WEB项目用Response下载
- 探究MySQL的DML提交事务的意义和DQL是否有必要提交事务
- 云计算的那些事儿之计算虚拟化
- Ubuntu16.04.1如何安装TensorFlow1.1.0(CPU版)
- 17年4月15日嵌入式Linux C语言(一)
- 查验身份证