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 ...
随机推荐
- SPOJ VJudge QTREE - Query on a tree
Query on a tree Time Limit: 851MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Submi ...
- HDU 6278 主席树(区间第k大)+二分
Just h-index Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)To ...
- mybatisplus代码生成器
一.随便建一个springboot工程,在pom文件中导入依赖 <!-- 模板引擎 --> <dependency> <groupId>org.apache.vel ...
- 系统性能不够原因可能是cpu不够,内存不够等等
1.Linux系统可以通过top命令查看系统的CPU.内存.运行时间.交换分区.执行的线程等信息. 通过top命令可以有效的发现系统的缺陷出在哪里.是内存不够.CPU处理能力不够.IO读写过高. 2. ...
- zedboard硬件连接过程
1. ZedBoard – Connect a 2nd micro-USBcable between the host machine and connector J17 (JTAG) 2. ...
- XMLHttpRequest对象解读
<!DOCTYPE html> <html> <body> <script> function reqListener () { console.log ...
- C#调用国家气象局天气预报接口
原文:C#调用国家气象局天气预报接口 一.需求 最近,刚好项目中有天气预报查询功能的需求,要求录入城市名称,获取该城市今日天气信息及相关气象生活辅助信息等. 例如:查询北京市天气 结果为: 今日北京天 ...
- Android反复闹钟(每天)的实现
MainActivity例如以下: package cc.cc; import java.util.Calendar; import java.util.Locale; import android. ...
- Eclipse中jvm执行库的Access restriction问题的解决方法
今天在写代码的时候遇到了jre system libraries的訪问限制问题,该库是jvm执行的依赖库rt.jar,解决方式例如以下: 步骤: (1)项目右击.出现Build Path.点击进入Ja ...
- Linux下使用inotify实现对文件的监控
项目中,要实现用户通过网页设置參数,后台接收数据然后写串口. 网页写数据到本地文件,使用inotify监控文件的IN_MODIFY事件.当文件被改动,然后触发写串口事件. 第一个程序只把要监控的文件增 ...